【发布时间】:2016-03-14 19:33:10
【问题描述】:
我正在为学校做作业。我必须在随机位置创建 30 个随机颜色的游戏对象。我必须使用 2 个类,一个 GameObject 类,其中包含 GameObject 数据、x 和 y 坐标和颜色,以及移动和绘制方法......以及一个主要的 MovingSquaresApplication,它将 GameObjects 放入数组并调用paint() 和move() 方法...当前程序编译、运行、绘制60 个正方形(paint() 和repaint())但没有动画。我看过很多不同的帖子,但仍然无法正确。任何帮助都会很棒。这是代码.....
*编辑新代码
import java.awt.*;
import javax.swing.*;
public class MovingSquaresApplication extends JFrame implements Runnable {
//member data
private static final Dimension WindowSize = new Dimension(600,600);
private static final int NUMGAMEOBJECTS = 30;
private GameObject[] gameObjectsArray = new GameObject[NUMGAMEOBJECTS];
private int i,j;
//constructor
public MovingSquaresApplication(){
this.setTitle("MovingSquaresApplication");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window, centered on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
for (i=0; i<gameObjectsArray.length; i++){ //fills array with GameObjects
GameObject NewSquare = new GameObject();
gameObjectsArray[i] = NewSquare;
}
Thread t = new Thread(this); //creates and stars a thread
t.start();
}
//threads entry point
public void run(){
while (true){
try {
Thread.sleep(20);
for (j=0; j<gameObjectsArray.length; j++){
gameObjectsArray[j].move();
repaint();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//applications paint method
public void paint (Graphics g){
super.paintComponents(g);
for (i=0; i<gameObjectsArray.length; i++){
gameObjectsArray[i].paint(g);
}
}
//applications entry point
public static void main(String[] args){
MovingSquaresApplication w = new MovingSquaresApplication();
}
}
还有GameObject 类
import java.awt.*;
public class GameObject {
//member data
private int x,y,xvel=2,yvel=2;
private Color c;
public GameObject(){
x = (int) (Math.random( )*600);
y = (int) (Math.random( )*600);
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
c = new Color (R, G, B);
}
//public interface
public void move(){
x += xvel;
y += yvel;
if(x<10)
{
xvel = 2;
}
else if(y<30)
{
yvel = 2;
}
else if(x>=560)
{
xvel = -2;
}
else if(y>=560)
{
yvel = -2;
}
}
public void paint(Graphics g){
g.setColor(c);
g.fillRect(x, y, 30, 30);
}
}
感谢大家的帮助,非常感谢
感谢帮助,我没有创建扩展JPanel的类,我只是简单地说
super.paintComponent(g);
在绘画方法中,不确定这是否是好的做法,但它有效....也在旁注中我以前从未见过这个
for (GameObject gameObject : gameObjectArray)
与我使用的循环相比,这到底有什么作用?
【问题讨论】:
-
Im doing an assignment for school.- 那么你应该使用正确的变量名。变量名不应以大写字符开头。 -
您没有在更新循环中更新您的游戏对象。你只是创建了一个新的游戏对象,然后循环遍历你的游戏对象并移动这个新的游戏对象,但它永远不会显示,然后被销毁。只需通过它们更新循环中的每个游戏对象,您就应该接近实现目标了。
-
then you should be using proper variable names我同意这一点。当人们告诉我他们的学校老师不关心命名约定时,我有点恼火。 -
对不起,我知道,但它们是给我们的变量名称,我打算更改它们。感谢您的帮助
-
不,不要在
paint方法中调用paintComponent,是的,为您的动画扩展 JPanel,而不是 JFrame,请不要尝试编辑我的答案。
标签: java multithreading swing animation