Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 1 Drawing in Java 2D Graphics API.

Similar presentations


Presentation on theme: "Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 1 Drawing in Java 2D Graphics API."— Presentation transcript:

1 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 1 Drawing in Java 2D Graphics API

2 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 2 Customised Drawing and Using Images in Java. Crucial Things to know –When a Component is created its Graphics context is also created –The graphics context (an instance of the Graphics class) comes some useful fields e.g. The Component object on which to draw. A translation origin for rendering and clipping co-ordinates. The current clip. The current colour. The current font. The current logical pixel operation function (XOR or Paint). –Contains classes for drawing primitive shapes –Better to use Graphics2D

3 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 3 Java 2D Graphics class belongs to Java 1.1 The Java 2 platform supports much more sophisticated graphics Uses a Graphics2D context for use in e.g. –Presentation graphics –CAD –Scientific visualisation –Cartography –Advertising/entertainment

4 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 4 Facilities Supports device independence Supports printing Renders 3 main classes of graphical object –Shapes –Images –Text Comes with built in transformation capabilities

5 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 5 Java 2D Graphics Model Inherits from Graphics Similar to Graphics –Prepare a context Initialise graphic attributes to draw graphics elements –Call a suitable method to render the graphical element, passing the context object as a parameter –Rendering methods are: paint(), paintAll(), print(), printAll(), update() BUT –Components are still created with a Graphics context. –Need to cast to Graphics2D to use the new facilities

6 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 6 Using Colour Color class defines the following colours: black, blue, cyan, darkGray, gray, green, lightGray, magneta, orange, pink, red, white, yellow. Example gg.setColor(Color.green ) where g is the Graphics2D context. Colour can also be defined in terms of the RGB model. Example g.setColor(102,56,9 ); OR use floats OR a single integer value (bits 0 - 7, 8 - 15, 16 - 23 )

7 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 7 Typical Code public void paint(Graphics g){ // Cast as Graphics2D Graphics2D gg = (Graphics2D)g; // Set context attributes e.g. gg.setPaint(Color.orange); // Add rest of rendering code….. }

8 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 8 Rendering Method In order to draw customised images, it is advisable to use a panel (also know as pane) Usually to draw an image/primitive shape, you make a call to the component’s paint method. paint is automatically called when a component is first displayed or redrawn You have to implement paint to invoke your customized behaviour. This is the method in which you embed your drawing code. Call paint from the super class to clear the drawing area

9 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 9 Example – primitive shape import java.awt.*; import javax.swing.*; public class TestDrawing extends JFrame { public TestDrawing() { JPanel myPanel; setTitle("Test Drawing"); myPanel = new RectPanel(); setContentPane(myPanel); } public static void main(String[] args) { TestDrawing f; f = new TestDrawing(); f.setSize(300,250); f.setVisible(true); } class RectPanel extends JPanel { public void paint(Graphics g) { super.paint(g); Graphics2D gg = (Graphics2D)g; gg.setColor(Color.red); gg.drawRect(30, 30, 100, 100); }

10 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 10 A Better Version (more Graphics2D) All Geometric shapes are represented by the Shape interface Java2D represents various primitive shapes in the java.awt.geom package, all of which implement this interface (not points) Also provides the classes for performing operations on objects related to two-dimensional geometry. –E.g. transformations Defines an interface (PathInterator) for defining arbitrarily complex shapes

11 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 11 import java.awt.*; Import java.awt.geom.*; import javax.swing.*; class RectPanel extends JPanel { public void paint(Graphics g) { Graphics2D gg; Rectangle2D rect; super.paint(g); gg = (Graphics2D)g; gg.setColor(Color.red); rect = new Rectangle2D.Float(30, 30, 100, 100); gg.draw(rect); }

12 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 12 Why is this Better When It’s more Complex? Allows filling with solid colour or patterns Gradient fill Textured filling Clipping Constructive Area Geometry Compositing ……. And more

13 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 13 Using Images Relevant classesPackage Imagejava.awt BufferedImagejava.awt.image Appletjava.applet Toolkitjava.awt MediaTrackerjava.awt Plus Various ‘management’ classes in the java.awt.image package.

14 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 14 Loading Images Support provided for.gif and.jpeg –getImage in the Toolkit class ( or Applet) –Image isn’t loaded until you try to draw it. –MediaTracker and ImageObserver keep track of progress –Images can be loaded to an off-screen area, using: createGraphics (a BufferedImage method)

15 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 15 getImage (Toolkit method) getImage( URL url) OR getImage(String filename) Needs to use either : Toolkit.getDefaultToolkit OR getToolkit from the Component class Example: Toolkittoolkit = Toolkit.getDefaultToolkit() Imageimage1 = toolkit.getImage(“myImage.gif”);

16 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 16 Tracking Image Loading Done in: MediaTracker class OR ImageObserver interface Mostly MediaTracker is sufficient especially checkID and checkAll (request loading) waitForID and waitForAll (wait for loading

17 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 17 Drawing Images drawImage: From the Graphics class. drawImage gives the following capabilities: –Draws an image at its ‘normal’ size in a given position on the component area –Draws a scaled image at a given position on the component area –Draws an image at ‘normal’ size and at a given position, with a background colour under transparent pixels –Draws a scaled image and at a given position, with a background colour under transparent pixels

18 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 18 Example mport javax.swing.*; import java.awt.image.*; import java.awt.*; public class TestImage extends JPanel{ Image displayImage; TestImage(){ displayImage= Toolkit.getDefaultToolkit().getImage("children.jpg"); MediaTracker mt = new MediaTracker(this); mt.addImage(displayImage,1); try { mt.waitForAll(); } catch(Exception e){ System.out.println("Exception while loading"); }

19 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 19 Example (continued) public void paint(Graphics g){ Graphics2D gg; gg = (Graphics2D)g; gg.drawImage(displayImage,0,0,this); }

20 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 20 Animation Always achieved by creating the appearance of motion by showing successive frames at speed. Computer animation runs at about 10 - 12 frames per second 24 frames per second gives smooth motion: Animation loop (pseudocode) : Get position for image WHILE (you want the image animated) Draw the image at given position Wait a while Work out a new position Repaint the image in the background colour END WHILE

21 Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 21 Alternative Type of Animation You might want to animate a sequence of frames Pseudocode is the same except that instead of calculating a new position, you need to determine which frame to display next. –i.e. You might have a collection of file names through which you cycle. Animation is ‘processor hungry’ Use a separate Thread to control the animation


Download ppt "Graphics Programming UQC117S2 Semester 1 2003/4. Session 3 1 Drawing in Java 2D Graphics API."

Similar presentations


Ads by Google