1 Chapter 4 2D Graphics: Advanced Topics  To construct custom shape primitives  To apply basic image processing techniques  To create fractal images.

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

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.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
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.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Chapter 2: Graphics Programming
Computer Graphics Tz-Huan Huang National Taiwan University (Slides are based on Prof. Chen’s)
Lecture 9 Grey Level & Colour Enhancement TK3813 Dr. Masri Ayob.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
Continuing to very Powerful Rendering Model Java2D.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Point Processing Image Arithmetic. Arithmetic Image Operations (blending) 2  Two source images can be added, multiplied, one subtracted from the other.
Regional Processing There goes the neighborhood. Overview  Under point processing a single input sample is processed to produce a single output sample.
Intro to Game Development An introduction to Swing, Java2D, JOGL, and game design Created by: Dennis Lawter and Dustin Scott.
Java2 Printing API Jean-Cédric Desrochers March 9, 2000.
UID – Swing and Graphics Boriana Koleva
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 4 2D Graphics: Advanced Topics.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
Lecture09 Push & Immediate Mode Imaging. Push Imaging Model (PIM) ImageProducer is an interface for objects which can produce the image data for Images.
Images and colour Colour - colours - colour spaces - colour models Raster data - image representations - single and multi-band (multi-channel) images -
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.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
V Obtained from a summer workshop in Guildford County July, 2014
1 Review of COMPSCI 221  Chapters 1-11 in text  User Interfaces will be addressed as a take- home question.
1 CMT Fundamentals of Computer Graphics Revision Dr. Xiaohong Gao BG---Room 2C23 Week 11.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Vector vs. Bitmap
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2D Graphics: Rendering Details
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new.
Field Trip #33 Creating and Saving Fractals. Julia Set We consider a complex function, f(z) For each point on the complex plane (x,y), where z = x + iy,
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Field Trip #19 Animations with Java By Keith Lynn.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
2D Graphics: Advanced Topics Chapter 4. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics  2D Graphics: Rendering Details  2D Graphics:
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Digital Media Dr. Jim Rowan ITEC Up Next! In the next several lectures we will be covering these topics: –Vector graphics –Bitmapped graphics –Color.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Java Dynamic Graphics.
Digital Media Dr. Jim Rowan ITEC 2110 Images: Chapters 3, 4 & 5.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Three-Dimensional Object Representation
CISC 110 Day 3 Introduction to Computer Graphics.
Object-Oriented Software Engineering Using Threads and simple Animation.
Digital Media Dr. Jim Rowan ITEC 2110 Chapter 3. Roll call.
Digital Media Lecture 5: Vector Graphics Georgia Gwinnett College School of Science and Technology Dr. Jim Rowan.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java 2D Image Manipulation A Brief Overview Ziv Yaniv.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
OUTPUT PRIMITIVES A.Aruna/Faculty of Information technology/SNSCE13/19/2016.
Graphics Programming. Graphics Functions We can think of the graphics system as a black box whose inputs are function calls from an application program;
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 3 Syntax, Errors, and Debugging
Digital Media Dr. Jim Rowan ITEC 2110.
Presentation transcript:

1 Chapter 4 2D Graphics: Advanced Topics  To construct custom shape primitives  To apply basic image processing techniques  To create fractal images  To create 2D animation  To perform graphics printing

2 Custom Shapes  GeneralPath –a legacy class that is final  Path2D –has two subclasses: Path2D.Double Path2D.Float (aka GeneralPath)‏ –an abstract class so it can be extended  Area –provides constructive area geometry

3 Shape Interface  The Graphics2D class has methods for drawing and filling Shape objects  Shape is an interface –it has lots of methods  You can implement the interface to create your own Shape class

4 Shape Interface public boolean contains(Rectangle2D rect) public boolean contains(Point2D point) public boolean contains(double x, double y) public boolean contains(double x, double y, double w, double h) public Rectangle getBounds() public Rectangle2D getBounds2D() public PathIterator getPathIterator(AffineTransform at) public PathIterator getPathIterator(AffineTransform at, double flatness) public boolean intersects(Rectangle2D rect)‏ public boolean intersects(double x, double y, double w, double h)

5 A Custom Primitive  The old-fashioned way  Cannot extend the final class GeneralPath  Instead, you can create a wrapper for GeneralPath public class Heart implements Shape { GeneralPath path; public Heart(float x, float y, float w, float h) { path = new GeneralPath(); …

6 A Custom Primitive  Path2D is an abstract class that implements shape –Path2D.Float and Path2D.Double are concrete subclasses  Create new classes using inheritance public class Heart extends Path2D.Double { public Heart(float x, float y, float w, float h) { /* use lineTo, curveTo,etc to build path */

7 Other kinds of Path shapes  For parametric curves, use a loop to compute successive points and do lineTo  Example: Try creating a class to represent a general spirograph curve  Example: superellipse

8 Making Regular Polygons  You could extend the Polygon class and have the constructor generate the vertices for a regular polygon with the desired number of sides

9 Custom Areas  You can also extend the Area class to create a custom shape  See the HeartArea example in the ~tcole/teaching/cs221/demo/ch4 directory on onyx

10 Custom Objects  What do you do if you want to draw objects that consist of multiple shapes and/or curves with specific colors?  Create a class with a method that draws the object. –the method should have a Graphics or Graphics2D parameter –call this method from your paintComponent method

11 Image Processing  AWT – push model  Java 2D – immediate model  JAI – pull model

12 Image Processing Models  push model - source of image controls the loading –e.g. an image coming across the network –AWT classes  immediate model - image data is immediately available –Java2D classes –BufferedImage  pull model – used by Java Advanced Imaging API

13 BufferedImage  An image consists of a set of pixels –each pixel has a value associated with it  A BufferedImage contains –Raster -- holds the pixel values –ColorModel -- specifies how to interpret pixel values  BufferedImage has a create Graphics method which allows you to draw to the image

14 BufferedImage (Java2D)‏ BufferedImage bi = new BufferedImage(300, 400, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)(bi.createGraphics()); g2.drawImage(bi, 0, 0, new Component() {}); BufferedImage bi = ImageIO.read(file); Create Read Display

15 Image Types for BufferedImage  BufferedImage has a set of constants to represent different ways of storing color values –TYPE_INT_RGB –TYPE_BYTE_INDEXED –TYPE_BYTE_GRAY

16 ColorModel  An abstract class with methods for translating pixel values into colors plus alpha value –DirectColorModel - RGB colors –IndexedColorModel - for color maps

17 Filtering Images  BufferedImageOp is an interface used for filtering operations  Methods –BufferedImage createCompatibleImage( BufferedImage, ColorModel)‏ –BufferedImage filter(BufferedImage src, BufferedImage dest)‏ –Rectangle2D getBounds2D( BufferedImage src)‏ –Point2D getPoint2D( Point2D srcPt, Point2D destPt)‏

18 BufferedImageOp Implementers  RescaleOp - scale pixel values linearly  LookupOp - use a table to convert pixel values  ConvolveOp - convolute pixel value with thos of neighbors  ColorConvertOp - pixel-by-pixel color conversion  AffineTransformOp - apply the specified transformation to each pixel  Define a custom filter

19 ConvolveOp  Output pixel is a weighted sum of the values of the pixels in some window surrounding the pixel location –use a 3x3 window  A particular convolution is defined by a 3x3 kernel matrix  Common convolutions include smoothing, sharpening and edge detection

20 Example Convolutions  Smoothing averages all the pixels in the window. –reduces contrast  Sharpening(edge detection) accentuate differences in neighboring pixel values 1/ (4)‏ 0 0

21 Low-Level Pixel Manipulation  The Raster class stores an array of pixel values –You can read data for individual pixels but not change them  The Roster class has a subclass called WritableRaster –This class has methods that allow you to change individual pixels and groups of pixels

22 Raster and WritableRaster int[] getPixel(int x, int y, int[] data)‏ float[] getPixel(int x, int y, float[] data)‏ double[] getPixel(int x, int y, double[] data)‏ int[] getPixels(int x, int y, int w, int h, int[] data)‏ float[] getPixels(int x, int y, int w, int h, float[] data)‏ double[] getPixels(int x, int y, int w, int h, double[] data)‏ void setPixel(int x, int y, int[] data)‏ void setPixel(int x, int y, float[] data)‏ void setPixel(int x, int y, double[] data)‏ void setPixels(int x, int y, int w, int h, int[] data)‏ void setPixels(int x, int y, int w, int h, float[] data)‏ void setPixels(int x, int y, int w, int h, double[] data)‏

23 Fractals  Fractals are self-similar structures –look the same on all scales –useful for describing many natural phenomena (coastlines)‏  Koch-Snowflake, Sierpinski Gasket  Mandelbrot and Julia sets  Fractals are often used in computer graphics

24 Raster Manipulation  We are going to plot the Mandelbrot set as an example of writing directly to an image raster  To understand the program, we need a little background –complex numbers –bitwise operators

25 Complex Numbers  Complex numbers are numbers which include numbers like i which is the square root of -1  A complex number has two parts z = x + i y  We visualize the complex plane just like a 2D space with the real part along the x axis and the imaginary part along the y axis

26 Complex Arithmetic  The standard arithmetic operations are defined for complex numbers –z 1 + z 2 = (x 1 + x 2 ) + i (y 1 + y 2 )‏ –z 1 * z 2 = (x 1 x 2 - y 1 y 2 ) + i (x 1 y 2 + y 1 x 2 )‏  The magnitude of a complex number is –((x + i y)(x - i y)) 1/2 = (x 2 + y 2 ) 1/2

27 Bitwise Operators  Bitwise operators combine two integers using bit-by-bit logical operations & bitwise AND - use to mask bits | bitwise OR >> binary operator to shift bits right << shift bits left ^ exclusive or (XOR)‏ ~ complement

28 Mandelbrot Set  Consider the following iterative equation –z n+1 = z n 2 + cwhere c is a complex constant  The Mandelbrot set is the set of values of c for which this iteration sequence is bounded –if the magnitude of z ever exceeds 2 for a given c, that value of c is not in the set

09/19/08 Animation  To make the content of a panel dynamic, we can animate it  An animation consists of a series of rendered images (called frames)  Frame rate should be at least 60 frames per second to make the animation smooth  Frame rate is limited by the time it takes to render the image

09/19/08 Animation Process  You need a way to make something happen at regular intervals –Change some parameter used by the paintComponent method –call repaint() or revalidate() to cause paintComponent to be called

09/19/08 Two Approaches  Create a separate thread of execution to handle changes with time –Swing components are not thread-safe so direct manipulation of the component is not a good idea –Animation logic is performed in a separate thread  Use the Timer class

09/19/08 Threads  Threads allow a program to divide itself into parts that run independently and simultaneously –threads are not separate processes  The run-time system for Java always has multiple threads running  Create new threads by extending the Thread class or implementing the Runnable interface –Since our classes already extend another class, use the Runnable interface

09/19/08 Implementing Runnable public class PanelClass extends JPanel implements Runnable { public PanelClass() { //create and start thread } public void run() {…} }

34 Animation: Multi-threading Outline of a typical multi-threading animation public void paintComponent(Graphics g) { } public void run() { while(true) { repaint(); try { Thread.sleep(sleepTime); } catch (InterruptedException ex) {} }

09/19/08 Timer class  Timer is a Swing Component –delay can be set –generates an ActionEvent after each delay period –register a Listener to respond to the event

36 Animation: Timer Timer timer = new Timer(period, listener); timer.start(); public void actionPerformed(ActionEvent event) { } Outline of an animation with Timer

37 Cellular Automata The iteration of the system proceeds by assigning the next state of each cell based on the previous configuration. Each cell follows the same set of rules and the new state depends only upon the current states of the same cell and its neighbors. Example: A cell is black if exactly one of its neighbors in the current configuration is black

09/19/08 Conway's Game of Life  Birth: a dead cell becomes live if it has exactly 3 live neighbors  Survival: a live cell remains live if it has 2 or 3 neighbors  Death: a live cell with less than 2 or more than 3 neighbors dies

09/19/08 Rendering to Output Devices  Tha Java2D API was defined to do device- independent rendering  Provides access to three output devices –Screen using a Component like JPanel –Image using a BufferedImage –Printer using Printable interface and PrintJob class

40 Classes used in Printing  PrinterJob –getPrintJob –printDialog –setPrintable –print  Printable –Print  PageFormat –getOrientation –getWidth –getHeight –getImageableX –getImageableY –getImageableWidth –getImageableHeight