我想在我画的矩形上方绘制一个图像,但问题是只有当我修改 (h/w) 以使它们与矩形不匹配时,图像才会出现在矩形上
int x = w / 2 - 20;
int y = h / 2 - 20;
g.setColor(255, 255, 255);
g.fillRect(0, 0, w, h);
g.setColor(0, 0, 255);
g.fillRect(0, h / 2, h, w);
g.drawImage(img, x, y,Graphics.BASELINE);
这样图像就不会出现
class GameFish extends Canvas {
int h = getHeight();
int w = getWidth();
int x = w / 2 - 20;
int y = h / 2 - 20;
int pas = 10;
Timer tim= new Timer();
myTask matache= new myTask();
Image img;
public GameFish(){
tim.schedule(matache, 0,100);
}
protected void paint(Graphics g) {
try {
img = Image.createImage("/Images/fish1.png");
} catch (IOException ex) {
ex.printStackTrace();
}
g.setColor(255, 255, 255);
g.fillRect(0, 0, w, h);
g.setColor(0, 0, 255);
g.drawImage(img, x, y,Graphics.VCENTER|Graphics.HCENTER);
}
class myTask extends TimerTask{
public void run() {
if(x<0 || x>w-40)
{
pas=-pas;
}
x+=pas;
repaint();
}
}
}