gpt4 book ai didi

java - 如何将对象添加到 JPanel 中

转载 作者:行者123 更新时间:2023-12-01 09:13:28 25 4
gpt4 key购买 nike

我正在尝试将球添加到 JPanel 中,但是,我不确定如何执行此操作,下面是我当前用于将球添加到 JPanel 中的代码。我正在尝试在屏幕上的随机位置添加多个球。第一个按预期显示,但更多内容不会出现在屏幕上。

球类;

public class Ball extends Component {

public static double ballX = rand.nextInt(500) + 40;
public static double ballY = rand.nextInt(300) + 10;
public static Ball[] balls = new Ball[20];

public Ball() {

}

public void draw(Graphics g2) {
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}

尝试将球添加到 JPanel 中的方法;

public class BallFrames extends JFrame {

/* creates static varibles privite for use withing class and public for
use out side of the class */
public static final JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
public static final JPanel ballPanel = new JPanel();
public static JButton button;
public static JPanel displayPanel = new JPanel(new BorderLayout());
private static int FRAME_WIDTH = 600;
private static int FRAME_HEIGHT = 600;

/**
* constructor called method to create the components
*/
public BallFrames() {
setSize(FRAME_WIDTH,FRAME_HEIGHT);
createComponents(); // creates all compononents
}

/*
* calls helper methods to create all of the componanats needed
*/
private void createComponents() {
ActionListener listener = new BallListener();
createPanel(); // creates a panel
createButtons(listener); // Creates a button
createSlider();
}

/**
* method to create panels
*/
public void createPanel() {
BallComponent component = new BallComponent(); // creates a new BallCompoenet
displayPanel.add(component, BorderLayout.CENTER);
displayPanel.add(controlPanel, BorderLayout.SOUTH); // adds thecontrol panel
add(displayPanel); // adds the dice to the JFrame
}

public static void addBall() throws InterruptedException {
Ball.balls[BallListener.balls] = new Ball();
System.out.println(BallListener.balls); // testing
ballPanel.add(Ball.balls[BallListener.balls]);
// ballPanel.add(ball);
Runnable r = new BallRunnable();
Thread t = new Thread(r);
t.start();
t.join();
ballPanel.repaint();
}

我如何绘制它们;

public class BallComponent extends JComponent {

/**
* calls paintComponenet to render images on the screen
*
* @param g
*/
@Override
public void paintComponent(Graphics g) {
int delay = 1000;
super.paintComponent(g); // call to super
Graphics2D g2 = (Graphics2D) g; // recover the graphic

TimerListener listener = new TimerListener();
Timer t = new Timer(delay, listener);

if (BallListener.gameOn == true) {
for(int i = 0; i < BallListener.balls; i++){
Ball.balls[i].draw(g2);
}

BallFrames.displayPanel.repaint();
}
}

ActionListener for the buttons;

public class BallListener implements ActionListener {

public static boolean gameOn = false;
public static int balls = 0;

/**
* gets the which button has been pressed and responds appriately
*
* @param event based on the event it will preform selected actions
*/
@Override
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();

switch (action) {
case "start":
gameOn = true;

{
try {
BallFrames.addBall();
} catch (InterruptedException ex) {
Logger.getLogger(BallListener.class.getName()).log(Level.SEVERE, null, ex);
}
balls++;
}
BallFrames.displayPanel.repaint();
break;
case "pause":

break;
}
}

球可运行;

public class BallRunnable implements Runnable {

private static Random rand = new Random();
public static boolean goLeft = true;
public static boolean goRight = false;
public static boolean goUp = false;
public static boolean goDown = false;
public static boolean diagonalUp = false;
public static boolean diagonalDown = false;
public static double speed = 0.2f;

public BallRunnable() {

}

public void run() {
boundsCheckY1();
boundsCheckX1();
boundsCheckY2();
boundsCheckX2();
}

public void boundsCheckX1() {
int temp;

if (Ball.ballX < 1) {
temp = rand.nextInt(5) + 1;

if(temp == 1) {
goRight = true;
goLeft = false;
goUp = false;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = false;
goDown = false;
goLeft = false;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = true;
goUp = false;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = true;
}
}
}

public void boundsCheckY1(){
int temp;

if (Ball.ballY < 1) {
temp = rand.nextInt(5) + 1;

if(temp == 1) {
goRight = false;
goLeft = false;
goUp = false;
goDown = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = true;
} else if (temp == 5) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = true;
}
}
}

public void boundsCheckX2 () {
int temp;

if (Ball.ballX > 559) {
temp = rand.nextInt(5) + 1;

if(temp == 1) {
goRight = false;
goLeft = true;
goUp = false;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = false;
goUp = false;
goDown = false;
goLeft = true;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = false;
goUp = false;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = true;
}
}
}

public void boundsCheckY2() {
int temp;

if (Ball.ballY > 470) {
temp = rand.nextInt(5) + 1;

if(temp == 1) {
goRight = false;
goLeft = false;
goUp = true;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = true;
diagonalDown = false;
}
}
}

}

查看器类;

public class BallViewer {

/**
* creates the main frame
*
* @param args
*/
public static void main(String[] args) throws InterruptedException {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
// new BallFrames().setVisible(true);
JFrame frame = new BallFrames(); // creates new JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Multi-Treaded Balls");// Sets the Title
frame.setVisible(true); // Makes the frame visible
frame.setResizable(false);
}
});
}

}

定时器监听器; 公共(public)类 TimerListener 实现 ActionListener {

@Override
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();

try {
movement();
} catch (InterruptedException ex) {
Logger.getLogger(TimerListener.class.getName()).log(Level.SEVERE, null, ex);
}

}
public void movement() throws InterruptedException {
if(goLeft == true) {
Ball.ballX -= speed;
}
if(goRight == true) {
Ball.ballX += speed;
}
if(goUp == true) {
Ball.ballY -= speed;
}
if(goDown == true) {
Ball.ballY += speed;
}
if(diagonalUp == true) {
Ball.ballY += speed / 2;
}
if(diagonalDown == true) {
Ball.ballY -= speed / 2;
}

}

}

我希望能够在给定时间在屏幕上添加多个球,这些球也像第一个球一样移动。

最佳答案

您对球类的定义的行为符合预期:

public class Ball extends Component {

public static double ballX = rand.nextInt(500) + 40;
public static double ballY = rand.nextInt(300) + 10;
public static Ball[] balls = new Ball[20];

public Ball() {

}

public void draw(Graphics g2) {
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}

所有静态字段在该类的所有实例之间共享。这就是static 的含义。所有都具有相同的ballXballY 坐标。

您正在寻找的是属于每个单独实例的非静态字段。每次制作新球时,您还必须初始化这些字段。 static 字段仅在类加载时初始化一次。我建议还有一些更改:

  • 如果您的 x 坐标和 y 坐标仅属于一个球,最好将它们设置为私有(private)
  • 扩展 JComponent 而不是 Component,因为您显然在这里使用了 swing。
  • 实现 paintComponent 而不是 draw。如果不出意外的话,它将使您的绘图代码需要更少的维护。

这是该类的一个版本,应该可以更好地工作:

public class Ball extends JComponent
{

private double ballX;
private double ballY;
public static Ball[] balls = new Ball[20];

public Ball()
{
ballX = rand.nextInt(500) + 40;
ballY = rand.nextInt(300) + 10;
}

public void paintComponent(Graphics g2)
{
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}

关于java - 如何将对象添加到 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769740/

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