An Introduction on OpenGL With 2D Graphics - OpenGL Tutorial
Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real...
-
Upload
blanche-kennedy -
Category
Documents
-
view
215 -
download
1
Transcript of Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real...
![Page 1: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/1.jpg)
Java Graphics
Stuart Hansen11/6/03
![Page 2: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/3.jpg)
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
![Page 4: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/4.jpg)
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.
![Page 5: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/5.jpg)
Steps to Drawing in JPanel
• Inherit from ComponentUI
• Override the paint methodpublic void paint (Graphics g, JComponent jc)
– Define but never explicitly call
• Call JPanel’s setUI method with instance of our class.
![Page 6: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/6.jpg)
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.
![Page 7: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/7.jpg)
Example 1 – A Clock
• Clock outline is lines, rectangles and ovals
• Clock hands are lines, but rotated
• Clock pendulum swings by doing a rotation
![Page 8: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/8.jpg)
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.
![Page 9: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/9.jpg)
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
![Page 10: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/10.jpg)
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.
![Page 11: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/11.jpg)
This is a Set
![Page 12: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/12.jpg)
This is not a Set
![Page 13: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/13.jpg)
Object – Oriented Design of Set Cards
• Each attribute is an object– No need for if– E.g. No need forif (card.shading.equals(“squiggle”)) . .
– Shading has all needed methods in it.
![Page 14: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/14.jpg)
Flyweight Design Pattern
• Only one instance of each class!– Allows us to compare using ==
• Limits number of objects to 12 rather than hundreds
![Page 15: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/15.jpg)
Back to Graphics
• Card’s paint method pseudocode:
setColor(color)
for each location
shape.draw(shading)
• Color, location, shape and shading are the attributes
![Page 16: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/16.jpg)
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.
![Page 17: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/17.jpg)
Shading specifies the fill pattern
• Solid cards use foreground color
• Open cards use the background color
• Striped cards use a Texture Paint pattern
![Page 18: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081000/56649f045503460f94c17a8e/html5/thumbnails/18.jpg)
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?)