.
accepts an order-0 dragon curve,produces an order-n dragon curve,
and displays it.
dragon-curve
should take a single command-line argument n
giving the dragon-curve order.
$ echo 100 400 100 100 400 400 100 400 | java DCShow 15
JFrame
class is an abstraction of
visible screen surface.
public static void main(String args[]) JFrame frame = new JFrame() frame.setSize(200, 200) frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) frame.setVisible(true)
|
|
|
|
JComponent
paintComponent()
method.
class DragonCurvePanel extends JPanel DragonCurvePanel() { ... } @Override void paintComponent(Graphics g) { ... }
JFrame frame = new JFrame() Container cp = frame.getContentPane() DragonCurvePanel p = new DragonCurvePanel() cp.add(p)
or, as Horstmann and Cornell point out
JFrame frame = new JFrame() frame.add(new DragonCurvePanel())
paintComponent()
method abstracts the notion of drawing on
demand.
paintComponent()
MethodpaintComponent()
method whenever the panel's graphics should be drawn or
redrawn.
@Override void paintComponent(Graphics g) { super.paintComponent(g) // the magic happens here. }
JPanel.paintComponent()
redraws the panel background at the
specified color.
DragonCuvePanel.paintComponent()
should make sure that gets
done.
Graphics
ClassGraphics
class abstracts the ability to
scribble on a Component.
arcs, images, lines and polylines, ovals, rectangles and polygons, strings (text).
drawString(String str, int x, int y) drawRect(int x, int y, int w, int h) fillPolygon(int[] x, int[] y, int n)
color, clipping region, font, and origin translation
setColor(Color c) getColor() setFont(Font f) getFont()
g.setColor(Color.BLACK) g.drawRect(0, 0, 10, 10)
Graphics2D
.
class DCShow public static void main(String args[]) int order = doArgs(args) DragonCurve dc = new DragonCurve(readPoints()) while (--order > -1) dc = new DragonCurve(dc) displayDragonCurve(dc) private static void displayDragonCurve(DragonCurve dc) DragonFrame frame = new DragonFrame(dc, 500, 500) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.setVisible(true)
class DragonFrame extends JFrame DragonFrame( DragonCurve dc, int ht, int wd) setTitle("Order " + dc.order() + " Dragon Curve") setSize(ht, wd) add(new DragonPanel(dc))
class DragonPanel extends JPanel public DragonPanel(DragonCurve dc) super() dragonCurve = dc public void paintComponent(Graphics g) super.paintComponent(g) final Graphics2D g2 = (Graphics2D) g final Point2D points [] = dragonCurve.points() for (int i = 0; i < points.length - 1; i++) g2.draw(new Line2D.Float(points[i], points[i + 1])) private final DragonCurve dragonCurve