我有这个方法 paint() 接收 Graphics2D 参数。发生的奇怪事情是,除非存在 System.out.println(我在下面的块中注释掉),否则画布不会绘制任何东西。
public class Map{
public void paint(Graphics2D g){
//fill background to black
g.setColor(Color.black);
g.fillRect(0, 0, TILE_SIZE*WIDTH, TILE_SIZE*HEIGHT);
//draw the tiles and buildings
for(int i=0;i<WIDTH;i++){
for(int j=0;j<HEIGHT;j++){
if(map[j][i] == CLEAR){
//System.out.println("");
g.setColor(Color.gray);
g.fillRect(i*TILE_SIZE, j*TILE_SIZE, TILE_SIZE, TILE_SIZE);
g.setColor(Color.red);
g.drawRect(i*TILE_SIZE, j*TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
}
}
}
这里我使用 BufferStrategy 在 Canvas 上绘制并将其添加到 Frame 中。这个方法在 Map 类中,它将从 BufferStrategy 的 getDrawGraphics() 方法传递一个 Graphics2D(我希望很多人熟悉这些东西以理解我在做什么)。
public class MapTest extends Canvas{
private Map map;
public MapTest(){
Frame frame = new Frame("MAP");
frame.add(this);
frame.setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
//draw the map
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
//g.translate(100, 100);
map.paint(g);
g.dispose();
strategy.show();
}
}
此代码来自 Canvas 类。如您所见,paint() 方法与 Canvas 类(我将其命名为 GameTest)是分开的。因此,如果我注释掉 println 语句,则画布中不会显示任何图形,否则会正确显示。谁能帮帮我???