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

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

  • View
    222

  • Download
    4

Embed Size (px)

Citation preview

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

1

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Drawing in Java

2D Graphics API

Page 2: 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

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

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

3

Graphics Programming UQC117S2 Semester 1 2003/4. Session 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

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

4

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Facilities

• Supports device independence• Supports printing• Renders 3 main classes of graphical

object– Shapes– Images– Text

• Comes with built in transformation capabilities

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

5

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

6

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Using Colour• Color class defines the following colours:

black, blue, cyan, darkGray, gray, green, lightGray, magneta, orange, pink, red, white, yellow.

Examplegg.setColor(Color.green)

where g is the Graphics2D context.

• Colour can also be defined in terms of the RGB model.

Exampleg.setColor(102,56,9);

OR use floats OR a single integer value (bits 0 - 7 , 8 - 15, 16 - 23 )

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

7

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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…..

}

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

8

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

9

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Example – primitive shapeimport 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); }}

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

10

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

11

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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); }}

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

12

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

13

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Using Images

Relevant classes Package

Image java.awt

BufferedImage java.awt.image

Applet java.applet

Toolkit java.awt

MediaTracker java.awt

PlusVarious ‘management’ classes in the java.awt.image package.

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

14

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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)

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

15

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

• getImage (Toolkit method)getImage( URL url)

ORgetImage(String filename)

• Needs to use either :Toolkit.getDefaultToolkit

ORgetToolkit from the Component class

• Example:Toolkit toolkit = Toolkit.getDefaultToolkit()Image image1 = toolkit.getImage(“myImage.gif”);

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

16

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

17

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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

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

18

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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"); } }

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

19

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Example (continued)

public void paint(Graphics g){

Graphics2D gg;

gg = (Graphics2D)g;

gg.drawImage(displayImage,0,0,this);

}

}

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

20

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

Animation

• Always achieved by creating the appearance of motion by showing successive frames at speed.

• Computer animation runs at about 10 - 12 frames per second24 frames per second gives smooth motion:

Animation loop (pseudocode) :Get position for imageWHILE (you want the image animated)

Draw the image at given positionWait a whileWork out a new positionRepaint the image in the background colour

END WHILE

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

21

Graphics Programming UQC117S2 Semester 1 2003/4. Session 3

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