1 / 28

The Graphics Class

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

rollin
Download Presentation

The Graphics Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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() SE204: 2004/2005: Lecture 4

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

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

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

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

  8. SE204: 2004/2005: Lecture 4

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

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

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

  12. SE204: 2004/2005: Lecture 4

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

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

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

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

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

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

  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() SE204: 2004/2005: Lecture 4

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

  21. SE204: 2004/2005: Lecture 4

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

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

  24. SE204: 2004/2005: Lecture 4

  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 SE204: 2004/2005: Lecture 4

  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 SE204: 2004/2005: Lecture 4

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

  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 SE204: 2004/2005: Lecture 4

More Related