MCPcopy Create free account
hub / github.com/OpenSourcePhysics/osp / DrawableGroup

Class DrawableGroup

src/org/opensourcephysics/display/DrawableGroup.java:20–133  ·  view source on GitHub ↗

Draws a group of shapes. @author Wolfgang Christian @version 1.0

Source from the content-addressed store, hash-verified

18 * @version 1.0
19 */
20public class DrawableGroup implements Drawable {
21 protected double x = 0, y = 0, theta = 0;
22 protected ArrayList<Drawable> drawableList = new ArrayList<Drawable>(); // list of Drawable objects
23
24 /**
25 * Adds a drawable object to the drawable list.
26 * @param drawable
27 */
28 public void addDrawable(Drawable drawable) {
29 if((drawable!=null)&&!drawableList.contains(drawable)) {
30 drawableList.add(drawable);
31 }
32 }
33
34 AffineTransform trDG = new AffineTransform();
35
36 /**
37 * Draws the shapes in the drawable list.
38 *
39 * @param panel the drawing panel
40 * @param g the graphics context
41 */
42 @Override
43public void draw(DrawingPanel panel, Graphics g) {
44 int xpix = panel.xToPix(0);
45 int ypix = panel.yToPix(0);
46
47 Graphics2D g2 = (Graphics2D) g;
48
49 double xt = x*panel.getXPixPerUnit()*Math.cos(theta)+y*panel.getYPixPerUnit()*Math.sin(theta);
50 double yt = x*panel.getXPixPerUnit()*Math.sin(theta)-y*panel.getYPixPerUnit()*Math.cos(theta);
51 AffineTransform at = g2.getTransform();
52 trDG.setTransform(at);
53 trDG.rotate(-theta, xpix, ypix);
54 trDG.translate(xt, yt);
55
56
57 // was:
58// AffineTransform at = g2.getTransform();
59// at.concatenate(AffineTransform.getRotateInstance(-theta, xpix, ypix));
60// double xt = x*panel.getXPixPerUnit()*Math.cos(theta)+y*panel.getYPixPerUnit()*Math.sin(theta);
61// double yt = x*panel.getXPixPerUnit()*Math.sin(theta)-y*panel.getYPixPerUnit()*Math.cos(theta);
62// at.concatenate(AffineTransform.getTranslateInstance(xt, yt));
63
64 g2.setTransform(trDG);
65 for (int i = 0, n = drawableList.size(); i < n; i++) {
66 drawableList.get(i).draw(panel, g2);
67 }
68 g2.setTransform(at);
69 }
70
71 /**
72 * Sets the x and y coordinates.
73 *
74 * @param _x double
75 * @param _y double
76 */
77 public void setXY(double _x, double _y) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected