Java 我的弹跳球不划水

Java 我的弹跳球不划水,java,swing,Java,Swing,当我点击框架时,让我的应用程序画一个球时,我遇到了一个问题。 我认为我的代码是正确的,我没有得到任何错误,但它仍然不工作。 我觉得问题在于MouseListener实现,应用程序没有正确处理MouseEvent import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class BouncingBallApp exte

当我点击框架时,让我的应用程序画一个球时,我遇到了一个问题。 我认为我的代码是正确的,我没有得到任何错误,但它仍然不工作。 我觉得问题在于MouseListener实现,应用程序没有正确处理MouseEvent

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class BouncingBallApp extends JFrame
{

public static void main(String[] args)
{
 Container container;
 BouncingBallApp bouncingBalls = new BouncingBallApp();
 bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 container = new Container();
 BouncingBallPanel BBP = new BouncingBallPanel();
 container.add(BBP);
 bouncingBalls.addMouseListener(BBP);
 //addMouseListener;
 bouncingBalls.setBackground( Color.WHITE );
 bouncingBalls.setSize(400,300);
 bouncingBalls.setVisible( true );



}//end of main method
}//end of BouncingBallApp



class BouncingBallPanel extends JPanel implements MouseListener, Runnable
{
private Ball[] ballArray = new Ball[20];
private int ballCount = 0;
public void run()
{

for(int i = 0; i<ballArray.length; i++){
if(ballArray[i] != null){
ballArray[i].move();}} 
repaint(); 
//delay(1);

}
public void mouseClicked(MouseEvent e)
{


ballArray[ballCount] = new Ball();
ballCount++;
if(ballCount == 1)
(new Thread(new BouncingBallPanel())).start();

}

//empty interface methods
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}

public void paintComponent(Graphics g)
{
super.paintComponent( g );
Graphics2D g2d = (Graphics2D) g;

for(int i = 0; i<ballArray.length; i++)
{
if(ballArray[i] != null){
g2d.setColor(ballArray[i].getColor());
g2d.fill(new Ellipse2D.Double(ballArray[i].getX(),ballArray[i].getY(),ballArray   
[i].getDiameter(),ballArray[i].getDiameter()));}
}//end of for loop

}

}//end of BouncingBallPanel 

class Ball
{
private double x;
private double y;
private double deltaX;
private double deltaY;
private double diameter;
private Color color;

Random random = new Random();

public Ball()
{
    x = random.nextInt(400);
y = random.nextInt(300);
deltaX = 2;
deltaY = 2;
diameter = 10;
color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
}//end of constructor

public double getX(){
return x;}

public double getY(){
return y;}

public double getDiameter(){
return diameter;}

public Color getColor(){
return color;}

public void move()
{
x += deltaX;
y += deltaY;

if (x < 0) {  
x = 0; 
deltaX = -deltaX;}

else if (x > 400) { 
x = 400;    
deltaX = -deltaX;}

if (y < 0) {     
y = 0;
deltaY = -deltaY;}

else if (y > 300) { 
y = 300;
deltaY = -deltaY;}


}//end of method move

}//end of ball
import java.awt.*;
导入java.awt.geom.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.*;
公共类BouncingBallApp扩展JFrame
{
公共静态void main(字符串[]args)
{
集装箱;
BouncingBallApp bouncingBalls=新的BouncingBallApp();
bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
容器=新容器();
BouncingBallPanel BBP=新的BouncingBallPanel();
容器。添加(BBP);
弹跳球。添加鼠标侦听器(BBP);
//addMouseListener;
弹跳球。挫折地面(颜色。白色);
弹跳球。设置尺寸(400300);
反弹球。设置为可见(真);
}//主方法结束
}//弹跳巴拉普结束
类BouncingBallPanel扩展JPanel实现MouseListener,Runnable
{
私人舞会[]巴拉雷=新舞会[20];
私人整数球数=0;
公开募捐
{
对于(inti=0;i300){
y=300;
deltaY=-deltaY;}
}//方法移动结束
}//球的末端
3件事

  • 容器无尺寸
  • 您没有添加
    容器
  • 没有更新图形的循环
  • 下面的代码正在运行

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class BouncingBallApp extends JFrame {
    
        public static void main(String[] args) {
            // Container container;
            BouncingBallApp bouncingBalls = new BouncingBallApp();
            bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // container = new Container();
            BouncingBallPanel BBP = new BouncingBallPanel();
            // container.add(BBP);
            bouncingBalls.addMouseListener(BBP);
            // addMouseListener;
            bouncingBalls.setBackground(Color.WHITE);
            bouncingBalls.setSize(400, 300);
    
            BBP.setSize(400, 300);
            BBP.setLayout(null);
            bouncingBalls.setContentPane(BBP);
    
            /*
             * JTextField jtf = new JTextField(); BBP.add(jtf); jtf.setSize(100,
             * 20);
             */
    
            bouncingBalls.setVisible(true);
    
        }// end of main method
    }// end of BouncingBallApp
    
    class BouncingBallPanel extends JPanel implements MouseListener {
        private Ball[] ballArray = new Ball[20];
        private int ballCount = 0;
    
        public void mouseClicked(MouseEvent e) {
    
            ballArray[ballCount] = new Ball();
            ballCount++;
            if (ballCount == 1) {
    
                final Runnable updateGraphics = new Runnable() {
                    public void run() {
                        for (int i = 0; i < ballArray.length; i++) {
                            if (ballArray[i] != null) {
                                ballArray[i].move();
                            }
                        }
                        repaint();
                    }
                };
    
                Runnable animation = new Runnable() {
                    public void run() {
                        while (true) {
                            try {
                                EventQueue.invokeLater(updateGraphics);
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                return;
                            }
                        }
                    }
                };
    
                new Thread(animation).start();
    
            }
    
        }
    
        // empty interface methods
        public void mouseExited(MouseEvent e) {
        }
    
        public void mouseReleased(MouseEvent e) {
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mousePressed(MouseEvent e) {
    
        }
    
        public void paintComponent(Graphics g) {
            // super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
    
            for (int i = 0; i < ballArray.length; i++) { if (ballArray[i] !=
                null) { System.out.println(" ball " + i + " , " +
                        ballArray[i].getColor() + " , " + ballArray[i].getX() + " , " +
                        ballArray[i].getY() + " , " + ballArray[i].getDiameter());
                g2d.setColor(ballArray[i].getColor());
                g2d.fillRect((int)ballArray[i].getX(), (int)ballArray[i].getY(),
                        (int)ballArray[i].getDiameter(), (int)ballArray[i].getDiameter()); }
            }// end of for loop
    
    
        }
    
    }// end of BouncingBallPanel
    
    class Ball {
        private double x;
        private double y;
        private double deltaX;
        private double deltaY;
        private double diameter;
        private Color color;
    
        Random random = new Random();
    
        public Ball() {
            x = random.nextInt(400);
            y = random.nextInt(300);
            deltaX = 2;
            deltaY = 2;
            diameter = 10;
            color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        }// end of constructor
    
        public double getX() {
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public double getDiameter() {
            return diameter;
        }
    
        public Color getColor() {
            return color;
        }
    
        public void move() {
            x += deltaX;
            y += deltaY;
    
            if (x < 0) {
                x = 0;
                deltaX = -deltaX;
            }
    
            else if (x > 400) {
                x = 400;
                deltaX = -deltaX;
            }
    
            if (y < 0) {
                y = 0;
                deltaY = -deltaY;
            }
    
            else if (y > 300) {
                y = 300;
                deltaY = -deltaY;
            }
    
        }// end of method move
    
    }// end of ball
    
    import java.awt.*;
    导入java.awt.geom.*;
    导入javax.swing.*;
    导入java.awt.event.*;
    导入java.util.*;
    公共类BouncingBallApp扩展JFrame{
    公共静态void main(字符串[]args){
    //集装箱;
    BouncingBallApp bouncingBalls=新的BouncingBallApp();
    bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //容器=新容器();
    BouncingBallPanel BBP=新的BouncingBallPanel();
    //容器。添加(BBP);
    弹跳球。添加鼠标侦听器(BBP);
    //addMouseListener;
    弹跳球。挫折地面(颜色。白色);
    弹跳球。设定尺寸(400300);
    BBP.设置尺寸(400300);
    BBP.setLayout(空);
    bouncingBalls.setContentPane(BBP);
    /*
    *JTextField jtf=new JTextField();BBP.add(jtf);jtf.setSize(100,
    * 20);
    */
    反弹球。设置为可见(真);
    }//主方法结束
    }//弹跳巴拉普结束
    类BouncingBallPanel扩展JPanel实现MouseListener{
    私人舞会[]巴拉雷=新舞会[20];
    私人整数球数=0;
    公共无效mouseClicked(MouseEvent e){
    巴拉雷[球数]=新球();
    ballCount++;
    如果(球数==1){
    final Runnable updateGraphics=new Runnable(){
    公开募捐{
    对于(int i=0;i400),则为else{
    x=400;
    deltaX=-deltaX;
    }
    if(y<0){
    y=0;
    deltaY=-deltaY;
    }
    如果(y>300),则为else{
    y=300;
    deltaY=-deltaY;
    }
    }//方法移动结束
    }//球的末端
    
    每单击一次鼠标,您似乎都在制作一个新的BouncingBallPanel。我想这不是你想要的。我如何调整鼠标听筒来增加一个球?mouseCliked方法应在每次单击时创建一个球。我以为它就是这么做的。是(新线程(新BouncingBallPanel()).start();问题出在哪里?你是不是一直想这么做