java - Multi-threaded bouncing balls, issue displaying balls -


i'm trying use multithreading draw balls bounce around inside jframe. can coordinates of each ball update , print out, can't balls display. i'm not strong in graphics, , i'm not quite sure i'm missing. think need add each instance of ball panel have inside frame, when tried didn't make difference. have class used view jframe, i've omitted. missing here?

ball

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.util.random; import javax.swing.jpanel;  public class ball extends jpanel implements runnable  { jpanel pan; private static int radius = 10; private color color; private int xpos; private int ypos; private int dx; private int dy; dimension d;  public ball(jpanel p) {     random r = new random();     this.pan = p;     this.d = pan.getsize();     xpos = r.nextint(d.width-50)+25;     ypos = r.nextint(d.height-50)+25;     dx = r.nextint(3)+1;     dy = r.nextint(3)+1;     color = new color(r.nextint(255*255*255));     paintcomponent(pan.getgraphics()); }  public void move() {     xpos += dx;     ypos += dy;     if (xpos+radius <= 0 || xpos+radius >= d.width)         dx = -dx;     if (ypos+radius <= 0 || ypos+radius >= d.height)         dy = -dy; }  public void paintcomponent(graphics g) {     super.paintcomponent(g);     g.setcolor(color);     g.filloval(xpos-radius, ypos-radius, 2*radius, 2*radius);     g.dispose(); }  public void animate() {     paintcomponent(pan.getgraphics());     move();     //pan.validate();//this didn't     //pan.repaint();// work     try {         thread.sleep(40);     } catch (interruptedexception e) {} }  public void run() {     while(true)         animate(); } } 

balltracker

import java.util.arraylist;  public class balltracker { private arraylist<ball> balls;  public balltracker() {     balls = new arraylist<ball>(); }  public void addball(ball b) {     balls.add(b);     thread t = new thread(b);     t.start(); } } 

ballframe

import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel;  public class ballframe extends jframe { public static final int width = 500; public static final int height = 550; private balltracker tracker; private jpanel ballpanel; private jpanel buttonpanel;  public ballframe() {     super("ballframe");     tracker = new balltracker();      // set ball panel     ballpanel = new jpanel();     ballpanel.setsize(width, 500);      // listener add new ball tracker     class blistener implements actionlistener {         public void actionperformed( actionevent event ) {             ball b = new ball(ballpanel);             tracker.addball(b);          }     }      // set button panel     buttonpanel = new jpanel();     buttonpanel.setsize(width, 50);     jbutton addballbutton = new jbutton();     addballbutton.settext("add ball");     addballbutton.addactionlistener(new blistener());     buttonpanel.add(addballbutton);      // add panels frame     add(buttonpanel, borderlayout.south);     add(ballpanel, borderlayout.center);     setsize( width, height ); } } 

it seems ball extends jpanel , has paint method, ballpanel need painting , ball doesn't seem need panel @ all.

set ball panel ballpanel = new jpanel(); ballpanel.setsize(width, 500); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -