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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
public class ExempleAnimationSwingWorker {
private static final int BORDER = 50;
private static final int WIDTH = 300;
private static final int MOBILE_WIDTH = 50;
private static final long DELAY = 33;
private static final int SPEED = 5;
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel panel = new Panel(ExempleAnimationSwingWorker.WIDTH+ExempleAnimationSwingWorker.BORDER*2,
new Mobile(ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER,
ExempleAnimationSwingWorker.BORDER,
Dir.EAST,
Color.RED,
ExempleAnimationSwingWorker.SPEED),
new Mobile(ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER+ExempleAnimationSwingWorker.WIDTH-ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER,
Dir.SOUTH,
Color.GREEN,
ExempleAnimationSwingWorker.SPEED),
new Mobile(ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER+ExempleAnimationSwingWorker.WIDTH-ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER+ExempleAnimationSwingWorker.WIDTH-ExempleAnimationSwingWorker.MOBILE_WIDTH,
Dir.WEST,
Color.YELLOW,
ExempleAnimationSwingWorker.SPEED),
new Mobile(ExempleAnimationSwingWorker.MOBILE_WIDTH,
ExempleAnimationSwingWorker.BORDER,
ExempleAnimationSwingWorker.BORDER+ExempleAnimationSwingWorker.WIDTH-ExempleAnimationSwingWorker.MOBILE_WIDTH,
Dir.NORTH,
Color.CYAN,
ExempleAnimationSwingWorker.SPEED)
);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
Animation animation = new Animation(ExempleAnimationSwingWorker.DELAY,
panel,
new Rectangle(ExempleAnimationSwingWorker.BORDER,ExempleAnimationSwingWorker.BORDER,ExempleAnimationSwingWorker.WIDTH,ExempleAnimationSwingWorker.WIDTH));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
animation.cancel(true);
}
});
animation.execute();
}
public enum Dir {
EAST(1,0),
SOUTH(0,1),
WEST(-1,0),
NORTH(0,-1);
private final int dx;
private final int dy;
private Dir(int dx, int dy) {
this.dx=dx;
this.dy=dy;
}
public int getDx() {
return dx;
}
public int getDy() {
return dy;
}
public Dir next() {
int ordinal = this.ordinal()+1;
if ( ordinal>=values().length ) {
ordinal=0;
}
return values()[ordinal];
}
}
public static class Mobile {
private Color color;
private int width;
private int x;
private int y;
private Dir dir;
private int speed;
public Mobile(int width, int x, int y, Dir dir, Color color, int speed) {
this.color=color;
this.width=width;
this.x=x;
this.y=y;
this.dir=dir;
this.speed=speed;
}
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, width, width);
}
public void move(Rectangle bounds) {
x+=dir.getDx()*speed;
y+=dir.getDy()*speed;
switch(dir) {
case EAST:
int maxwidth = (int) bounds.getMaxX();
if ( x+width>maxwidth ) {
x=maxwidth-width;
dir = dir.next();
}
break;
case WEST:
int minwidth = (int) bounds.getMinX();
if ( x<minwidth ) {
x=minwidth;
dir = dir.next();
}
break;
case SOUTH:
int maxheight = (int) bounds.getMaxY();
if ( y+width>maxheight ) {
y=maxheight-width;
dir = dir.next();
}
break;
case NORTH:
int minheight = (int) bounds.getMinY();
if ( y<minheight ) {
y=minheight;
dir = dir.next();
}
break;
}
}
}
public static class Panel extends JPanel {
private List<Mobile> mobiles;
public Panel(int width, Mobile...mobiles) {
super(true);
setBackground(Color.BLACK);
setPreferredSize(new Dimension(width,width));
this.mobiles = Arrays.asList(mobiles);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(Mobile mobile : mobiles) {
mobile.draw(g);
}
}
public Collection<? extends Mobile> getMobiles() {
return mobiles;
}
}
public static class Animation extends SwingWorker<Object, Runnable> {
private final Panel panel;
private final Rectangle bounds;
private final long delai;
public Animation(long delai, Panel panel, Rectangle bounds) {
this.panel=panel;
this.bounds=bounds;
this.delai=delai;
}
@Override
protected Object doInBackground() throws Exception {
while(!isCancelled()) {
for(Mobile mobile:panel.getMobiles()) {
publish(()->mobile.move(bounds));
}
Thread.sleep(delai);
}
return null;
}
@Override
protected void process(List<Runnable> runnables) {
if ( !runnables.isEmpty() ) {
for(Runnable runnable : runnables) {
runnable.run();
}
panel.repaint();
}
}
}
} |
Partager