swing - java animation trouble getting the x and y cordinates of the mouse -
so wrote code im not sure of mouse listener goes , how loop program. im trying achieve particle explosion passing x , y coordinates of mouse when it's clicked. want loop till program closes cant figure out. far got 1 explosion 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 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 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; } }
swing uses single thread painting , event handling. blocks thread executing, prevent event dispatch thread form processing repaint requests.
the while loop in mouseclicked event stop program running.
i suggest use javax.swing.timer instead. see concurrency in swing more details
Comments
Post a Comment