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

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

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.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
UID – Swing and Graphics Boriana Koleva
COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
GUI Tutorial Images. Useful Info – not on final exam.
Web Design & Development Lecture 18. Java Graphics.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
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.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Internet Software Development Applets Paul J Krause.
2D Graphics: Rendering Details
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Nov 061 Size Control How is a component’s size determined during layout and during resize operations? Three factors determine component sizes: The component’s.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
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.
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.
Objectives of This Session
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
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 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(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)
Java Dynamic Graphics.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Java Graphics Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zOffset between class.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Creating Your Own Widgets
Java Graphics.
Java Graphics CS 2511.
Basic Graphics Drawing Shapes 1.
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
Lecture 4: Standard Java Graphics
Presentation transcript:

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

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

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

Graphics Programming UQC117S2 Semester /4. Session 3 4 Facilities Supports device independence Supports printing Renders 3 main classes of graphical object –Shapes –Images –Text Comes with built in transformation capabilities

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

Graphics Programming UQC117S2 Semester /4. Session 3 6 Using Colour Color class defines the following colours: black, blue, cyan, darkGray, gray, green, lightGray, magneta, orange, pink, red, white, yellow. Example gg.setColor(Color.green ) where g is the Graphics2D context. Colour can also be defined in terms of the RGB model. Example g.setColor(102,56,9 ); OR use floats OR a single integer value (bits 0 - 7, , )

Graphics Programming UQC117S2 Semester /4. Session 3 7 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….. }

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

Graphics Programming UQC117S2 Semester /4. Session 3 9 Example – primitive shape import 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); }

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

Graphics Programming UQC117S2 Semester /4. Session 3 11 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); }

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

Graphics Programming UQC117S2 Semester /4. Session 3 13 Using Images Relevant classesPackage Imagejava.awt BufferedImagejava.awt.image Appletjava.applet Toolkitjava.awt MediaTrackerjava.awt Plus Various ‘management’ classes in the java.awt.image package.

Graphics Programming UQC117S2 Semester /4. Session 3 14 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)

Graphics Programming UQC117S2 Semester /4. Session 3 15 getImage (Toolkit method) getImage( URL url) OR getImage(String filename) Needs to use either : Toolkit.getDefaultToolkit OR getToolkit from the Component class Example: Toolkittoolkit = Toolkit.getDefaultToolkit() Imageimage1 = toolkit.getImage(“myImage.gif”);

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

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

Graphics Programming UQC117S2 Semester /4. Session 3 18 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"); }

Graphics Programming UQC117S2 Semester /4. Session 3 19 Example (continued) public void paint(Graphics g){ Graphics2D gg; gg = (Graphics2D)g; gg.drawImage(displayImage,0,0,this); }

Graphics Programming UQC117S2 Semester /4. Session 3 20 Animation Always achieved by creating the appearance of motion by showing successive frames at speed. Computer animation runs at about frames per second 24 frames per second gives smooth motion: Animation loop (pseudocode) : Get position for image WHILE (you want the image animated) Draw the image at given position Wait a while Work out a new position Repaint the image in the background colour END WHILE

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