java - Graphical Components -
java - Graphical Components -
i have done programme numerically solves set of differential equations describes how "arbitrary" illness move in isolated , constant population, programming assignment class took while ago. i've done extend add together graphical components can pause, reset , "play" simulation, components allows me alter constants in equations.
all exercise in programming find fun , exciting , want become better.
however, @ moment i'm stuck, want create simple form of animation of it. want visualize info number of infected, susceptibles , resistants in grid points. managed create grid , have thought of how place dots.
the problem have how draw dots programme working, can draw 1 dot in grid grid created, that's it. need able create dot @ specific place in grid, goes on until number of dots reaches finite number, 30. @ points want have first dot, 1 left, removed, dots shifted left , place new dot @ furthest right of grid, whole thing repeated.
i think able figure out help/hints paintcomponent()
method , whether need utilize repaint() method @ all, can't head around these reason. i've read through course of study literature on java, despite extensive sections explains of different graphical components not much methods, don't phone call paintcomponent()
method, done automatically.
if there unclear allow me know , i'll seek clarify it.
thanks in advance.
// fox mulder
i think able figure out help/hints paintcomponent()
method , whether need utilize repaint()
method @ all, can't head around these reason.
basically, create custom component extending jpanel
. when @override
paintcomponent()
method, implicitly called for you, never have phone call it. ever paint within method, gets drawn on surface. example
public class drawingpanel extends jpanel { @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.filloval(x, y, 10, 10); } }
when phone call repaint()
causing paintcomponent
method phone call implicitly. reply question, yes need phone call if want animate, need update kind of variable (like x , y) in paintcomponent() method, see alter in drawing.
you can see more @ performing custom painting
not handle actual animation, you'll want utilize javax.swing.timer
. can see more @ how utilize swing timers. here's basic construct
timer ( int delayinmillis, actionlistener listener )
where delayinmillis
time delay between ticks(in case animations) , actionlistener
listens "ticks". each tick, actionperformed
of actionlistener
called. there, can set code update variables utilize animation.
so illustration update x , y, in actionperformed
, phone call repaint()
public class drawingpanel extends jpanel { int x = 50; int y = 50; public drawingpanel() { timer timer = new timer(40, new actionlistener(){ @override public void actionperformed(actionevent e) { x += 5; y += 5; repaint(); } }); timer.start(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.filloval(x, y, 10, 10); } }
now simple example. in case want animate scatter plot. can have list of point
s , in actionperformed
can add together pull points that list , force them list drawn. have this
list<point> originalpoints; list<point> pointstodraw; ... @override protected void paintcomponent(grapchics g) { super.paintcomponent(g); (point point : pointstodraw) { g.filloval(point.x - 5, point.y - 5, 10, 10); } }
basically points in pointstodraw
list drawn. empty. , in timer, can add together list, until originalpoints
list exhausted. example.
list<point> originalpoints; list<point> pointstodraw; private int currentindex = 0; public drawingpanel(list<point> originalpoints) { this.originalpoints = originalpoints; pointstodraw = new arraylist<>(); timer timer = new timer(40, new actionlistener(){ @override public void actionperformed(actionevent e) { if (currentindex == originalpoints.size() - 1) { ((timer)e.getsource()).stop(); } else { pointstodraw.add(originalpoints.get(currentindex)); currentindex++; } repaint(); } }); timer.start(); }
so basicall maintain current index. when index reaches size of original list, stop timer. otherwise pop originalpoints
, force pointstodraw
. every point add together pointstodraw
, repaint()
called, , there point paintcomponent
draw circle with.
the end
udpate
i reread question, , think have have misunderstood it. if want points drawn, have 1 list. , draw points initially. each tick, remove first index, advance rest index, , add together new 1 end. though implementation of linkedlist
may want utilize that
java swing graphics plot real-time
Comments
Post a Comment