-2

so i wrote this code but im not sure of where does the mouse listener goes and also how to loop the program. im trying to achieve a particle explosion passing the x and y coordinates of the mouse when it's clicked. also i want to loop it till the program closes but i cant figure it out. so far all i got is one explosion the main class (frame)

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.");
}

}

the ExplosingSquares class

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();
}
}

the Square class

  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;
       }
  }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rui Monroy
  • 1
  • 1
  • 1

2 Answers2

1

Swing uses a single thread for painting and event handling. Anything that blocks this thread from executing, will prevent the event dispatch thread form processing repaint requests.

The while loop in the mouseClicked event will stop your program from running.

I suggest you use a javax.swing.Timer instead. See Concurrency in Swing for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You should use .getY() if you need the y position.

@Override
public void mouseClicked(MouseEvent me) {
    while(x>0){
    this.x=me.getX();
    this.y=me.getY();
    }
}
pad
  • 239
  • 2
  • 8