i'm trying create simple panel 2-dimensional ball bouncing , down. can't work because reason can't call repaint method more once second. design there object can given "an impulse" method move()
. everytime evaluateposition
method called, current position calculated through time has passed, velocity , acceleration. code panel is:
public class display extends jpanel { private movableobject object = new movableobject(new ellipse2d.double(5,5,50,50)); private static final int delay = 1000; public display(){ object.move(50,50); timer timer = new timer(delay, new actionlistener(){ @override public void actionperformed(actionevent e){ object.evaluateposition(); repaint(); } }); timer.start(); } } @override public void paintcomponent(graphics g){ super.paintcomponent(g); graphics2d g2 = (graphics2d)g; g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g.drawoval((int)object.getposition().getx(), (int)object.getposition.gety() (int)object.getshape().getwidth(), object.getshape().getheight()); }
this code works delay=1000 not delay=100 or delay=10 , on. read example code here on seem me did. why code not working?
edit (2016-01-30): since seems performance issue, here's code movableobject (i thought irrelevant , see why):
public class movableobject { // use shape instead of ellipse2d // objects of shape can created private ellipse2d.double shape; private point position; // vector own class. want have easy vector addition , // multiplication , magnitude methods private vector velocity = new vector(0, 0); private vector acceleration = new vector(0, 0); private date lastevaluation = new date(); public movableobject(ellipse2d.double objectshape){ shape = objectshape; } public void evaluateposition(){ date currenttime = new date(); long deltatins = (currenttime.gettime()-lastevaluation.gettime())/1000; // s = s_0 + v*t + 0.5*a*t^2 position = new point((int)position.getx()+ (int)(velocity.getx()*deltatins) + (int)(0.5*acceleration.getx()*deltatins*deltatins), (int)position.gety()+ (int)(velocity.gety()*deltatins) + (int)(0.5*acceleration.gety()*deltatins*deltatins)); lastevaluation = currenttime; } } public void move(vector vector){ velocity = velocity.add(vector); evaluateposition(); } public point getposition(){ return position; } public ellipse2d.double getshape(){ return shape; }
my move method not change position velocity. please notice changed shape object shape ellipse2d testing if code has performance issue because of additional code. if think more complex needs be: want add complexity movableobject can have shape of subclass of shape. i've seen lot of code seemed more complex me , rendered fast. i'd know what's wrong (besides fact it's bit complex rendering ellipse).
okay, simple example, based on out-of-context code snippet left doesn't seem have problems. has variable speed controlled simple slider...
import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.renderinghints; import java.awt.shape; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.geom.affinetransform; import java.awt.geom.ellipse2d; import java.awt.geom.generalpath; import java.awt.geom.pathiterator; import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jslider; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class display extends jpanel { public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new display()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } private movableobject object = new movableobject(new ellipse2d.double(5, 5, 50, 50)); private int delay = 40; private timer timer = new timer(40, new actionlistener() { @override public void actionperformed(actionevent e) { object.evaluateposition(getsize()); repaint(); } }); private jslider slider = new jslider(5, 1000); public display() { object.move(50, 50); slider = new jslider(5, 1000); slider.setsnaptoticks(true); slider.setmajortickspacing(10); slider.setminortickspacing(5); setlayout(new borderlayout()); add(slider, borderlayout.south); // designed put artificate delay between // change listener , time update takes place, intention // stop pausing "main" timer... timer delay = new timer(250, new actionlistener() { @override public void actionperformed(actionevent e) { if (timer != null) { timer.stop(); } timer.setdelay(slider.getvalue()); timer.start(); } }); slider.addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { delay.restart(); repaint(); } }); slider.setvalue(40); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g.create(); g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.draw(object.gettranslatedshape()); fontmetrics fm = g2.getfontmetrics(); string text = integer.tostring(slider.getvalue()); g2.drawstring(text, 0, fm.getascent()); g2.dispose(); } public class movableobject { private shape shape; private point location; private int xdelta, ydelta; public movableobject(shape shape) { this.shape = shape; location = shape.getbounds().getlocation(); random rnd = new random(); xdelta = rnd.nextint(8); ydelta = rnd.nextint(8); if (rnd.nextboolean()) { xdelta *= -1; } if (rnd.nextboolean()) { ydelta *= -1; } } public void move(int x, int y) { location.setlocation(x, y); } public void evaluateposition(dimension bounds) { int x = location.x + xdelta; int y = location.y + ydelta; if (x < 0) { x = 0; xdelta *= -1; } else if (x + shape.getbounds().width > bounds.width) { x = bounds.width - shape.getbounds().width; xdelta *= -1; } if (y < 0) { y = 0; ydelta *= -1; } else if (y + shape.getbounds().height > bounds.height) { y = bounds.height - shape.getbounds().height; ydelta *= -1; } location.setlocation(x, y); } public shape gettranslatedshape() { pathiterator pi = shape.getpathiterator(affinetransform.gettranslateinstance(location.x, location.y)); generalpath path = new generalpath(); path.append(pi, true); return path; } } }
you have at
- swing animation running extremely slow
- rotating multiple images causing flickering. java graphics2d
- java bouncing ball
for more examples...
Comments
Post a Comment