Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real...

18
Java Graphics Stuart Hansen 11/6/03

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?)