Object-Oriented Programming with Java Lecture Notes

3 April 2008 • Java Graphics


Outline

Kinking

Orientation

Repeated Kinking

Kinking Chains

Recursion!

Dragon Curves

Operation

Example

$ echo 100 400 100 100 400 400 100 400 | 
  java DCShow 15

viola!

Screen Real Estate

JFrame Example

JFrame Classes

frame class hirearchy

  • The JFrame class hierarchy is deep and spread out.

    • Finding information can involve much pointing and clicking through Javadocs.

  • Typical practice makes it deeper by customizing JFrames via inheritance.

    • On the other hand, custom JFrames are (should be) easier to use.

JFrame Structure

Drawing Surfaces

jpanel class hirearchy

  • A JPanel instance provides a drawing surface.

  • JPanel inherits almost all its features from ancestors.
    Around 130 methods from JComponent.
    Around 56 methods from Container.
    Around 149 methods from Component.

  • Fortunately, you only need to know about one or two methods to start.

Custom JPanels

JFrames And JPanels

On-Demand Graphics

The paintComponent() Method

Redraw Example

The Graphics Class

Graphical Items

Graphical Properties

Items and Properties

Concrete Graphics Classes

Summary

Main Example

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)

Custom Frame Example

class DragonFrame
extends JFrame

  DragonFrame(
    DragonCurve dc, int ht, int wd)

    setTitle("Order " + 
      dc.order() + " Dragon Curve")

    setSize(ht, wd)

    add(new DragonPanel(dc))

Custom Panel Example

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

References


This page last modified on 7 April 2008.

Creative
    Commons License