Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D."— Presentation transcript:

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

2 J McQuillan SE204: 2004/2005: Lecture 4slide 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.

3 J McQuillan SE204: 2004/2005: Lecture 4slide 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 component s –It can be copied from an existing Graphics object using the Graphics method create() –It can be gotten through the component s method getGraphics()

4 J McQuillan SE204: 2004/2005: Lecture 4slide 4 So, Graphics g = new Graphics(); is invalid!

5 J McQuillan SE204: 2004/2005: Lecture 4slide 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); }

6 J McQuillan SE204: 2004/2005: Lecture 4slide 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.

7 J McQuillan SE204: 2004/2005: Lecture 4slide 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(); }

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

9 J McQuillan SE204: 2004/2005: Lecture 4slide 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!

10 J McQuillan SE204: 2004/2005: Lecture 4slide 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); }

11 J McQuillan SE204: 2004/2005: Lecture 4slide 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(); }

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

13 J McQuillan SE204: 2004/2005: Lecture 4slide 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)

14 J McQuillan SE204: 2004/2005: Lecture 4slide 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 }

15 J McQuillan SE204: 2004/2005: Lecture 4slide 15 Using Graphics2D There are several Graphics2D classes. These include –Line2D –Rectangle2D –Ellipse2D –Arc2D All of these implement the shape interface.

16 J McQuillan SE204: 2004/2005: Lecture 4slide 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);

17 J McQuillan SE204: 2004/2005: Lecture 4slide 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); }

18 J McQuillan SE204: 2004/2005: Lecture 4slide 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);

19 J McQuillan SE204: 2004/2005: Lecture 4slide 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 to change the colour Color.red.darker()

20 J McQuillan SE204: 2004/2005: Lecture 4slide 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); }

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

22 J McQuillan SE204: 2004/2005: Lecture 4slide 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);

23 J McQuillan SE204: 2004/2005: Lecture 4slide 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(); }

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

25 J McQuillan SE204: 2004/2005: Lecture 4slide 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

26 J McQuillan SE204: 2004/2005: Lecture 4slide 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

27 J McQuillan SE204: 2004/2005: Lecture 4slide 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);

28 J McQuillan SE204: 2004/2005: Lecture 4slide 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 Component s implement ImageObserver


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

Similar presentations


Ads by Google