gpt4 book ai didi

带线程的 Java 动画

转载 作者:行者123 更新时间:2023-11-30 06:54:48 24 4
gpt4 key购买 nike

我正在为学校做作业。我必须在随机位置创建 30 个随机着色的游戏对象。我必须使用 2 个类,一个 GameObject 类,其中包含 GameObject 数据、x 和 y 坐标和颜色,以及移动和绘制方法...和一个main MovingSquaresApplication 将 GameObjects 放入数组并调用 paint()move() 方法...当前程序编译、运行,绘制 60 个方 block (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);

在 paint 方法中,不确定这是否是好的做法,但它确实有效......还有一个旁注,我以前没见过这个

for (GameObject gameObject : gameObjectArray)

与我使用的循环相比,它到底做了什么?

最佳答案

您需要在 JPanel 的 paintComponent 方法中进行绘制(我相信您已经阅读过,....所以我建议您这样做。您

  • 用 GameObjects 填充你的 GameObjectsArray,并且不是在任何绘画方法中,而是在 GUI 构造中这样做
  • 创建一个 JPanel 并覆盖其 paintComponent 方法,就像 Swing painting tutorials 一样告诉你去做
  • 在您的重写中调用 super 的 paintComponent 方法(再次按照教程将告诉您的那样做)
  • 在 paintComponent 方法中遍历您的 GameObjectsArray(但将其重命名为 gameObjectsArray,因为它是一个变量而不是一个类)
  • 在同一个 for 循环中调用每个 GameObject 的绘制方法。

编辑:检查你的这段代码:

GameObject MoveSquare = new GameObject();
for (y = 0; y < GameObjectsArray.length; y++) {
MoveSquare.move();
}

您正在做的是创建一个全新的 GameObject 对象 MoveSquare,并尝试在 for 循环内移动,同时您没有触及 gameObjectsArray 中保存的任何 GameObject .你看到你的错误了吗?


编辑2
此外,您还使用 y 变量作为数组索引,与您用来确定 GUI 的 y 轴边界的变量相同——不要这样做。使用完全独立的变量。

编辑4
在这里:

public void paint(Graphics g) {
for (y = 0; y < GameObjectsArray.length; y++) {
GameObject NewSquare = new GameObject();
if (GameObjectsArray[y] == null) {
GameObjectsArray[y] = NewSquare;
NewSquare.paint(g);
}
}
}

您在每次调用 paint 时创建新的 GameObject 变量,忽略数组中已经存在的任何变量(??)。绘画方法应为绘画和绘画而已。同样,在您的类构造函数中用新的 GameObject 项填充您的 GameObject 数组一次,而不是在绘画方法中。

你在这里做了很多猜测,把代码扔到墙上看看有什么能坚持下去,这对于创建程序来说并不是一个很好的启发式方法。而是在将代码提交给 IDE 之前在纸上规划每个步骤。


编辑5
GameObject move 方法中的 if 条件需要修复。但是一旦代码开始运行,您就会明白我的意思,因为您会看到所有游戏对象都在页面上运行。


编辑6

我不打算向您展示所有代码,但同样,您需要创建一个扩展 JPanel 的类,覆盖其 paintComponent 方法,该方法将非常简单,如下所示:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // do housekeeping painting

// note that I've renamed the array to gameObjectArray
for (GameObject gameObject : gameObjectArray) {
gameObject.paint(g); // this is all you need to call
}
}

同样,run 方法类似于:

@Override
public void run() {
while (true) {
try {
Thread.sleep(SLEEP_TIME); // constant for the sleep time
} catch (InterruptedException e) {
e.printStackTrace();
}

// again note that the array has been renamed
for (GameObject gameObject : gameObjectArray) {
gameObject.move(); // this is all that needs to be called here
}
repaint();
}
}

编辑下一个 :)
您正在创建两个 Thread 对象并同时启动它们——不要那样做。只有一个会做。

关于带线程的 Java 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com