Java Graphics Stuart Hansen 11603 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Java Graphics Stuart Hansen 11603

Description:

Find cards that form a Set. Each card has four features. Color, Symbol Number and Shading ... Striped cards use a Texture Paint pattern. Drawbacks of Java Graphics ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 19
Provided by: csU53
Learn more at: http://www.cs.uwp.edu
Category:
Tags: graphics | hansen | java | stuart

less

Transcript and Presenter's Notes

Title: Java Graphics Stuart Hansen 11603


1
Java GraphicsStuart Hansen11/6/03
2
Whats Wrong with OpenGL
  • Graphics not GUI
  • No real support of text boxes, buttons, etc.
  • Procedural - not OOP
  • No sense of attaching frame to polygon, etc.
  • No inheritance or polymorphism
  • Open Inventor OOP extension to OpenGL
  • Java OpenGL bridge
  • http//www.sgi.com/newsroom/press_releases/2003/ju
    ly/sgisun_opengl.html

3
Java Graphics
  • Component Based
  • AWT vs. Swing
  • We will discuss Swing
  • Uses Model View Controller after a fashion
  • Model holds the data
  • View presents the data
  • Control changes the data

4
Swing Classes
  • JFrame is the window
  • setSize(), setLocation(), setVisible()
  • getContentPane()
  • JButton, JLabel, JTextBox, etc.
  • JPanel is the generic component, useful if we
    are going to do graphics as you are studying them.

5
Steps to Drawing in JPanel
  • Inherit from ComponentUI
  • Override the paint method
  • public void paint (Graphics g, JComponent jc)
  • Define but never explicitly call
  • Call JPanels setUI method with instance of our
    class.

6
Graphics and Graphics2D
  • Graphics2D is subclass and is almost always used.
  • We cast Graphics to Graphics2D
  • Coordinate system (0,0) is upper left of JPanel
  • Lots of draw and fill methods
  • Lots of translate and rotate methods.

7
Example 1 A Clock
  • Clock outline is lines, rectangles and ovals
  • Clock hands are lines, but rotated
  • Clock pendulum swings by doing a rotation

8
Math for Hour Hand
  • 2p is one rotation.
  • 12 hours is one rotation.
  • hourTheta hour/12 2p hour p / 6
  • Add a fudge factor for minutes so hand moves
    smoothly.

9
Example 2 Set Cards
  • Four properties for each card
  • Color, number, symbol and shading
  • 3 possibilities for each property
  • Goal is to have elegant way of drawing the cards

10
Who needs OOP?
  • Game of Set
  • http//www.setgame.com
  • Simple rules
  • Find cards that form a Set
  • Each card has four features
  • Color, Symbol Number and Shading
  • A Set consists of three cards where each feature
    agrees on all cards or disagrees on all cards.

11
This is a Set
12
This is not a Set
13
Object Oriented Design of Set Cards
  • Each attribute is an object
  • No need for if
  • E.g. No need for
  • if (card.shading.equals(squiggle)) . .
  • Shading has all needed methods in it.

14
Flyweight Design Pattern
  • Only one instance of each class!
  • Allows us to compare using
  • Limits number of objects to 12 rather than
    hundreds

15
Back to Graphics
  • Cards paint method pseudocode
  • setColor(color)
  • for each location
  • shape.draw(shading)
  • Color, location, shape and shading are the
    attributes

16
Shape uses polymorphism
  • Ovals, Diamonds and Squiggles each know how to
    draw themselves
  • Shape interface specifies that each shape has a
    draw method. Thats it.

17
Shading specifies the fill pattern
  • Solid cards use foreground color
  • Open cards use the background color
  • Striped cards use a Texture Paint pattern

18
Drawbacks of Java Graphics
  • Java will always be slower than C
  • Java lacks much of the OpenGL functionality
  • Texturing, lighting, etc.
  • Java requires non-standard library to do 3D
    Graphics
  • Complex class hierarchies are harder to learn
    than procedural API (or are they?)
Write a Comment
User Comments (0)
About PowerShow.com