java - Nothing shows up to the screen when I call the repaint() method -
this common question, unique every situation.
here call .repaint()
in code:
timer = new timer(5, new actionlistener() { double t = 0; public void actionperformed(actionevent e) { arraylist<particle> fireworks = manager.getfireworks(t/1000); showfireworks(fireworks,t/1000); t = t + timer.getdelay(); (particle projectile : fireworks) { canvas = new fireworksdisplay(projectile); canvas.repaint(); add(canvas); } } });
it creating arraylist of firework particles (which works correctly, since tested printing (x,y) postions in console window , went fine). i want program paint little particles on screen every member of arraylist list
here drawpanel
private class fireworksdisplay extends jpanel { private color colour; private int xpos; private int ypos; private int size; public fireworksdisplay(particle particle) { super(); string string = particle.getcolour(); string = string.tolowercase(); switch(string) { case "blue": this.colour = color.blue; break; case "red": this.colour = color.red; break; case "green": this.colour = color.green; break; case "orange": this.colour = color.orange; break; case "cyan": this.colour = color.cyan; break; case "magenta": this.colour = color.magenta; break; case "yellow": this.colour = color.yellow; break; case "pink": this.colour = color.pink; break; default: this.colour = color.white; break; } double[] positions = particle.getposition(); this.xpos = (int) (getwidth()*positions[0]*(50) + tubeimage.getsize().width/2); this.ypos = (int) (getheight()*positions[1]*(50) + tubeimage.getsize().height - 100); this.size = particle.getrendersize(); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(colour); g.filloval(xpos, ypos, size, size); } }
for reason it's not painting screen though other times painted successful.
is there problem in posted code, or you think there's wrong elsewhere?
remember need call timer
's start()
method after instantiation.
also, you're adding new jpanel every time timer
fires. may want rewrite code paint new particle
s instead.
Comments
Post a Comment