java - how to implement timer the right way in JFrame -
i struggling find out how point in program change every second or 2 timer. have tried combinations, have been unsuccessful. believe there in actionlistener might have failed on.
arraylist<point> punkter = new arraylist<point>(); int = 0; int n = 0; public point[] point = null; private timer timer; random rg = new random(); public timer(){ this.settitle("draw"); this.setdefaultcloseoperation(jframe.exit_on_close); this.setsize(1010, 710); this.setlayout(null); this.setlocationrelativeto(null); point = new point[100]; this.setvisible(true); timer = new timer(500,this); timer.start(); } public void paint(graphics g){ super.paint(g); (int = 0; < punkter.size(); i++) { point = punkter.get(i); point b = punkter.get((i+1)%punkter.size()); g.filloval(a.x, a.y, 5, 5); g.drawline(a.x, a.y, b.x, b.y); } } @override public void actionperformed(actionevent e) { // todo auto-generated method stub for(int = 0;i < 100;i++){ point[i] = new point(rg.nextint(1000), rg.nextint(700)); punkter.add(point[i]); } } }
call repaint() on component when wish repaint, otherwise paintcomponent (or paint) method may not called. possibly not directly related advice worth giving (and noted in comment trashgod): use component added jframe (like jpanel), , perform drawing within paintcomponent method of component (if so, should call repaint on component).
Comments
Post a Comment