gpt4 book ai didi

java - 当我使用不同的变量时,为什么 java 复制而不是创建一个新的 Instant?

转载 作者:行者123 更新时间:2023-11-29 06:49:04 24 4
gpt4 key购买 nike

我写了一个相当简单的代码,但我遇到了一个让我困惑的错误。

所以我有一个类创建两个完全不同的变量并使用 new 关键字创建它们

Player playerLeft = new Player(5,150);
Player playerRight = new Player( 150,150);

玩家类:

import javax.swing.*;
import java.awt.*;

public class Player extends JComponent {

private int posY;
private int posX;

public Player(int x, int y) {
posX = x;
posY = y;
//repaint();
}

public float getMovementY() {
return movementY;
}

public void setMovementY(int movementY) {
this.movementY = movementY;
}

int movementY = 0;

public void paintComponent(Graphics g) {
Graphics2D _g2 = (Graphics2D) g;
Rectangle rect = new Rectangle(posX, posY, 20, 150);
_g2.fill(rect);
}

public void setLocation(int x, int y) {
posY = y;
posX = x;
repaint();
}

public void move() {
setLocation(posX, posY + movementY);
}
}

可能是我对 Java 不了解,但对我来说,当我尝试实例化 playerRight 时,它只会覆盖 player left 并仅 drawsOut playerRight。

完整代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class mainJFrame extends JFrame implements KeyListener {

int relativeTimeMillsec = 0;

Player playerLeft = new Player(5, 150);
Player playerRight = new Player(150, 150);

Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
relativeTimeMillsec++;
refreshTimeText(relativeTimeMillsec);
calcMovements();
}
};

//components
JLabel timeCounterLabel = new JLabel("Time: " + 0, SwingConstants.CENTER);

public mainJFrame() {
createComponents();

addKeyListener(this);
}

public void createComponents() {
this.setTitle("The title");
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

timer.scheduleAtFixedRate(task, 0, 10);

JButton testButton = new JButton("Label");
testButton.setSize(100, 25);
testButton.setLocation(this.getWidth() / 2 - testButton.getWidth() / 2, this.getHeight() / 2 - testButton.getHeight() / 2);

timeCounterLabel.setSize(200, 25);
timeCounterLabel.setLocation(this.getWidth() / 2 - timeCounterLabel.getWidth() / 2, 10);

//playerRight = new Player(this.getWidth()-45,this.getHeight()/2);

// this.add(testButton);
this.add(timeCounterLabel);
this.add(playerLeft);
this.add(playerRight);
}

public void paintComponent(Graphics g) {
{
super.repaint();
}

}

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_S) {
playerLeft.movementY = +2;
} else if (e.getKeyCode() == KeyEvent.VK_W) {
playerLeft.movementY = -2;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
playerRight.movementY = +2;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
playerRight.movementY = -2;
}
}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}

private double calcRealRelativeTime(int _relTime) {
return relativeTimeMillsec / (double) 100;

}

private void refreshTimeText(int _relTime) {
timeCounterLabel.setText("Time: " + Math.round(calcRealRelativeTime(_relTime)));
}

private void calcMovements() {
playerLeft.move();
playerRight.move();

}
}

最佳答案

了解 JFrame 的 contentPane(包含其组件的容器)默认使用 BorderLayout,此代码:

    this.add(timeCounterLabel);
this.add(playerLeft);
this.add(playerRight);

正在将所有组件添加到相同的默认 BorderLayout.CENTER 位置,这意味着添加的任何组件都将替换之前添加的组件。

但更重要的是,您的问题是一个常见问题,源于您让 Player 类从 GUI 组件扩展而来。不要这样做,因为那样的话您将很难绘制多个 Player 对象并让它们轻松交互(正如您所发现的)。相反,让 Player 成为一个逻辑(非组件)类,并且只有 一个 类扩展 JPanel 并完成所有绘图。这个类可以保存 Player 对象,可能保存在一个集合中,例如 ArrayList<Player>。 ,然后在其 paintComponent 方法中遍历集合。

其他问题:

  • 不要使用 java.util.Timerjava.util.TimerTask用于 Swing 动画,因为这些类不遵循 Swing 线程规则。改用 javax.swing.Timer .
  • 学习和使用Java naming conventions .变量名应全部以小写字母开头,而类名应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码
  • 如果/当您重写绘画方法时,例如 paintComponent ,一定要在你的override中调用super的方法,通常在第一行,以免打断绘画链。另外,使用 @Override此方法和您认为您可能会覆盖的任何其他方法之前的注释,以便编译器捕获可能的错误。

例如(但不是完整的例子)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleAnimation extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final int TIMER_DELAY = 20;
private Player2 playerLeft = new Player2(5, 150, Color.RED);
private Player2 playerRight = new Player2(150, 150, Color.BLUE);

public SimpleAnimation() {
playerLeft.setySpeed(1);
playerRight.setySpeed(-1);
new Timer(TIMER_DELAY, new TimerListener()).start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
playerLeft.draw(g);
playerRight.draw(g);
}

@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) {
playerRight.move();
playerLeft.move();
repaint();
}
}

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

JFrame frame = new JFrame("SimpleAnimation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

class Player2 {
private static final int RECT_WIDTH = 20;
private static final int RECT_HEIGHT = 50;
private int x;
private int y;
private int ySpeed;
private Color color;

public Player2(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setySpeed(int ySpeed) {
this.ySpeed = ySpeed;
}

public int getySpeed() {
return ySpeed;
}

public void setLocation(int x, int y) {
setX(x);
setY(y);
}

public void move() {
setLocation(x, y + ySpeed);
}

public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, RECT_WIDTH, RECT_HEIGHT);
}
}

关于java - 当我使用不同的变量时,为什么 java 复制而不是创建一个新的 Instant?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667447/

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