Download Java Graphics Stuart Hansen 11/6/03

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Graphics
Stuart Hansen
11/6/03
What’s 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/july/sgisun_opengl.html
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
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.
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 JPanel’s setUI method with instance
of our class.
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.
Example 1 – A Clock
• Clock outline is lines, rectangles and ovals
• Clock hands are lines, but rotated
• Clock pendulum swings by doing a
rotation
Math for Hour Hand
• 2π is one rotation.
• 12 hours is one rotation.
• hourTheta = hour/12 * 2π = hour * π / 6
• Add a fudge factor for minutes so hand
moves smoothly.
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
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.
This is a Set
This is not a Set
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.
Flyweight Design Pattern
• Only one instance of each class!
– Allows us to compare using ==
• Limits number of objects to 12 rather than
hundreds
Back to Graphics
• Card’s paint method pseudocode:
setColor(color)
for each location
shape.draw(shading)
• Color, location, shape and shading are the
attributes
Shape uses polymorphism
• Ovals, Diamonds and Squiggles each
know how to draw themselves
• Shape interface specifies that each shape
has a draw method. That’s it.
Shading specifies the fill pattern
• Solid cards use foreground color
• Open cards use the background color
• Striped cards use a Texture Paint pattern
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?)