所以我写了这段代码,但我不确定鼠标监听器在哪里以及如何循环程序。我试图在单击鼠标时通过鼠标的 x 和 y 坐标实现粒子爆炸。我也想循环它直到程序关闭,但我无法弄清楚。到目前为止,我得到的只是主类(框架)的一次爆炸
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class AnimationOfExplosingSquares extends JFrame implements MouseListener{
private int x;
private int y;
AnimationOfExplosingSquares(){
add(new ExplosingSquares(x,y));
setBackground(Color.BLACK);
}
public static void main (String[] args){
AnimationOfExplosingSquares frame = new AnimationOfExplosingSquares();
frame.setTitle("Squares");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 800);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent me) {
while(x>0){
this.x=me.getX();
this.y=me.getX();
}
}
@Override
public void mousePressed(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
ExplosingSquares 类
import java.awt.Graphics;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ExplosingSquares extends JPanel implements ActionListener
{
private int x;
private int y;
private Timer timer = new Timer(20, this);
private ArrayList<Square> squares=new ArrayList<Square>();
ExplosingSquares(int x,int y){
this.x=x;
this.y=y;
for(int i=0; i<100;i++){
Square squ = new Square(x,y);
squares.add(squ);
}
timer.start();
}
public void paintComponent( Graphics g ){
super.paintComponent( g );
for( Square square : squares ) {
square.boom();
g.setColor( square.getColor() );
// update the square's location
g.fillRect( square.getXCoord(), square.getYCoord(),(int)square.getSize(), (int)square.getSize());
}
}
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
}
广场班
import java.awt.Color;
import java.util.Random;
class Square {
private int width;
private int height;
//direction of x cordinate
private Random random= new Random();
private int randomNumber=(random.nextInt(25)-12);
//direction of y cordinate
private Random rand= new Random();
private int rando=(rand.nextInt(25)-12);
private int x;
private int y;
private double size=50;
Color c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
Square(){
}
Square(int width, int height){
this.width=width;
this.height=height;
}
Square(int width, int height,Color c){
this.width=width;
this.height=height;
this.c=c;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
public void setSize(double size){
this.size=size;
}
public void setColor(Color c){
this.c=c;
}
public int getXCoord() {
return x+width;
}
public int getYCoord(){
return y+height;
}
public double getSize(){
return size;
}
public double getHeight(){
return size;
}
public Color getColor(){
return c;
}
public void boom(){
this.x+=rando;
this.y+=randomNumber;
this.size-=1;
}
}