【发布时间】:2013-10-30 12:38:55
【问题描述】:
我有一些这样的代码:
public void paintComponent(Graphics graphics){
graphics.setColor(Color.WHITE);
for (GameObject object : GameArea.objects){
graphics.fillRect(object.position.x, object.position.y,object.width, object.height);
}
graphics.setColor(Color.BLUE);
graphics.fillRect(GameArea.square.position.x,GameArea.square.position.y,GameArea.square.width, GameArea.square.height);
for(GameObject object2 : GameArea.objects){
graphics.fillRect(object2.position.x, object2.position.y,object2.width, object.height);
}
}
它在一个名为 FieldPanel 的类中。我从 MainGame 类中这样调用它:
Timer t = new Timer(50, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//The following line does not work:
fieldPanel.paintComponent(Graphics g);
}
});
但是,行不通的线路给我带来了问题。如何创建新的图形对象以传递给其他类的方法?而且,当我创建它时,它应该具有哪些属性等?我不完全确定 Graphics 类的作用,解释会有所帮助。
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。