1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| package fr.sca.tests;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
/**
* Created by sinok on 28/02/15.
*/
public class AnimatedText extends JPanel {
private int period = 20;
private float movementByMillisecond = 0.15f;
private String text = "Hello World!";
private long lastExec = 0;
private Point2D.Float location = new Point2D.Float(150, 20);
private Timer timer = new Timer(period, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AnimatedText.this.repaint();
System.out.println("Timer tick");
}
});
public static void main(String[] args) {
JFrame f = new JFrame("Exemple");
final AnimatedText t = new AnimatedText();
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.startAnimation();
}
});
JButton stop = new JButton("Stop");
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.stopAnimation();
}
});
JButton reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.reset();
}
});
JPanel p = new JPanel();
p.add(start);
p.add(stop);
p.add(reset);
f.add(t, BorderLayout.CENTER);
f.add(p, BorderLayout.SOUTH);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
f.setVisible(true);
}
private Point2D.Float computePositionFromTime(Point2D.Float previousLocation, long millisecondsElapsed) {
float movement = millisecondsElapsed * movementByMillisecond;
Point2D.Float location = new Point2D.Float(previousLocation.x, previousLocation.y + movement);
return location;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (lastExec == 0) {
location = computePositionFromTime(location, 0);
} else {
location = computePositionFromTime(location, System.currentTimeMillis() - lastExec);
}
lastExec = System.currentTimeMillis();
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawString(text, location.x, location.y);
}
public void startAnimation() {
timer.start();
}
public void stopAnimation() {
timer.stop();
System.out.println("Stopping timer");
}
public void reset() {
location = new Point2D.Float(150, 20);
lastExec = 0;
}
} |
Partager