/* @(#)Methods.java 1.0 12 February 2005 --- Ball Sample Code */ import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.codec.*; import edu.neu.ccs.console.*; import edu.neu.ccs.filter.*; import edu.neu.ccs.jpf.*; import edu.neu.ccs.parser.*; import edu.neu.ccs.pedagogy.*; import edu.neu.ccs.quick.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import java.util.*; import java.math.*; import java.beans.*; import java.lang.reflect.*; import java.net.*; import java.util.regex.*; // import java.text.*; public class Methods extends JPF { public static void main(String[] args) { new Methods(); } // window dimensions int width = window.getWidth(); int height = window.getHeight(); // direct transform to flip coordinates Point2D flip(Point2D point) { if (point == null) point = new Point2D.Double(0, 0); double x = point.getX(); double y = point.getY(); return new Point2D.Double(x, height - y); } // paint a paintable at x, y // without needing a mutatable wrapper void paintAt (Graphics g, Paintable p, double x, double y) { if ((g == null) || (p == null)) return; Graphics2D h = (Graphics2D) g; h.translate( x, y); p.paint(h); h.translate(-x, -y); } // paint a paintable at a point // without needing a mutatable wrapper void paintAt (Graphics g, Paintable p, Point2D point) { if (point == null) return; paintAt(g, p, point.getX(), point.getY()); } // flip the point and then paint the paintable void paintWithFlipAt (Graphics g, Paintable p, double x, double y) { Point2D point = new Point2D.Double(x, y); paintWithFlipAt(g, p, point); } // flip the point and then paint the paintable void paintWithFlipAt (Graphics g, Paintable p, Point2D point) { if (point == null) return; paintAt(g, p, flip(point)); } // ball object represented as paintable circle int size = 20; // position the circle so its base is on the // line y = 0 in the natural coordinates of // the graphics window Ellipse2D.Double circle = new Ellipse2D.Double (0, -size, size, size); Color shapecolor = Colors.red; ShapePaintable sp = new ShapePaintable (circle, PaintMode.FILL, shapecolor); // text sample String sample = "Graphics Sample"; Font font = new Font("serif", Font.PLAIN, 48); Color textcolor = Colors.blue; // position text so its center baseline is at // (0, 0) TextAnchor.Locator anchor = TextAnchor.CENTER_BASELINE; TextPaintable tp = new TextPaintable (sample, font, textcolor, null, anchor); // line sample // position line so its midpoint is at (0, 0) int halfwidth = width / 2; Line2D line = new Line2D.Double(-halfwidth, 0, width, 0); Color lineColor = Colors.cyan; ShapePaintable lp = new ShapePaintable (line, PaintMode.DRAW, null, lineColor); public void Ball() { window.clearPanel(); Graphics2D g = window.getBufferGraphics(); // set ball displacement // as 40 in x-direction // and 20 in y-direction int dx = 40; int dy = 20; // paint ball 6 times for (int i = 0; i <= 5; i++) paintWithFlipAt(g, sp, i*dx, i*dy); // locate text and line at // 1/2 of width in x-direction // 3/4 of height in y-direction int x = width / 2; int y = (height * 3) / 4; paintWithFlipAt(g, tp, x, y); paintWithFlipAt(g, lp, x, y); window.repaint(); } }