无法绘制 BufferedImage
Cannot draw a BufferedImage
我有一个播放器class
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import game.input.InputHandler;
import game.player.Missile;
public class Player{
private BufferedImage sprite;
public int x, y, width, height;
private final double speed = 5.0d;
private Missile missile;
public Player(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
try{
URL url = this.getClass().getResource("ship.png");
sprite = ImageIO.read(url);
} catch(IOException e){}
}
public void keyPlayer(double delta, InputHandler i){
if(i.isKeyDown(KeyEvent.VK_D)){
if(this.x>=1240) return;
else this.x+=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_A)){
if(this.x<=0) return;
else this.x-=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_SPACE)){
Missile missile = new Missile(this.x);
}
}
public void update(InputHandler inputP){
keyPlayer(2.0, inputP);
if(missile != null) missile.update(0);
}
public void Draw(Graphics a){
a.drawImage(sprite,x,y,width,height,null);
if(missile!=null) a.drawImage(missile.spriteM,missile.x,missile.y,2,6,null);
}
public Rectangle getBounds(){
return new Rectangle(x,y,width,height);
}
}
和一枚导弹class:
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
public class Missile{
public BufferedImage spriteM;
public int x;
public int y=504;
private Timer timer2;
public Missile(int x){
this.x=x;
this.y=504;
this.spriteM=spriteM;
try{
URL url = this.getClass().getResource("missile.png");
spriteM = ImageIO.read(url);
} catch(IOException e){}
//Draw(game.ShooterGame.bbg);
System.out.println("Missile launched!");
}
/*public void Draw(){
g.drawImage(sprite,x,y,2,6,null);
}*/
public void update(int sec){
System.out.println("Updating missile");
timer2 = new Timer();
timer2.schedule(new Move(), sec*1000);
}
class Move extends TimerTask{
public void run(){
if(2>1){
y+=3;
}
}
}
}
玩家发射一枚导弹,飞到屏幕上,增加"y"。我的问题是:出于某种原因,导弹没有出现在屏幕上(即使 "Missile launched!" 出现了),我无法弄清楚哪里出了问题。
我在 backBuffer.getGraphics 中使用 JFrame 和 drawImage。我的想法是在 Player class 中绘制导弹,而不是在 Missile class.
中绘制导弹
谢谢。
我的第一个猜测是抛出 IOException
并且图像未加载,或者您应该在 JPanel
或您绘制图形的任何地方安排 repaint()
.
你能给我们更多的信息吗?
我有一个播放器class
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import game.input.InputHandler;
import game.player.Missile;
public class Player{
private BufferedImage sprite;
public int x, y, width, height;
private final double speed = 5.0d;
private Missile missile;
public Player(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
try{
URL url = this.getClass().getResource("ship.png");
sprite = ImageIO.read(url);
} catch(IOException e){}
}
public void keyPlayer(double delta, InputHandler i){
if(i.isKeyDown(KeyEvent.VK_D)){
if(this.x>=1240) return;
else this.x+=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_A)){
if(this.x<=0) return;
else this.x-=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_SPACE)){
Missile missile = new Missile(this.x);
}
}
public void update(InputHandler inputP){
keyPlayer(2.0, inputP);
if(missile != null) missile.update(0);
}
public void Draw(Graphics a){
a.drawImage(sprite,x,y,width,height,null);
if(missile!=null) a.drawImage(missile.spriteM,missile.x,missile.y,2,6,null);
}
public Rectangle getBounds(){
return new Rectangle(x,y,width,height);
}
}
和一枚导弹class:
package game.player;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
public class Missile{
public BufferedImage spriteM;
public int x;
public int y=504;
private Timer timer2;
public Missile(int x){
this.x=x;
this.y=504;
this.spriteM=spriteM;
try{
URL url = this.getClass().getResource("missile.png");
spriteM = ImageIO.read(url);
} catch(IOException e){}
//Draw(game.ShooterGame.bbg);
System.out.println("Missile launched!");
}
/*public void Draw(){
g.drawImage(sprite,x,y,2,6,null);
}*/
public void update(int sec){
System.out.println("Updating missile");
timer2 = new Timer();
timer2.schedule(new Move(), sec*1000);
}
class Move extends TimerTask{
public void run(){
if(2>1){
y+=3;
}
}
}
}
玩家发射一枚导弹,飞到屏幕上,增加"y"。我的问题是:出于某种原因,导弹没有出现在屏幕上(即使 "Missile launched!" 出现了),我无法弄清楚哪里出了问题。
我在 backBuffer.getGraphics 中使用 JFrame 和 drawImage。我的想法是在 Player class 中绘制导弹,而不是在 Missile class.
中绘制导弹谢谢。
我的第一个猜测是抛出 IOException
并且图像未加载,或者您应该在 JPanel
或您绘制图形的任何地方安排 repaint()
.
你能给我们更多的信息吗?