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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
| public class Dammier extends JPanel {
public enum AnimType {
NO, FIXED_TIME, FIXED_STEP
}
private static final int NBCASES = 10;
protected static final long DURATION = 500;
protected static final long STEPS = 300;
private static final long RATE = 5;
private volatile Rectangle2D shape;
// TODO : on devrait mettre ça dans un objet à part (si on veut pouvoir en bouger plusieurs, il vaudrait mieux)
private volatile int currentcase;
private volatile boolean move;
private final Timer timer;
private volatile TimerTask timerTask;
private AnimType animType;
private long duration;
private long step;
public Dammier() {
super(new GridLayout(0, NBCASES,0, 0), true);
animType = AnimType.NO;
duration=DURATION;
step=STEPS;
timer=new Timer();
setBorder(null);
for (int i = 0; i < NBCASES; i++) {
for (int j = 0; j < NBCASES; j++) {
JPanel panel = new JPanel();
if (j % 2 == (i % 2 == 0 ? 0 : 1)) {
panel.setBackground(Color.WHITE);
} else {
panel.setOpaque(false);
}
final int icase = i*NBCASES+j;
panel.setEnabled(true);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if ( e.getButton()== MouseEvent.BUTTON1 ) {
switch (animType) {
case NO:
moveShape(icase);
break;
case FIXED_TIME:
moveShapeFixedTime(icase, duration);
break;
case FIXED_STEP:
moveShapeFixedSpeed(icase, step);
break;
default:
break;
}
}
}
});
add(panel);
}
}
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if ( timerTask==null ) {
shape = getComponent(currentcase).getBounds().getBounds2D();
}
}
});
shape = new Rectangle2D.Double();
currentcase=0;
}
public void setAnimType(AnimType animType) {
this.animType=animType;
}
public AnimType getAnimType() {
return animType;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getDuration() {
return duration;
}
public void setStep(long step) {
this.step = step;
}
public long getStep() {
return step;
}
protected void moveShapeFixedTime(final int target, final long duration) {
if ( timerTask==null && target!=currentcase ) {
timerTask = new AdaptableTimerTask() {
private volatile FixedTimeMove fixedTimeMove = new FixedTimeMove(
getComponent(currentcase).getBounds().getBounds2D(),
getComponent(target).getBounds().getBounds2D(),
duration,
target);
protected void recompute() {
Rectangle2D currentBounds = fixedTimeMove.getCurrentBounds();
Rectangle2D bounds = getComponent(currentcase).getBounds().getBounds2D();
double sx = bounds.getWidth()/currentBounds.getWidth();
double sy = bounds.getHeight()/currentBounds.getHeight();
fixedTimeMove = new FixedTimeMove(
new Rectangle2D.Double(currentBounds.getCenterX()*sx-bounds.getWidth()/2,
currentBounds.getCenterY()*sy-bounds.getHeight()/2,
bounds.getWidth(),
bounds.getHeight()),
getComponent(target).getBounds().getBounds2D(),
fixedTimeMove.getRemainingTime(),
target);
}
@Override
public void run() {
fixedTimeMove.run();
}
};
timer.scheduleAtFixedRate(timerTask, 0, RATE);
}
}
protected void moveShapeFixedSpeed(final int target, final long step) {
if ( timerTask==null && target!=currentcase ) {
timerTask = new AdaptableTimerTask() {
private volatile FixedStepMove fixedStepMove = new FixedStepMove(
getComponent(currentcase).getBounds().getBounds2D(),
getComponent(target).getBounds().getBounds2D(),
step,
Double.MAX_VALUE,
target);
protected void recompute() {
Rectangle2D currentBounds = fixedStepMove.getCurrentBounds();
Rectangle2D bounds = getComponent(currentcase).getBounds().getBounds2D();
double sx = bounds.getWidth()/currentBounds.getWidth();
double sy = bounds.getHeight()/currentBounds.getHeight();
fixedStepMove = new FixedStepMove(
new Rectangle2D.Double(currentBounds.getCenterX()*sx-bounds.getWidth()/2,
currentBounds.getCenterY()*sy-bounds.getHeight()/2,
bounds.getWidth(),
bounds.getHeight()),
getComponent(target).getBounds().getBounds2D(),
step,
fixedStepMove.getRemainingDistance(),
target
);
}
@Override
public void run() {
fixedStepMove.run();
}
};
timer.scheduleAtFixedRate(timerTask, 0, RATE);
}
}
protected void moveShape(int dest) {
cancelPreviousMove();
currentcase=dest;
shape=getComponent(currentcase).getBounds().getBounds2D();
repaint();
}
private void cancelPreviousMove() {
if (timerTask!=null) {
timerTask.cancel();
timerTask=null;
}
}
@Override
protected void paintChildren(Graphics g) {
super.paintChildren(g);
g.setColor(Color.GREEN);
((Graphics2D)g).fill(shape);
}
private abstract class AdaptableTimerTask extends TimerTask implements ComponentListener {
public AdaptableTimerTask() {
addComponentListener(this);
}
@Override
public boolean cancel() {
if( super.cancel() ) {
removeComponentListener(this);
return true;
}
else {
return false;
}
}
@Override
public void componentResized(ComponentEvent e) {
recompute();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
protected abstract void recompute();
}
private class FixedTimeMove {
private final long startTime = System.currentTimeMillis();
private final Point2D.Double startPoint;
private final Point2D.Double endPoint;
private final long duration;
private final Rectangle2D targetBounds;
private final int target;
private final double dx;
private final double dy;
private Rectangle2D currentBounds;
private long remainingTime;
public FixedTimeMove(Rectangle2D startBounds, Rectangle2D endBounds, long duration, int target) {
this.duration = duration;
this.remainingTime = duration;
this.startPoint = new Point2D.Double(startBounds.getCenterX(), startBounds.getCenterY());
this.endPoint = new Point2D.Double(endBounds.getCenterX(), endBounds.getCenterY());
double timeStep = duration/RATE;
dx = computePixelStep(endPoint.getX()-startPoint.getX(), timeStep);
dy = computePixelStep(endPoint.getY()-startPoint.getY(), timeStep);
currentBounds = startBounds;
targetBounds = endBounds;
this.target = target;
}
public final Rectangle2D getCurrentBounds() {
return currentBounds;
}
public final long getRemainingTime() {
return remainingTime;
}
public void run() {
final long time = System.currentTimeMillis();
long elapsedTime = time-startTime;
if ( elapsedTime<=duration) {
remainingTime = duration - elapsedTime;
currentBounds= new Rectangle2D.Double(currentBounds.getX()+dx, currentBounds.getY()+dy, currentBounds.getWidth(), currentBounds.getHeight());
shape=currentBounds;
}
else {
remainingTime = 0;
cancelPreviousMove();
currentcase = target;
shape=targetBounds;
}
repaint();
}
private double computePixelStep(double distance, double step) {
distance /= step;
if ( distance==java.lang.Double.NaN ) {
distance=0;
}
return distance;
}
}
private class FixedStepMove {
private final Point2D.Double startPoint;
private final Point2D.Double endPoint;
private final double step;
private final Rectangle2D targetBounds;
private final int target;
private final double dx;
private final double dy;
private final double distance;
private volatile double remainingDistance;
private volatile Rectangle2D currentBounds;
public FixedStepMove(Rectangle2D startBounds, Rectangle2D endBounds, long step, double remainingDistance, int target) {
this.step = step/100.0; // 100ème pixels
this.startPoint = new Point2D.Double(startBounds.getCenterX(), startBounds.getCenterY());
this.endPoint = new Point2D.Double(endBounds.getCenterX(), endBounds.getCenterY());
distance = startPoint.distance(endPoint);
dx = computePixelStep(endPoint.getX()-startPoint.getX(), distance, this.step);
dy = computePixelStep(endPoint.getY()-startPoint.getY(), distance, this.step);
currentBounds = startBounds;
targetBounds = endBounds;
this.target = target;
this.remainingDistance = Math.min(remainingDistance, distance);
}
public Rectangle2D getCurrentBounds() {
return currentBounds;
}
public double getRemainingDistance() {
return remainingDistance;
}
private double computePixelStep(double dist, double distance, double step) {
return step * (dist/distance); // Thales
}
public void run() {
double nextX = currentBounds.getX()+dx;
double nextY = currentBounds.getY()+dy;
double distance = Point.distance(currentBounds.getX(), currentBounds.getY(), nextX, nextY);
remainingDistance -= distance;
if ( remainingDistance>0 ) {
currentBounds= new Rectangle2D.Double(nextX, nextY, currentBounds.getWidth(), currentBounds.getHeight());
shape=currentBounds;
}
else {
cancelPreviousMove();
currentcase = target;
shape=targetBounds;
}
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> run());
}
private static void run() {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel configPanel = new JPanel(new GridBagLayout());
JRadioButton buttonNoAnim = new JRadioButton("Pas d'animation");
configPanel.add(buttonNoAnim, new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH, new Insets(1,1,1,1), 0, 0));
JRadioButton buttonFixedTime = new JRadioButton("Temps fixe (ms)");
configPanel.add(buttonFixedTime, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH, new Insets(1,1,1,1), 0, 0));
JSpinner timeSpinner = new JSpinner(new SpinnerNumberModel(DURATION, 15, 300000, 100));
configPanel.add(timeSpinner, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.BOTH, new Insets(1,1,1,1), 0, 0));
JRadioButton buttonFixedStep = new JRadioButton("Pas fixe (100ème px)");
configPanel.add(buttonFixedStep, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH, new Insets(1,1,1,1), 0, 0));
JSpinner stepSpinner = new JSpinner(new SpinnerNumberModel(STEPS, 1, 300, 1));
configPanel.add(stepSpinner, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.BOTH, new Insets(1,1,1,1), 0, 0));
ButtonGroup group = new ButtonGroup();
group.add(buttonNoAnim);
group.add(buttonFixedTime);
group.add(buttonFixedStep);
Dammier dammier = new Dammier();
buttonNoAnim.addActionListener(e-> dammier.setAnimType(AnimType.NO));
buttonFixedTime.addActionListener(e-> dammier.setAnimType(AnimType.FIXED_TIME));
buttonFixedStep.addActionListener(e-> dammier.setAnimType(AnimType.FIXED_STEP));
timeSpinner.addChangeListener(e-> dammier.setDuration(((java.lang.Double)timeSpinner.getValue()).longValue()));
stepSpinner.addChangeListener(e-> dammier.setStep(((java.lang.Double)stepSpinner.getValue()).longValue()));
switch (dammier.getAnimType()) {
case NO:
buttonNoAnim.setSelected(true);
break;
case FIXED_TIME:
buttonFixedTime.setSelected(true);
break;
case FIXED_STEP:
buttonFixedStep.setSelected(true);
break;
default:
break;
}
mainPanel.add(dammier, BorderLayout.CENTER);
mainPanel.add(configPanel, BorderLayout.SOUTH);
frame.getContentPane().add(mainPanel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager