gpt4 book ai didi

java - 图形对象不会随着 xPosition 的更新而更新

转载 作者:行者123 更新时间:2023-12-01 22:31:00 25 4
gpt4 key购买 nike

我正在尝试创建一个行星(蓝色圆圈),并在更新 x 位置时让它移动。这是主类。

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class Main extends Canvas implements Runnable{

public int width = 1400;
public int height = (width/16)* 9;

Dimension dim = new Dimension(width, height);

JFrame frame;

boolean running;

NewBody earth;

public Main(){
this.setPreferredSize(dim);
this.setBackground(Color.BLACK);
}

public void start(){
running = true;

Thread thread = new Thread(this, "display");
thread.start();
}

public void run(){
long startTime = System.currentTimeMillis();
double conv = Math.pow(10, 3);

while(running){
long now = System.currentTimeMillis();
if((now-startTime)/conv >= 1){
earth.incXPos();
startTime = now;
return;
}

update();
}
}

public void update(){
repaint();

}

public void stop(){
running = false;
}

public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLUE);
g2d.fillOval(earth.xPos,earth.yPos, earth.radius*2, earth.radius*2);

}

public static void main(String args[]){
Main main = new Main();

main.frame = new JFrame();
main.frame.setResizable(false);
main.frame.add(main);
main.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.frame.pack();

main.frame.setVisible(true);

main.earth = new NewBody(0, 0,0, 50);

main.start();
}
}

这是 NewBody 蓝图,我从中创建“地球”

public class NewBody {

Main main = new Main();

public int xOrigo = 1400/2;
public int yOrigo = 800/2;

public double mass;
public double velocity;
public int xPos;
public int yPos;
public double force;
public double vectorAngle;
public double fx;
public double fy;
public double acceleration;
public int radius;

public NewBody(double mass, int xPos, int yPos, int radius){
this.mass = mass;
this.xPos = xOrigo + xPos - radius;
this.yPos = yOrigo + yPos - radius;
this.radius = radius;
}

public void incXPos(){
this.xPos++;
}

问题是,当我运行程序时,蓝色圆圈只是停留在初始化时的相同位置。它只是闪烁得非常快,没有其他任何反应。我对编码很陌生,我似乎没有收到任何错误消息,因此我不知道如何继续。我已经被困在这个问题上几个小时了。

你有什么想法吗?

最佳答案

run() 方法中的 return; 语句会导致方法短路退出,从而在调用 incXPos() 后立即退出一次。这种情况甚至在调用 update() 之前就会发生,因此永远不会调用 repaint()

不过我会做一些不同的事情:

  • 我会在 JPanel 中绘图
  • 我会在其paintComponent方法中进行绘制。
  • 我会使用 Swing Timer 而不是 Thread 来执行动画循环。
  • 我一定要在我的覆盖内部调用 super 的 paintComponent(g)
<小时/>

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SimpleAnimation extends JPanel {
private static final int PREF_W = 1400;
private static final int PREF_H = (PREF_W * 9) / 16; // do int mult **first**
private static final int TIMER_DELAY = 13;
private NewBody earth = new NewBody(0, 0, 0, 50);

public SimpleAnimation() {
new Timer(TIMER_DELAY, new TimerListener()).start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;

// to allow for smooth graphics
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setColor(Color.BLUE);
g2d.fillOval(earth.xPos, earth.yPos, earth.radius * 2, earth.radius * 2);
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
earth.incXPos();
repaint();
}
}

private static void createAndShowGui() {
SimpleAnimation mainPanel = new SimpleAnimation();

JFrame frame = new JFrame("SimpleAnimation");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class NewBody {

// !! Main main = new Main();

public int xOrigo = 1400 / 2;
public int yOrigo = 800 / 2;

public double mass;
public double velocity;
public int xPos;
public int yPos;
public double force;
public double vectorAngle;
public double fx;
public double fy;
public double acceleration;
public int radius;

public NewBody(double mass, int xPos, int yPos, int radius) {
this.mass = mass;
this.xPos = xOrigo + xPos - radius;
this.yPos = yOrigo + yPos - radius;
this.radius = radius;
}

public void incXPos() {
this.xPos++;
}
}

关于java - 图形对象不会随着 xPosition 的更新而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27759083/

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