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.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
Web Design & Development Lecture 18. Java Graphics.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Java Graphics Swing Graphics
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
© A+ Computer Science - Chicken yeller = new Chicken();
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 18 產生驗證碼. No Cache 防止在 JSP 或 SERVLET 中的輸出不被 BROWSER 保存在 CACHE 中.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Creating Your Own Widgets CompSci 230 S Software Construction.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
25/2/00SEM107 - © Kamin & ReddyClass 9 - HTML - 1 Class 9 - HTML r Creating a personal web page r Structure of HTML documents r HTML tags r The APPLET.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
CS Lecture 03 Drawing & Displaying Images Lynda Thomas
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven programming.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Eleventh Graphics in Java.
Creating Your Own Widgets
Java Graphics.
Java Graphics CS 2511.
Applets In Java Visit for more Learning Resources 1.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Presentation transcript:

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

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.

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()

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

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

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.

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

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

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!

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

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

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

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)

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 }

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.

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

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

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

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()

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

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

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

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

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

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

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

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

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