Lecture07 Graphics Programming in Java. Introduction Most of the graphics programming of java is done with: Most of the graphics programming of java is.

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

Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
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.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
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. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
A Simple Applet.
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.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
Java ME & Blackberry APIs for Game Dev Week III. Overview Java 2D API Java 3D API SVG Blackberry APIs
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Graphics and Multimedia Part #2
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.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Digital Media Dr. Jim Rowan ITEC Vector Graphics Elegant way to construct digital images that –have a compact representation –are scalable –are.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
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
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
Creating Your Own Widgets CompSci 230 S Software Construction.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Fall UI Design and Implementation1 Lecture 6: Output.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
Java Graphics Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zOffset between class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Graphics in Java Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
IS502:M ULTIMEDIA D ESIGN FOR I NFORMATION S YSTEM D IGITAL S TILL I MAGES Presenter Name: Mahmood A.Moneim Supervised By: Prof. Hesham A.Hefny Winter.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Advanced AWT Paint. ● Paint is used to fill a shape, its inside is covered with paint. ● Use the setPaint method to set the paint style to an object with.
Barb Ericson Georgia Institute of Technology September 2005
Advanced AWT The Rendering Pipeline
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Java Graphics CS 2511.
Graphics and Multimedia
JAVA 2 Design and programming of GUI
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.
Chapter 49 Java 2D.
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

Lecture07 Graphics Programming in Java

Introduction Most of the graphics programming of java is done with: Most of the graphics programming of java is done with: Java AWT - The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. This is the very first GUI support Java had.Java AWT - The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. This is the very first GUI support Java had. Swing – An advanced GUI library designed to overcome limitations of AWT.Swing – An advanced GUI library designed to overcome limitations of AWT. Java2D - The Java 2DTM API is a set of classes for advanced 2D graphics and imaging. It encompasses line art, text, and images in a single comprehensive model. These classes are provided as additions to the java.awt and java.awt.image packages.Java2D - The Java 2DTM API is a set of classes for advanced 2D graphics and imaging. It encompasses line art, text, and images in a single comprehensive model. These classes are provided as additions to the java.awt and java.awt.image packages.

The Java 2D API Enhances the graphics, text, and imaging capabilities of the Abstract Windowing Toolkit (AWT), enabling the development of richer user interfaces and new types of Java applications. The Java 2D API supports enhanced colour definition and composition. It provides a uniform rendering model for printers and display devices. When used in conjunction with the Java Media Framework and other Java Media APIs, the Java 2D APIs can be used to create and display animations and other multimedia presentations.

Coordinate Systems The Java 2D API maintains two coordinate systems: User space is a device-independent, logical coordinate system. Applications use this coordinate system exclusively; all geometries passed into Java 2D rendering routines are specified in user space. Device space is a device-dependent coordinate system that varies according to the target rendering device.

User Space The user space origin is located in the upper-left corner of the space, with x values increasing to the right and y values increasing downward. User space represents a uniform abstraction of all possible device coordinate systems. The device space for a particular device might have the same origin and direction as user space, or it might be different. User space coordinates are automatically transformed into the appropriate device space when a graphic object is rendered. Often, the underlying platform device drivers are used to perform this conversion.

Device Space The Java 2D API defines three levels of configuration information that are maintained to support the conversion from user space to device space. This information is encapsulated by three classes: GraphicsEnvironment : The GraphicsEnvironment describes the collection of rendering devices visible to a Java application on a particular platform. GraphicsDevice : A GraphicsDevice describes an application-visible rendering device, such as a screen or printer. GraphicsConfiguration : Each possible configuration of the device is represented by a GraphicsConfiguration. E.g. 640x480x16 colours, 640x480x256 colours, etc. A GraphicsEnvironment can contain one or more GraphicsDevices; in turn, each GraphicsDevice can have one or more GraphicsConfigurations.

Example – Graphics Environment GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j < gs.length; j++) { GraphicsConfiguration[] gc = gs[i].getConfigurations(); // Display the details of gc for (int i=0; i < gc.length; i++) { Rectangle virtualBounds = new Rectangle(); virtualBounds = gc[i].getBounds(); // Display details of virtualBounds }

Rendering with Graphics and Graphics2D There is a three-step recipe for writing a graphics program in Java Get a graphics context Set or Modify the context Render something.

Graphics to Graphics2D The graphics context of a awt program can be obtained as follows: public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; } The casting is done to access the increased functionality of Graphics2D class which was made available via Java2D API.

Example public void paint(Graphics g) { // Cast the Graphics object to access the increased // functionality of Java2D Graphics2D g2d = (Graphics2D) g; // Set or Modify the graphics context g2d.setColor(Color.red); // Render something g2d.fill(new Rectangle2D.Float(200.0f,200.0f,75.0f,75.0f)); }

Graphics Context The collection of state attributes associated with a Graphics2D is referred to as the Graphics2D context. Graphics context specifies properties of rendering. You can modify the state attributes that form the Graphics2D context to: Vary the stroke width. Change how strokes are joined together. Set a clipping path to limit the area that is rendered. Translate, rotate, scale, or shear objects when they are rendered. Define colours and patterns to fill shapes with.

Exercise Check out the Java 2 SDK documentation and find out which of following are interfaces and which are classes: Composite, CompositeContext, Paint, PaintContext, Stroke, AffineTransform, AlphaComposite, BasicStroke, Color, GradientPaint, Graphics2D, TexturePaint.

Rendering on Components Any object derived from the Component class has a Graphics object that can be used to render graphics onto it. Thus the developer can render graphics on all types of buttons, canvases, and check boxes.

An example rendering on a JButton: class myCustomButton extends JButton { public void paint(Graphics g) { // Step one: Cast the Graphics object as Graphics2D Graphics2D g2d = (Graphics2D) g; // make the GradientPaint object going from blue to green // across from top left to bottom right GradientPaint gp = new GradientPaint(0,0,Color.blue, this.getSize().width/20, this.getSize().height/20, Color.green, true); // Step two: set the graphics context g2d.setPaint(gp); // Render something g2d.fill(new Rectangle2D.Float(0.0f,0.0f,75.0f,75.0f)); }

Shape Primitives The shape primitives are all contained in the java.awt.geom package. All shape primitives implement the PathIterator interface that specifies the outline of the shape. Rectangle2D, RoundRectangle2D, Arc2D, and Ellipse2D are derived from the abstract class RectagularShape based on the common ability to describe these primitives through a rectagular bounding box. Line, QuadCurve2D, and CubicCurve2D are line segments described by their two endpoints with the requisite control points. GeneralPath allows for the specification of a series of points that can be connected with any combination of straight, cubic, or quadratic line segments. Area allows the creation of new shapes through the use of intersection, union, and subtraction of other shapes.

Example public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; // cast the graphics object g2d.setColor(Color.green); // set the graphics context // Create some graphics - (A path) GeneralPath gp = new GeneralPath(); int cwidth = this.getSize().width; int cheight = this.getSize().height; gp.moveTo((int) cwidth/2, (int) cheight/2); // starting point gp.append(new Rectangle2D.Float((float)cwidth/2, (float)cheight/2,20.0f,45.0f),true); // append a rectangle gp.lineTo((int)cwidth/4, (int)cheight/4); // line segment gp.append(new Ellipse2D.Float((float)0.9*cwidth, (float)0.25*cheight,20.0f,45.0f),true); // append a ellipse gp.closePath(); g2d.draw(gp); // Render the graphics reportGP(gp); }

Example // Report about each segment as we go along the general path public void reportGP(GeneralPath gp) { AffineTransform at = new AffineTransform(); PathIterator pi = gp.getPathIterator(at); int segNumber = 0; while (pi.isDone() == false) { segNumber++; System.out.println("Getting data for segment#: "+ segNumber +"**"); float[] coords = new float[6]; // get segment types and sequential pairs of (x,y) coords System.out.println("Current Segment Type: "+ pi.currentSegment(coords)); // print coords pairs for(int j=0;j<6;j++) System.out.println("j:"+j+" coords[j] "+coords[j]); pi.next(); }

Graphics Stroking Stroking a Shape such as a GeneralPath object is equivalent to running a logical pen along the segments of the GeneralPath. The Graphics2D Stroke attribute defines the characteristics of the mark drawn by the pen. A BasicStroke object is used to define the stroke attributes for a Graphics2D context.

Example code segment public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; // Create some graphics GeneralPath gp = new GeneralPath(); // build gp BasicStroke stroke = new BasicStroke(10); // set line thickness to 10 // modify graphics context - change the stroke g2d.setStroke(stroke); g2d.draw(gp); // render graphics }

Fill Attributes and Painting The fill attribute in the Graphics2D context is represented by a Paint object. You add a Paint to the Graphics2D context by calling setPaint. There are three classes that implement Paint interface Solid color painting with the Color object. Gradient painting with the GrdientPaint object. Texture painting with the TexturePaint object. When a Shape or glyph is drawn (Graphics2D.draw, Graphics2D.drawString), the Paint is applied to all of the pixels that lie inside of the Shape that represents the object’s stroked outline. When a Shape is filled (Graphics2D.fill), the Paint is applied to all of the pixels that lie within the Shape’s contour.

Solid Color Painting To user-define predefined color objects Color red = new Color.red; To specify RGB values between 0 and 1 Color red = new Color(1.0f,0.0f,0.0f); To specify an alpha value for transparency Color red = new Color(1.0f,0.0f,0.0f,0.5f);

Gradient Painting GradientPaint object represent transition between two colors. In order to create a GradientPaint object it is necessary to specify: The starting and end points for the transition. The two colors to use. An optional rule to specify how the paint looks outside the region specified by the starting and end points. This outer zone can be either cyclic (repeats outside the start and endpoints) or acyclic (remains at the final value of the gradient outside the start and endpoints).

An Example: Gradient Painting public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; //... int width = this.getSize().width; int height = this.getSize().height; Point x = new Point2D.Double(width/3,height/3); Point y = new Point2D.Double(2*width/3,2*height/3); GradientPaint gp = new GradientPaint(x,y,Color.red,Color.green,cycle); g2d.setPaint(gp); //... }

Texture Painting The constructor for a TexturePaint object would look as follows: public TexturePaint(BufferedImage txtbuffer, Rectangle2D anchor); The BufferedImage object must be created from an external source or by programmatically filled by using some sort of algorithm. When the TexturePaint object is instantiated, an anchoring rectangle is specified in user space coordinates. This acchoring rectangle (with its associated BufferedImage) is copied in both x and y directions infinitely across the shape to be rendered.

Managing Transparency a source over alpha composite object is created with an alpha of.5 and added to the graphics context, causing subsequent shapes to be rendered 50% transparent. public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // … // … // Create a new alpha composite AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f); g2.setComposite(ac); // … // …}