1 package nl.tudelft.simulation.examples.dsol.animation;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.image.ImageObserver;
7 import java.rmi.RemoteException;
8
9 import javax.naming.NamingException;
10
11 import nl.tudelft.simulation.dsol.animation.SimRenderable2d;
12 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
13
14 /**
15 * The Animation of a Ball.
16 * <p>
17 * Copyright (c) 2003-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
19 * project is distributed under a three-clause BSD-style license, which can be found at
20 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
21 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
22 * </p>
23 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter Jacobs </a>
24 * @since 1.4
25 */
26 public class BallAnimation extends SimRenderable2d<Ball>
27 {
28 /** */
29 private static final long serialVersionUID = 1L;
30
31 /**
32 * the color of the ballAnimation.
33 */
34 private Color color = Color.ORANGE;
35
36 /**
37 * constructs a new BallAnimation.
38 * @param source Locatable; the source
39 * @param simulator SimulatorInterface<Double>; the simulator
40 * @throws NamingException on registration error
41 * @throws RemoteException on remote animation error
42 */
43 public BallAnimation(final Ball source, final SimulatorInterface<Double> simulator)
44 throws RemoteException, NamingException
45 {
46 super(source, simulator);
47 // even numbered balls are vertically scaled; odd numbered balls not. Balls 6-10 are twice as small.
48 int nr = Integer.parseInt(source.toString());
49 setScaleObject(nr > 5);
50 setScaleY(nr % 2 == 1);
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public void paint(final Graphics2D graphics, final ImageObserver observer)
56 {
57 graphics.setColor(this.color);
58 graphics.fillOval(-(int) Ball.RADIUS, -(int) Ball.RADIUS, (int) (Ball.RADIUS * 2.0), (int) (Ball.RADIUS * 2.0));
59 graphics.setFont(graphics.getFont().deriveFont(Font.BOLD));
60 graphics.setColor(Color.GRAY);
61 graphics.drawString(getSource().toString(), (float) (Ball.RADIUS * -1.0), (float) (Ball.RADIUS * 1.0));
62 }
63
64 /**
65 * @return Returns the color.
66 */
67 public Color getColor()
68 {
69 return this.color;
70 }
71
72 /**
73 * @param color Color; The color to set.
74 */
75 public void setColor(final Color color)
76 {
77 this.color = color;
78 }
79 }