28
J McQuillan SE204: 2004/2005: Lecture 4 slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes – Graphics – Graphics2D

The Graphics Class

  • Upload
    rollin

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

The Graphics Class. Used when we need to draw to the screen Two graphics classes Graphics Graphics2D. Graphics and Graphics2D. Graphics java.awt.Graphics is an abstract class It provides a limited range of drawing operations Graphics2D Graphics2D is an extension of Graphics - PowerPoint PPT Presentation

Citation preview

Page 1: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 1

The Graphics Class

• Used when we need to draw to the screen

• Two graphics classes

– Graphics

– Graphics2D

Page 2: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 2

Graphics and Graphics2D

• Graphics– java.awt.Graphics is an abstract class– It provides a limited range of drawing operations

• Graphics2D– Graphics2D is an extension of Graphics– It enables far more powerful drawing operations

Graphics context means the java.awt.Graphics

object that is currently being used. This is mostly the

current drawing space within a window.

Page 3: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 3

An instance of the java.awt.Graphics class can only be

obtained from another graphics element such as an image or an

already existing graphics object.

How do we obtain an instance of a graphics object?

– It is passed to the paint() and paintComponent() methods of the components

– It can be copied from an existing Graphics object using the Graphics method create()

– It can be gotten through the components method getGraphics()

Page 4: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 4

So,

Graphics g = new Graphics();

is invalid!

Page 5: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 5

A Simple Example

import java.awt.*;

import javax.swing.*;

class drawPanel extends JPanel{

drawPanel(){

setPreferredSize(new Dimension(200, 300));

}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawLine(10, 10, 100, 100);

}

}

Page 6: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 6

When we want to show graphics on a frame, we will not

directly draw on the surface of the frame.

Instead, we will draw the graphics onto a JPanel and add

the JPanel to the frame.

Page 7: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 7

public class drawFrame extends JFrame {drawFrame(){

setSize(300, 400);drawPanel p = new drawPanel();getContentPane().add(p);setVisible(true);

}

public static void main(String args[]){new drawFrame();

}}

Page 8: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 8

Page 9: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 9

Methods in the Graphics class

void drawLine(int startX, int startY, int endX, int endY)

void drawRect(int x, int y, int width, int height)

void drawOval(int x, int y, int width, int height)

Look up the rest of these methods!

Page 10: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 10

Another Example

import java.awt.*;

import javax.swing.*;

class drawPanel extends JPanel{

drawPanel(){

setPreferredSize(new Dimension(200, 300));

}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawLine(10, 10, 100, 100);

g.drawRect(30, 35, 60, 70);

}

}

Page 11: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 11

public class drawFrame extends JFrame {drawFrame(){

setSize(300, 400);drawPanel p = new drawPanel();getContentPane().add(p);setVisible(true);

}

public static void main(String args[]){new drawFrame();

}}

Page 12: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 12

Page 13: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 13

The above methods drew the outline of the shapes. What if

we want to fill the shape with a particular colour?

Use the following methods

fillRect(int x, int y, int width, int height)

Page 14: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 14

Using the Graphics2D class

A graphics object is passed to the paint() and

paintComponent() methods.

To use a Graphics2D object we cast it.

public void paintComponent(Graphics g){

super.paintComponenet(g);

Graphics2D g2 = (Graphics2D)g;

//draw using Graphics2D

}

Page 15: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 15

Using Graphics2D

There are several Graphics2D classes.

These include – Line2D– Rectangle2D– Ellipse2D– Arc2D

All of these implement the shape interface.

Page 16: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 16

Drawing Shapes using Graphics2D

To draw a particular shape using Graphics2D,

1. Create an instance of the shape

2. Call the draw method on the Graphics2D object and pass in the shape

Rectangle r = new Rectangle(5, 10, 20, 30);

g2.draw(r);

Page 17: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 17

import java.awt.*;

import javax.swing.*;

public class drawPanel2 extends JPanel{

drawPanel2(){

//constructor code

}

paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

Rectangle r = new Rectangle(0,0,5,6);

g2.draw(r);

}

}

Page 18: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 18

How do we fill these shapes?

Call the fill method instead of draw

Rectangle r = new Rectangle(0,0,5,7);

g2.fill(r);

Page 19: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 19

Colour

setColor(Color c) - used to set the colour for drawing

public void paintComponent(Graphics g){super.paintComponent(g);g.setColor(Color.red);g.drawLine(10, 10, 100, 100);

}

Can use the brighter() and darker() methods tochange the colour

Color.red.darker()

Page 20: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 20

Text

Text is drawn using the drawString() method

drawString(String s, int x, int y)

x,y coordinates determine the far left position of the baseline

public void paintComponent(Graphics g){

super.paintComponent(g);

g. drawString(“Hello”, 50, 50);

}

Page 21: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 21

Page 22: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 22

Fonts

Various fonts can be used when drawing text on the screen.

It is possible to to create a font

Font myFont = new Font(“Fixed”, Font.BOLD, 12);

Pass the Font object to the Graphics setFont() method

g.setFont(myFont);

Page 23: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 23

import java.awt.*;import javax.swing.*;class drawPanel extends JPanel{

drawPanel(){setPreferredSize(new Dimension(200, 300));

}public void paintComponent(Graphics g){

super.paintComponent(g);setBackground(Color.white);Font myFont = new Font(“Fixed”, Font.BOLD, 20);g.setColor(Color.red);g.setFont(myFont);g.drawString("Hello", 50, 50);

}}public class drawFrame extends JFrame {

drawFrame(){setSize(300, 400);drawPanel p = new drawPanel();getContentPane().add(p);setVisible(true);

}

public static void main(String args[]){new drawFrame();

}}

Page 24: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 24

Page 25: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 25

Images

• java.awt.Image is an abstract class

• java.awt.image is a package; it contains many classes for image manipulation

• GIF and JPEG are only supported

Page 26: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 26

Displaying Images

To display an image

1. Load the Image

2. Draw the image in the components paint() method or the JPanels paintComponent() method

Page 27: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 27

Loading an Image

We never create an instance of the Image class. We load them

from a file.

String fname = “myImage.gif”;

Image i = Toolkit.getDefaultToolkit.getImage(fname);

We can also load an Image from a URL

String fname = “myImage.gif”;

Image i = getImage(getDocumentBase(), fname);

Page 28: The Graphics Class

J McQuillan SE204: 2004/2005: Lecture 4 slide 28

Drawing the Image

java.awt.Graphics has several methods for drawing an

image.

• g.drawImage(img, x, y, observer);

• g.drawImage(img, x, y, w, h, observer);

An image informs its ImageObserver about any changes

Usually you are loading an image in the paint() method of

a component, so pass this as the ImageObserver

All Components implement ImageObserver