- Forward


Basics of Computer Graphics
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Rendering Engines
Back SMYC Forward
  • Purpose:
    • Determine how to present graphics primitives on a visual output device
  • In Java:
    • The rendering engine is a Graphics2D object
Modeling Color
Back SMYC Forward

The (Linear) Color Cube

rgb-cube
Modeling Color (cont.)
Back SMYC Forward
  • The RGB Model:
    • Begin with black then add red, green, and or blue
    • An additive model
    • Often used for displays/monitors
  • The CMYK Model:
    • Begin with white then remove cyan, magenta, and/or yellow
    • A subtractive model
    • Often used for printing
Coordinate Systems
Back SMYC Forward
  • Coordinates:
    • Quantities (linear and/or angular) that designate the position of a point in relation to a given reference frame
  • In Java:
    • coordinates
Rendering with a Graphics2D Object
Back SMYC Forward
  • Geometric Shapes (see the Shape interface):
    • draw(Shape s)
    • fill(Shape s)
  • Strings/Text:
    • drawString(String s, float x, float y)
  • Images:
    • drawImage(Image i, int x, int y, null)
Classes that Implement the Shape Interface
Back SMYC Forward
  • 0-Dimensional:
    • Points (Point2D )
  • 1-Dimensional:
    • Lines (Line2D )
    • Curves (CubicCurve2D and QuadCurve2D )
  • 2-Dimensional:
    • Rectangles(Rectangle2D )
    • Polygons (GeneralPath )
    • Ellipses (Ellipse2D )
Stroking a Shape
Back SMYC Forward
  • Stroke:
    • The line "style" (see the Stroke interface)
    • Set using the setStroke method in Graphics2D
  • Color:
    • The line color (see the Color class)
    • Set using the setColor method in Graphics2D
Stroking a Shape (cont.)
Back SMYC Forward

Joins and Caps

stroke_caps

stroke_joins

Filling a Shape
Back SMYC Forward
  • Paint:
    • The fill "style" and color (see the Paint interface)
    • Set using the setPaint method in Graphics2D
  • Types:
    • Color
    • GradientPaint
    • TexturePaint
Rendering Text/Strings
Back SMYC Forward
  • Font:
    • The "glyphs" used for each character
    • Set using the setFont method in Graphics2D
  • Properties:
    • Name
    • Size (in points)
    • Style (plain, italic, bold)
Rendering Text/Strings (cont.)
Back SMYC Forward

Line Metrics

fontmetrics

The x and y passed to drawString(String s, float x, float y) are the left end of the baseline.

Rendering Text/Strings (cont.)
Back SMYC Forward
  • FontRenderContext
    • Keeps information about fonts
  • LineMetrics
    • Keeps information about font and line heights
Rendering Images
Back SMYC Forward
  • BufferedImage Class:
    • Extends the abstract Image class
    • Objects can be read from a file using the static read() method in the ImageIO class
  • The drawImage(Image i, int x, int y, null) Method:
    • x and y determine where the the upper-left corner of the Image will be rendered
What GUI Component To Use?
Back SMYC Forward
  • Motivation:
    • You need a GUI component to render in
    • You can extend any GUI component, you just need to override the public void paint(Graphics g) method
  • A Good Class to Extend:
    • JComponent which does very little in its paint() method
An Example
Back SMYC Forward
javaexamples/multimedia/Example.java
 
An Example: The Driver
Back SMYC Forward
javaexamples/multimedia/ExampleDriver.java
 
There's Always More to Learn
Back -