我确实知道另一个主题与我的问题here
非常相似与here不同,因为向面板添加球的方法(不是内容窗格)
然而,该主题所涉及的问题并没有真正回答我的问题并解释为什么我遇到的问题正在发生
我试图创建一个非常简单的程序,可以创建多个球,它们可以独立弹跳并且不会互相交互,但是每当我成功创建一个球时,下一个尝试的实现都会被覆盖而前一个球没有显示
我认为可能的原因是布局以及如何在默认的中心布局上添加多个球
我看到了一些使用列表和/或空布局的建议和示例,并尝试了其中一些没有工作
该计划包括两个类别#34; GUI"用于创建窗口并添加球和" Ball"这个球就是球本身
这是GUI类
import javax.swing.JFrame;
import java.awt.*;
public class Gui extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Ball());
frame.add(new Ball(200,200,8,8,25,25)); //This overwrites above line
frame.setSize(500,400);
frame.setVisible(true);
}
}
这是球类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class Ball extends JPanel implements ActionListener{
Timer timer = new Timer(10,this);
double x_coord = 250, y_coord = 250, x_vector = 5, y_vector = 5, x_size = 50, y_size = 50;
public Ball(double x_c, double y_c, double x_v, double y_v, double x_s, double y_s){
x_coord = x_c;
y_coord = y_c;
x_vector = x_v;
y_vector = y_v;
x_size = x_s;
y_size = y_s;
timer.start();
setFocusable(true);
}
public Ball(){
timer.start();
setFocusable(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = g2d.getClipBounds();
rect.x = (int) x_coord;
rect.y = (int) y_coord;
g2d.fill(new Ellipse2D.Double(rect.getX(), rect.getY(), x_size, y_size));
if(rect.getY() < 0 || rect.getY() > (getHeight()-y_size)){
y_vector *= -1;
}else if(rect.getX() < 0 || rect.getX() > (getWidth()-x_size)){
x_vector *= -1;
}
}
public void actionPerformed(ActionEvent e) {
repaint();
x_coord += x_vector;
y_coord += y_vector;
}
}
我非常感谢一个很好解释的答案,因为我对JPanel和JFrame仍然非常环保,谢谢!
答案 0 :(得分:-1)
我尝试了评论建议的控制器概念以及&#34;画家&#34;概念,只是整体清理代码
当击中角落时,还将球固定在窗口中。
虽然我会将案件结案,但我并不认为在考虑实施不同类型的对象/绘图时,强迫自己拥有一个JPanel创建者(Ball Creator)是一个非常好的主意
Gui Class
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(500,400);
System.out.println();
BallCreator creator = new BallCreator();
frame.add(creator);
frame.setVisible(true);
}
}
新球创作者类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BallCreator extends JPanel implements ActionListener{
Timer timer = new Timer(10, this);
Ball2 ball = new Ball2();
Ball2 ball2 = new Ball2(100,100,-7,7,40,40);
Ball2 ball3 = new Ball2(50,50,6,-6,60,60);
public BallCreator(){
timer.start();
}
public void paint(Graphics g){
super.paint(g);
ball.paint(g);
ball2.paint(g);
ball3.paint(g);
}
public void actionPerformed(ActionEvent e){
repaint();
}
}
球类
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class Ball2 extends JPanel{
double x_coord = 250, y_coord = 250, x_vector = 5, y_vector = 5, x_size = 50, y_size = 50;
public Ball2(double x_c, double y_c, double x_v, double y_v, double x_s, double y_s){
x_coord = x_c;
y_coord = y_c;
x_vector = x_v;
y_vector = y_v;
x_size = x_s;
y_size = y_s;
}
public Ball2(){
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = g2d.getClipBounds();
rect.x = (int) x_coord;
rect.y = (int) y_coord;
g2d.fill(new Ellipse2D.Double(rect.getX(), rect.getY(), x_size, y_size));
if (rect.getX() >= (484-x_size-3) && x_vector > 0){
x_vector *= -1;
}
if (rect.getX() <= 0 && x_vector < 0){
x_vector *= -1;
}
if (rect.getY() >= (361-y_size-4) && y_vector > 0){
y_vector *= -1;
}
if (rect.getY() <= 0 && y_vector < 0){
y_vector *= -1;
}
y_coord += y_vector;
x_coord += x_vector;
}
}