所以我写了这段代码,但我不确定鼠标监听器的去向以及如何循环程序。 我试图实现一个粒子爆炸通过鼠标点击时的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();
}
}
Square类
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;
}
}
答案 0 :(得分:1)
Swing使用单个线程进行绘画和事件处理。阻止此线程执行的任何内容都将阻止事件调度线程表单处理重绘请求。
mouseClicked事件中的while
循环将阻止程序运行。
我建议您改用javax.swing.Timer。有关详细信息,请参阅Concurrency in Swing
答案 1 :(得分:0)
如果您需要y位置,则应使用.getY()
。
@Override
public void mouseClicked(MouseEvent me) {
while(x>0){
this.x=me.getX();
this.y=me.getY();
}
}