1.00 Lecture 18 Geometric Transformations in the 2D API.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 17: Lab: The Graphics 2D API 1 Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Web Design & Development Lecture 19. Java Graphics 2.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
COMPUTER GRAPHICS 2D TRANSFORMATIONS.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Computer Graphics Lecture 4 Geometry & Transformations.
Geometric Transformations
1 Computer Graphics Chapter 6 2D Transformations.
Chapter 5 Geometric Transformations
Linear Algebra and SVD (Some slides adapted from Octavia Camps)
1 CSCE 441 Computer Graphics: 2D Transformations Jinxiang Chai.
2D Transformations x y x y x y. 2D Transformation Given a 2D object, transformation is to change the object’s Position (translation) Size (scaling) Orientation.
2D Transformations Unit - 3. Why Transformations? In graphics, once we have an object described, transformations are used to move that object, scale it.
C O M P U T E R G R A P H I C S Stuff CMSC 435 / 634 Transformations 1/29 Geometric Transformations Readings: Chapters 5-6.
Geometric Transformation. So far…. We have been discussing the basic elements of geometric programming. We have discussed points, vectors and their operations.
Geometric Transformations Jehee Lee Seoul National University.
Dx = 2 dy = 3 Y X D Translation A translation is applied to an object by repositioning it along a straight-line path.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Transformations Jehee Lee Seoul National University.
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
CompSci Graphics. CompSci The Plan  Hardware  Coordinate System  Built-in Shapes  User-defined Shapes  Sprites and Shapes  Making a.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
 Definition: The ability to derive child classes (sub- classes) from parent classes (super-classes)  Characteristics:  Methods and instance variables.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Multiple Transformations Does the order in which two transformations are performed affect the final image? Watch as we draw ABC with vertices A(1, 1),
Computer Graphics 2D Transformations. 2 of 74 Contents In today’s lecture we’ll cover the following: –Why transformations –Transformations Translation.
1 Computer Graphics Week9 -3D Geometric Transformation.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
16/5/ :47 UML Computer Graphics Conceptual Model Application Model Application Program Graphics System Output Devices Input Devices API Function.
Two-Dimensional Geometric Transformations ch5. 참조 Subjects : Basic Transformations Homogeneous Coordinates Composite Transformations Other Transformations.
Geometric Transformations
Multimedia Programming 07: Image Warping Keyframe Animation Departments of Digital Contents Sang Il Park.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
Geometric Transformations Sang Il Park Sejong University Many slides come from Jehee Lee’s.
Chapter 14 Image Warping. Why do image warping? Looks cool! Can correct for optical distortion (i.e. keystoning). Remote Sensing (matching together multiple.
Affine Geometry.
Learning Objectives Affine transformations Affine transformations Translation Translation Rotation Rotation Scaling Scaling Reflection Reflection Shear.
1 By Dr. HANY ELSALAMONY.  We have seen how to create models in the 3D world. We discussed transforms in lecture 3, and we have used some transformations.
Jinxiang Chai CSCE441: Computer Graphics 3D Transformations 0.
1 Teaching Innovation - Entrepreneurial - Global The Centre for Technology enabled Teaching & Learning, N Y S S, India DTEL DTEL (Department for Technology.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Geometric Transformations Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
CSCE 441 Computer Graphics: 2D Transformations
Modeling Transformation
Transformations University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2016 Tamara Munzner.
Barb Ericson Georgia Institute of Technology September 2005
Forward Projection Pipeline and Transformations CENG 477 Introduction to Computer Graphics.
Lecture 10 Graphics D&D 3.14, 4:10, 5.13, 6.15, 8.16, Date.
Geometric Transformations Hearn & Baker Chapter 5
2D Transformations By: KanwarjeetSingh
Computer Graphics Transformations.
2D Imaging and Transformation
Graphics.
Computer Graphics Transformations.
Introduction to Computer Graphics CS 445 / 645
Lecture 7 Geometric Transformations (Continued)
Computer Graphics Transformations
Line and Character Attributes 2-D Transformation
Multiple Transformations
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.
null, true, and false are also reserved.
Drawing in Java.
7.5 Glide Reflections & Compositions
Transformations 2 University of British Columbia
TWO DIMENSIONAL TRANSFORMATION
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

1.00 Lecture 18 Geometric Transformations in the 2D API

Transformed Coordinates in NgonApp

Pixel Coordinates in NgonApp static final float SCALE=200.0F; static final float tx = 1.5F; static final float ty = 1.5F; float transX( float x ) { return ( x + tx ) * SCALE; } float transY( float y ) { return ( y + ty ) * SCALE; }

Transformations in the Cardioid Grapher

Affine Transformations The 2D API provides strong support for affine transformations. An affine transformation maps 2D coordinates so that the straightness and parallelism of lines are preserved. All affine transformations can be represented by a 3x3 floating point matrix. There are a number of “primitive” affine transformations that can be combined.

Scaling S x 0 0 x S x *x 0 S y 0 y = S y *y

Scaling Notes Basic scaling operations take place with respect to the origin. If the shape is at the origin, it grows. If it is anywhere else, it grows and moves. s x, scaling along the X dimension, does not have to equal s y, scaling along the y. For instance, to flip a figure vertically about the x- axis, scale by s x =1, s y =-1

Reflection as Scaling x x y = -y

Translation =

Rotation =

Composing Transformations Suppose we want to scale point (x, y) by 2 and then rotate by 90 degrees. rotate scale

Composing Transformations, 2 Because matrix multiplication is associative, we can rewrite this as

Composing Transformations, 3 Because matrix multiplication does not regularly commute, the order of transformations matters. This squares with our geometric intuition. If we invert the matrix, we reverse the transformation.

Transformations and the Origin When we transform a shape, we transform each of the defining points of the shape, and then redraw it. If we scale or rotate a shape that is not anchored at the origin, it will translate as well. If we just want to scale or rotate, then we should translate back to the origin, scale or rotate, and then translate back.

Transformations and the Origin, 2

Transformations in the 2D API Transformations are represented by instances of the AffineTransform class in the java.awt.geom package. Build a compound transform by 1. Creating a new instance of AffineTransform 2. Calling methods to build a stack of basic transforms: last in, first applied: –translate(double tx, double ty) –scale(double sx, double sy) –rotate(double theta) –rotate(double theta, double x, double y) rotates about (x,y)

Transformation Example baseXf = new AffineTransform(); baseXf.scale( scale, -scale ); baseXf.translate( -uRect.x, -uRect.y ); If we now apply baseXF it will translate first, then scale. Remember in Java® that transforms are built up like a stack, last in, first applied.

Back to Cardioid Let’s build our coordinate system: public class Cardioid extends JFrame { private CardioidGraph graph; private Rectangle2D.Float uSpace; public static void main( String [] args ) { Rectangle2D.Float uS = new Rectangle2D.Float(-1.5F,1.5F,4.0F,3.0F); Cardioid card = new Cardioid( uS ); card.setSize( 640, 480 ); card.setVisible( true ); }

Cardioid Cartesian Space Rectangle2D.Float(-1.5F,1.5F,4.0F,3.0F) represents the area of Cartesian coordinates in which we will draw our graph:

CardioidGraph public Cardioid( Rectangle2D.Float uS ) { uSpace = uS; graph = new CardioidGraph( uSpace );... public class CardioidGraph extends GraphPanel implements ActionListener { public CardioidGraph( Rectangle2D.Float uR ) { super( uR );...

GraphPanel public class GraphPanel extends JPanel { protected Rectangle2D.Float uRect; protected AffineTransform baseXf = null; protected Dimension curDim = null; protected double scale; private GeneralPath axes = null; public GraphPanel( Rectangle2D.Float uR ) { uRect = (Rectangle2D.Float) uR.clone(); }

GraphPanel, paintComponent() public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g; if ( ! getSize().equals( curDim ) ) doResize(); drawAxes( g2 ); drawGrid( g2 ); }

GraphPanel, doResize() private void doResize() { curDim = getSize(); hScale = curDim.width / uRect.width; vScale = curDim.height / uRect.height; scale = Math.min( hScale, vScale ); baseXf = new AffineTransform(); baseXf.scale( scale, -scale ); baseXf.translate( -uRect.x, -uRect.y ); axes = createAxes(); grid = createGrid(); }

Creating and Drawing the Axes private GeneralPath createAxes() { GeneralPath path = new GeneralPath(); path.moveTo( uRect.x, 0F ); path.lineTo( uRect.x + uRect.width, 0F ); path.moveTo( 0F, uRect.y ); path.lineTo( 0F, uRect.y - uRect.height ); return path; } private void drawAxes( Graphics2D g2 ) { g2.setPaint( Color.green ); g2.setStroke( new BasicStroke(3 ) ); g2.draw(baseXf.createTransformedShape(axes)); }

Initializing CardioidGraph

Initializing CardioidGraph,2 public CardioidGraph( Rectangle2D.Float uR ) { super( uR ); diam = uRect.width / 4; hub = new Ellipse2D.Float(0F, -diam/2, diam, diam); tick = uRect.width / 40; wheel = new GeneralPath(); Shape s = new Ellipse2D.Double(diam, -diam/2,diam, diam); wheel.append( s, false ); wheel.moveTo( 2*diam, 0F ); wheel.lineTo( 2*diam + tick, 0F ); }

Timers Swing provides a utility class called Timer that makes it simpler to build animations. Timers tick at an interval you can set, and the ticks are reported as ActionEvents. public class TimerUser implements ActionListener { private Timer timer = null; private int tIval = 100; // interval in milliseconds public TimerUser() { timer = new Timer( tIval, this ); } public void start() { timer.start(); } public void actionPerformed( ActionEvent e ) { /*do repeated action on every tick*/ }

Timer Methods Timer( int tickMillis, ActionListener l ) void start() void stop() boolean isRunning() void setRepeats(boolean repeats) boolean isRepeats() void setCoalesce(boolean coalesce) boolean isCoalesce()

CardioidGraph, start() public void start() { if ( timer != null ) { timer.stop(); } timer = new Timer( tIval, this ); curve = new GeneralPath(); curve.moveTo( 2F, 0F ); tCount = 0; repaint(); timer.start(); }

CardioidGraph, actionPerformed() public void actionPerformed( ActionEvent e ) { tCount++; double theta = tCount * Math.PI / 180; double sint = Math.sin( theta ); double cost = Math.cos( theta ); double cx = cost + cost*cost; double cy = sint + sint*cost; curve.lineTo( (float)cx, (float) cy ); if ( tCount >= 360 ) { timer.stop(); timer = null; } repaint(); }

CardioidGraph, paintComponent() public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g; drawHub( g2 ); drawCurve( g2 ); drawWheel( g2 ); }

CardioidGraph, drawCurve() private void drawCurve( Graphics2D g2 ) { if ( curve == null ) return; // make curve translucent Composite c = g2.getComposite(); Composite hc = AlphaComposite.getInstance( AlphaComposite.SRC_OVER,.5F ); g2.setComposite( hc ); g2.setPaint( Color.red ); g2.setStroke( new BasicStroke( 2 ) ); g2.draw( baseXf.createTransformedShape( curve ) ); g2.setComposite( c ); }

Geometry of the Cardioid

CardioidGraph, drawWheel() private void drawWheel( Graphics2D g2 ) { Composite c = g2.getComposite(); Composite hc = AlphaComposite.getInstance( AlphaComposite.SRC_OVER,.5F ); g2.setComposite( hc ); g2.setPaint( Color.orange ); g2.setStroke( new BasicStroke( 2 ) ); double theta = tCount * Math.PI / 180; AffineTransform whlXf = new AffineTransform(baseXf); whlXf.rotate( theta, diam/2, 0.0 ); whlXf.rotate( theta, 3*diam/2, 0.0 ); g2.draw( whlXf.createTransformedShape( wheel ) ); g2.setComposite( c ); }