public class loc { public int x; public int y; public int radius; public loc(int x,int y,int r) { this.x=x; this.y=y; this.radius=r; } public int getx() { return this.x; } public void setx(int x) { this.x =x; } public int gety() { return this.y; } public void sety(int y) { this.y =y; } public int getradius() { return radius; } public void setradius(int radius) { this.radius = radius; } } import java.awt.color; import java.awt.graphics; import java.util.random; import javax.swing.jpanel; public class display extends jpanel { public loc [] ar=new loc[5]; private int dx=1; private int dy=1; public display() { for(int i=0; i<5; i++) this.ar[i]=new loc(new random().nextint(615)+25,new random().nextint(615)+25,new random().nextint(30)+25); } public void paintcomponent(graphics g) { g.setcolor(color.darkgray); // clear frame ... g.fillrect(0, 0, getwidth(), getheight()); g.setcolor(color.red); for(int i=0; i<ar.length; i++) g.filloval(ar[i].x, ar[i].y, ar[i].radius,ar[i].radius); update(); } public void update() { try { thread.sleep(5); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } check(); repaint(); } public void check() { for(int i=0; i<ar.length; i++) { if(ar[i].x<0) dx=1; if(ar[i].x>=this.getwidth()-45) dx=-1; if(ar[i].y<0) dy=1; if(ar[i].y>=this.getheight()-45) dy=-1; ar[i].x+=dx; ar[i].y+=dy; } } }
main class:
import javax.swing.jframe; public class main1 { public static void main(string[] args) { display b=new display(); jframe j=new jframe(); j.setsize(700, 700); j.add(b); j.setdefaultcloseoperation( jframe.exit_on_close ); j.setvisible(true); }
the loc class include : random (x), random(y) of ball , random (radius). class display class painting ball.
the code painting circles , moves them ,but circles dont move well. problem circles moves not them self; want circles move them self.
what can do?
Comments
Post a Comment