Download presentation
Presentation is loading. Please wait.
Published byBlanche Kennedy Modified over 9 years ago
1
Java Graphics Stuart Hansen 11/6/03
2
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
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 JPanel’s 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 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.
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.comhttp://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 Card’s 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. That’s 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?)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.