我有一些像这样的代码:如何在Java中初始化图形?
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);
}
});
但是,不工作的线路导致我的问题。我如何创建一个新的图形对象传递给其他类的方法?而且,当我创建它时,它应该具有哪些属性等等?我不完全确定图形类的作用,解释会很有帮助。自动调用
1)为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 2)对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。 –