Lecture 7 Graphics under AWT Graphics Attributes Shapes Clips Affine Transformations Strokes, Paints, Colors, Translucency RenderingHints, Anti-aliasing.

Slides:



Advertisements
Similar presentations
Chapter 4 Transforming and Distorting Objects. Objectives Transform objects Offset and outline paths Create compound paths Work with the Pathfinder panel.
Advertisements

Digital Images in Java Java’s imaging classes. Java imaging library  Java has good support for image processing  Must master a handful of classes and.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Graphics Pipeline.
V Material obtained from a July, 2014 summer workshop in Guildford County.
Week 7 - Monday.  What did we talk about last time?  Specular shading  Aliasing and antialiasing.
Chapter 12 Using Clipping Masks, Paths, & Shapes.
UID – Swing and Graphics Boriana Koleva
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
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.
DIGITAL GRAPHICS & ANIMATION
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Beyond Meshes Spring 2012.
By: Zaiba Mustafa Copyright ©
Pasewark & Pasewark 1 Word Lesson 6 Working with Graphics Microsoft Office 2007: Introductory.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
Microsoft Office 2007: Introductory 1 Word Lesson 6 Working with Graphics Computer Applications 1.
1 Bitmap Graphics It is represented by a dot pattern in which each dot is called a pixel. Each pixel can be in any one of the colors available and the.
Java ME & Blackberry APIs for Game Dev Week III. Overview Java 2D API Java 3D API SVG Blackberry APIs
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Tutorial 2 Drawing Text, Adding Shapes, and Creating Symbols.
Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
2D Graphics: Rendering Details
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Digital Media Dr. Jim Rowan ITEC Vector Graphics Elegant way to construct digital images that –have a compact representation –are scalable –are.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
Chapter 6 Working with Patterns and Brushes. Objectives Use the Move command Create a pattern Design a repeating pattern Use the Pattern Options panel.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
INT 840E Computer graphics Introduction & Graphic’s Architecture.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
Chapter 18 產生驗證碼. No Cache 防止在 JSP 或 SERVLET 中的輸出不被 BROWSER 保存在 CACHE 中.
Creating Your Own Widgets CompSci 230 S Software Construction.
Computer Graphics Chapter 6 Andreas Savva. 2 Interactive Graphics Graphics provides one of the most natural means of communicating with a computer. Interactive.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
Fall UI Design and Implementation1 Lecture 6: Output.
Java Graphics Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zOffset between class.
Digital Media Dr. Jim Rowan ITEC Vector Graphics Elegant way to construct digital images that –have a compact representation –are scalable –are.
Lecture 13: Raster Graphics and Scan Conversion
Adobe Photoshop CS4 – Illustrated Unit A: Getting Started with Photoshop CS4.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
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.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
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 The Rendering Pipeline
12 Graphics and Java 2D™.
Drawing Geometric Objects
Pixels, Colors and Shapes
Flash Interface, Commands and Functions
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Word Lesson 6 Working with Graphics
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.
(c) 2002 University of Wisconsin, CS 559
Chapter 49 Java 2D.
Presentation transcript:

Lecture 7 Graphics under AWT Graphics Attributes Shapes Clips Affine Transformations Strokes, Paints, Colors, Translucency RenderingHints, Anti-aliasing Importing and Exporting Graphics Double Buffering

Graphics under AWT The screen “real-estate” is all owned by somebody. You cannot draw on screen area owned by another. Each component controls the way it, and to some extent its children, are drawn on screen. Parents draw first, then children draw, giving a layered effect of children “on top of” parents.

Making the component appear Component.paint(Graphics g); contains the actual instructions for drawing the component. Override for custom look. Component.update(Graphics g); first blanks the component by painting its background color, then calls paint. May override to reduce flicker. Component.repaint(); calls update ASAP. update only called from a special thread.

The Graphics Object All drawing is done through a Graphics object. This provides many drawing methods, which can be used to draw to many different locations (e.g. a screen region, a printer, an off-screen buffer). An enhanced Graphics2D object was introduced along with Swing. It has some really excellent features.

Shape drawing/filling/erasing. Text drawing Image drawing Hit detection (do 2 shapes overlap?) Graphics2D Methods

Color (foreground, background) Line style ( Stroke ) Fill style ( Paint ) Composite (translucency effects) AffineTransform (rotation, translation, scaling) Font (for drawing text) Clip Region ( Shape ): Graphics directives outside this region will be ignored. RenderingHints : Antialiasing, speed, etc. Graphics2D Attributes For drawing lines, shapes and text Also for images

Pixels and Coordinates The screen, which is an array of pixels, is thought of as part of an infinite plane. Remember geometry? Every point in the plane can be described using 2 coordinates. By default, (0,0) indicates top-left corner, and (width,height) indicates bottom-right corner. In Java, every pixel is a 1x1 square with 4 points as corners (pixels have area, they are not points).

The infinite plane This pixel has corners (2,6), (3,6), (2,7) and (3,7). Pixels are the same as 1x1 rectangles Another pixel, with top-left corner (7,-2) The point (8.7, 3.4) Actual points have no height or width.

Shapes in the plane A Shape is a class representing a subset of the plane. Every point in the plane is either inside or outside a particular Shape. Shapes in Java are represented by a PathIterator, which describes the outline of the shape by breaking it up into simple curves.

Shape Hierarchy Shape Line2DRectangularShape RoundRectangle2D Rectangle2D, Rectangle Ellipse2D Arc2D Polygon QuadCurve2D CubicCurve2D GeneralPath

Gettin’ Shape Use subclass from previous slide. GeneralPath is the most flexible. Shape GlyphVector.getOutline(); Shape GlyphVector.getLogicalBounds(); Font.createGlyphVector(…); Graphics.getClip(); Stroke.createStrokedShape(Shape);

Usin’ Shape boolean Shape.contains(Point); boolean Shape.contains(Rectangle); Rectangle Shape.getBounds2D(); boolean Shape.intersects(Rectangle); Shape AffineTransform.createTransformedShape(Shape); new Area(Shape); Area.add(Area); Area.intersect(Area); Area.subtract(Area); Area.isEmpty();

Showing off your Shape Graphics2D.draw(Shape); This uses the Clip, Transform, Paint, Stroke, and Composite attributes to draw the outline of the shape. Graphics2D.fill(Shape); This uses the Clip, Transform, Paint, and Composite to fill the interior of the shape. Both methods make some assumptions about pixellation and anti-aliasing which may be controlled by Graphics2D.setRenderHints();

Screen: window onto a plane The infinite graphics plane Screen rectangle Only those pixels inside the “screen rectangle” are visible. Similarly, each Component has its own rectangle.

The clip rectangle The plane Clip Rectangle Each Component has its own Graphics object, which has its own “clip” rectangle, initially set to the size of the Component. You can draw anywhere on the plane, but only the clip rectangle will affect the screen.

Clip Rectangles Actually, there are 3 clip rectangles: The user clip, which you use to cut out the piece of the image you want to display. The device clip, which is used to restrict your drawing to the allowed screen region. The composite clip, which finally determines what is displayed. Actually, these clips can be any Shape.

Example, System Clip 1.You load frogPic, a 200x200 image of a frog, and call g.drawImage(frogPic, 50,100); This is how it looks in the graphics plane. 2.Your component has size 150x250, This is how the component looks at its current size. It does not matter where your component is located on the screen. The system clip only allows part of the image.

Example, User Clip 1.Your BinocularsComponent already displays something like this. 2.Use g.setClip() to turn the user clip into the shape at left. Now whatever drawing instructions you give, graphics will only appear inside the white circles at top. If you draw this … this will display

Coordinates Points in the plane are described by two coordinates. Locations are relative to the axes. But what describes where the axes are?

User space and device space When you issue graphics commands, such as drawLine(x,y,width,height), or setClip(…), your inputs are treated as coordinates in user space (a coordinate space). Before rendering to the screen, the instructions are converted to device space. User space and device space have their own axes, and their own scales for each axis. These axes and scales can have any relation.

Affine Transformations (Mathematics) Regardless of how two different coordinate systems are set up, there is an affine transformation which converts one to the other. Affine transformations can be dilations, translations, rotations, shears, or any combination of these. (demo)

Affine Transformation Uses Makes your code simpler. Translate origin to natural location. Work in percentages—scale user coordinates. Handle resized components with one line. Some easy special effects Rotated text and images Shear (map rectangle to trapezoid)

Applying Affine Transforms Graphics2D.translate(); Graphics2D.rotate(); Graphics2D.scale(); Graphics2D.shear(); Graphics2D.transform(); Graphics2D.getTransform(); Graphics2D.setTransform(); compose existing transform with new one.

Affine Transform Matrices Any affine transform can be represented by a 3x3 matrix. See chalkboard… (see also the AffineTransform API) AffineTransform API Composing a sequence of transforms corresponds to matrix multiplication.

Different Strokes In the old AWT, lines were always 1- pixel wide, and you could only choose a single solid color (foreground and fill color). Now, the Stroke class gives a vast number of potential outline styles, and the Paint class lets you draw and fill with patterns, gradients, images, etc.

BasicStrokes Class BasicStroke implements Stroke Lets you control curve width, dash pattern, and corner appearance. Well documented on-line. JFC in a Nutshell provides nice pictures too.

Paint Predefined implementations: Color, GradientPaint, TexturePaint. Color: A solid color. GradientPaint: Linearly interpolates colors between 2 given colors at 2 given points. Cycles or is solid beyond points. TexturePaint: Tiles a BufferedImage in a specified rectangular pattern.

Specifying Colors constants Color.pink, Color.black, etc. Color(float r, float g, float b [, float a]) specifies red/green/blue/alpha each in range [0,1]. Color JColorChooser.showDialog( Component component, String title, Color initialColor); (gets user’s color selection)

Alpha-Compositing Compositing refers to superimposing one image on another. In alpha-compositing, this is done by calculating the color of each pixel as a linear combination of the original color and the new color: C’ = (1-a)C 1 + a C 2 The parameter a (should be alpha) varies from 0 to 1. Opaqueness factor.

Alpha-Compositing Graphics 2D g2 = (Graphics2D)g; g.setComposite(AlphaComposite.getInstan ce(AlphaComposite.SRC_OVER,0.5)); Many images have an “alpha channel” which defines an alpha-value for each pixel. The Image and ColorModel classes provide built-in support for translucent images. (“RGBA”) Example: Translucent GradientPaints.

RenderingHints One of the most important new features. Request graphics methods with Graphics2D.setRenderingHint. g.setRenderingHint( RenderingHint.KEY_ANTIALIASING, RenderingHint.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHint.KEY_RENDERING, RenderingHint.VALUE_RENDER_SPEED);

Anti-aliasing Reduces “jagginess” caused by sudden color changes at pixel borders, by making lots of minor changes to pixels. Can greatly improve readability of rotated text, appearance of sharp borders. May result in weird effects, especially if the background color changes. Demo

Antialiased text close-up

Antialiased line close-up

Image Classes Image BufferedImage extends Image ImageIcon (a small fixed-size image) RenderableImage, RenderedImage ImageProducer, ImageObserver, ImageConsumer ImageFilter

Images Unlike ImageIcon, an Image is not a fixed-size picture. Image can be scaled to any size. The data for an Image may be present in memory, somewhere else on the internet, or generated on demand by an ImageProducer. Prefer to work with BufferedImages. Stored locally; no latency issues.

BufferedImage BufferedImages are represented by a ColorMap and a Raster of pixels, in memory. Since they are in memory, there are many more methods to manipulate them, and fewer things to go wrong.

Loading a BufferedImage First, load the picture as an Image. Methods for doing this on next slide. Create a new BufferedImage of the same dimensions: Image.getHeight(); Image.getWidth(); new BufferedImage(w,h,TYPE_3BYTE_BGR); Copy the image data. Graphics2D g = bufImage.getGraphics(); g.drawImage(Image);

Operations on any Image Display. Graphics.drawImage(Image,int x,int y); Modify off-screen. Image.getGraphics(); The returned Graphics object can be used to change the image. Get dimensions. Image.getWidth(); Image.getHeight();

Uses for BufferedImages Use tiled image as a Paint. new TexturePaint(BufferedImage im, Rectangle r); Convert to JPEG format. import com.sun.image.codec.jpeg.*; JPEGImageEncoder.encode(BufferedImage);

Support for other formarts Go to search: “gif encoder java”. Lots of hits encoder/decoder for.gif,.ppm /java/how_to_print.htmlhttp:// /java/how_to_print.html many useful classes for printing & image formats.

Double Buffering Graphics computations can be very slow. In a long sequence of graphics ops, intermediate displays may be ugly. Double buffering solves this by doing all the graphics ops on an undisplayed image, then “blitting” or copying the image to screen. Trades memory & time for appearance.

Double Buffering in Swing On by default. Can be turned off: JComponent.setDoubleBuffered(false); Turning this off may speed things up, but is probably not worth doing.