java - How to repaint with timer? -
this question has answer here:
- timing swing animation 3 answers
- spring behavior simulation 1 answer
- swing timers , animations in jpanel 2 answers
- java swing timer , animation: how put together 1 answer
- basic java bullet animation using timer 1 answer
i want provide projectile motion timer. first of create timer , change location of x , y timer. when check x , y points updated can not repaint new x , y. how repaint panel , provide projectile motion? in advance. here code.
import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; public class deneme extends jpanel { double positiony = 100; double positionx = 100; double velocityy = 20; double velocityx = 20; double acceleration = -10; timer timer; public deneme() { timer = new timer(100, new mytimerlistener()); } private class mytimerlistener implements actionlistener { @override public void actionperformed(actionevent e) { update(0.09); repaint(); } } void update(double dt) { positionx += velocityx * dt; positiony += velocityy * dt + acceleration * 0.5 * dt * dt; velocityy += acceleration * dt; system.out.println(positionx + " " + positiony); } @override public void paint(graphics g) { super.paint(g); g.drawoval((int) positionx, (int) positiony, 15, 15); } public double positiony() { return positiony; } public double velocityy() { return velocityy; } public double positionx() { return positionx; } public double velocityx() { return velocityx; } public static void main(string[] args) { new deneme().timer.start(); jframe frame = new jframe("basketball"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(1500, 1000); frame.add(new deneme()); frame.setlocationrelativeto(null); frame.setresizable(false); frame.setvisible(true); } }
Comments
Post a Comment