Java 2D Graphics. Drawing Primitives, Affine Transformations, Scene Graphs, Hit Tests. Drawing Primitives 2/9/2014. Using it is simple.

Similar documents
Graphics Transformations

Affine Transformations. Transforming shape models Combining affine transformations Scene graphs, interactor trees Hit tests on transformed shapes

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Graphics Hit-testing. Shape Models Selecting Lines and Shapes. 2.7 Graphics Hit-testing 1

CIS 162 Project 1 Business Card Section 04 (Kurmas)

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Topics. Transient vs Persistent Representation of Objects. Object Serialization CSE Lecture 9 Inheritance, III

Week 4 Part 2. Introduction to 2D Graphics & Java 2D

Assignment 2. Application Development

(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci

Programmierpraktikum

2. (10 pts) Which of the following classes implements the Shape interface? Circle all that apply. a) Ellipse2D b) Area c) Stroke d) AffineTransform e)

TWO-DIMENSIONAL FIGURES

Overview. Java2D. Graphics in Java2D: Colour Images Fonts. The bigger picture of Java Graphics: Java Advanced Imaging (JAI) API Java3D

Lecture 7 A First Graphic Program And Data Structures & Drawing

Software System Components 1 Graphics

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Programming: You will have 6 files all need to be located in the dir. named PA4:

Object Orientated Programming in Java. Benjamin Kenwright

g2.drawline(146,miny,146,maxy); g2.drawline(293,miny,293,maxy); g2.drawline(145,miny,145,maxy); g2.drawline(292,miny,292,maxy);

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Java Coordinate System

Affine Transformation. Edith Law & Mike Terry

Letterkenny Institute of Technology

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Chapter 1 Introduction to Java

1. Complete these exercises to practice creating user functions in small sketches.

Using Graphics. Building Java Programs Supplement 3G

Icy Plugin Development. Training - Level 3

Graphics and Painting

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

Introduction to Programming Using Java (98-388)

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

3D Mathematics. Co-ordinate systems, 3D primitives and affine transformations

Building Java Programs

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CP122 Computer Science I. Chapter 2: Using Objects

Graphics in Swing. Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland

Building Java Programs

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Java Programming. Computer Science 112

Putting the 'Free' into JFreeChart

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Interactive Computer Graphics. Hearn & Baker, chapter D transforms Hearn & Baker, chapter 5. Aliasing and Anti-Aliasing

IT101. Graphical User Interface

public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D)g;... }

Dr. Hikmat A. M. AbdelJaber

Building Java Programs

UI Software Organization

Graphical User Interfaces 2

G51PRG: Introduction to Programming Second semester Applets and graphics

Computer Graphics: Geometric Transformations

03 Vector Graphics. Multimedia Systems. 2D and 3D Graphics, Transformations

Programming Languages and Techniques (CIS120)

Building Java Programs

AP CS Unit 12: Drawing and Mouse Events

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

AP Computer Science Unit 13. Still More Graphics and Animation.

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Class Meeting 05 (Lecture 04) Objectives for this class meeting. Conduct vote on basic style of game for class project

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Garfield AP CS. Graphics

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Lecture 5 2D Transformation

Bringing Life to Swing Desktop Applications

CSIS 10A Assignment 14 SOLUTIONS

CSCI 201L Midterm Written Fall % of course grade

Topic 9 More Graphics. Based on slides bu Marty Stepp and Stuart Reges from

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

Programming Graphics (P1 2006/2007)

Swing Rocks - A Tribute to Filthy Rich Clients

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Lecture 3: Java Graphics & Events

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

Object Oriented Programming

Reviewing OO Concepts

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java2D/Java3D Graphics

Topic 8 graphics. -mgrimes, Graphics problem report 134

Lecture 5: Java Graphics

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Windows and Events. created originally by Brian Bailey

Core Graphics and OpenGL ES. Dr. Sarah Abraham

Programming graphics

Programming Languages and Techniques (CIS120)

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Rendering with Java. Jim Graham Staff Engineer JavaSoft

CS 335 Graphics and Multimedia. Image Manipulation

Chapter 2 Using Objects

AP CS Unit 11: Graphics and Events

User interfaces and Swing

Chapter 24. Graphical Objects The GraphicalObject Class

Computer Science 336 Fall 2017 Homework 2

(SSOL) Simple Shape Oriented Language

UNIT 2 2D TRANSFORMATIONS

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Graphics -- To be discussed

CS380: Computer Graphics 2D Imaging and Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

Transcription:

Java 2D Graphics Drawing Primitives, Affine Transformations, Scene Graphs, Hit Tests Drawing Primitives Graphics - Abstract Base Class that supports basic drawing and rendering - Conceptually sim. to WatGUI Graphics object - Provides methods to draw primitives http://docs.oracle.com/javase/7/docs/api/java/awt/graphics.html Using it is simple - Instantiate a canvas (JPanel) - Override PaintComponent(Graphics g) 2 1

Demo: SimpleDraw // JComponent is a base class for custom components public class SimpleDraw extends JComponent { public static void main(string[] args) { SimpleDraw canvas = new SimpleDraw(); JFrame f = new JFrame("SimpleDraw"); // jframe is app window f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(400, 400); // window size f.setcontentpane(canvas); // add canvas to jframe f.setvisible(true); // show the window // custom graphics drawing public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; // cast for 2D drawing methods g2.setstroke(new BasicStroke(32)); // 32 pixel thick stroke g2.setcolor(color.blue); // make it blue g2.drawline(0, 0, getwidth(), getheight()); // draw line g2.setcolor(color.red); g2.drawline(getwidth(), 0, 0, getheight()); 3 Java2D AffineTransform Class AffineTransform handles all matrix manipulations - A bit more control than Graphics2D Static Methods - static AffineTransform getrotateinstance(double theta) - static AffineTransform getrotateinstance(double theta, double anchorx, double anchory) - static AffineTransform getscaleinstance( double sx, double sy) - static AffineTransform gettranslateinstance( double tx, double ty) 4 2

Java2D AffineTransform Class Concatenation methods - void rotate(double theta), void rotate(double theta, double anchorx, double anchory) - void scale(double sx, double sy) - void translate(double tx, double ty) - void concatenate(affinetransform Tx) Other Methods - AffineTransform createinverse() - void transform(point2d[] ptsrc, int srcoff, Point2D[] ptdst, int dstoff, int numpts) * 5 Useful Graphics2D methods - AffineTransform gettransform(), void settransform(affinetransform Tx) Returns/sets a copy of the current Transform in the Graphics2D context. - void rotate(double theta), void rotate(double theta, double x, double y) Concatenates the current Graphics2D Transform with a rotation transform. Second variant translates origin to (x,y), rotates, and translates origin (-x, -y). - void scale(double sx, double sy) Concatenates the current Graphics2D Transform with a scaling transformation. Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling. - void translate(double tx, double ty) Concatenates the current Graphics2D Transform with a translation transform. 6 3

Demo: RotateLine.java public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D) g; // draw axes g2.translate(20, 240); g2.setstroke(new BasicStroke(3)); g2.setcolor(color.black); g2.drawline(0, 0, 0, - 200); // vertical axis g2.drawline(0, 0, 200, 0); // horizontal axis // draw a line w. ends along the x axis g2.setstroke(new BasicStroke(5)); g2.setcolor(color.red); g2.drawline(40, 0, 120, 0); g2.drawoval(40-4, - 4, 8, 8); g2.drawoval(120-4, - 4, 8, 8); 7 // rotate the line? Demo: AffineDemo.java 8 4

Shape Model an array of points: {P 1, P 2,, P n isclosed flag (shape is polyline or polygon) isfilled flag (polygon is filled or not) (and stroke thickness, colours, etc.) 9 Constructing Complex Shapes Construct instance of Polygon class from Point2D list g2.drawpolygon (x_points, y_points, points.size()); g2.drawpolyline (x_points, y_points, points.size()); public void paint(graphics2d g2) { g2.setcolor(color.yellow); g2.setstroke(new BasicStroke(12)); g2.drawpolygon(x_points, y_points, points.size()); 10 5

Demo: ShapeDemo, ShapeDemo2 MyShape.java Point2D.Double type haschanged flag // call right drawing function if (isfilled) g2.fillpolygon(x_points, y_points, points.size()); else if (isclosed) g2.drawpolygon(x_points, y_points, points.size()); else g2.drawpolyline(x_points, y_points, points.size()); 11 Scene Graphs Each part has a transform matrix Each part draws its children relative to itself 12 6

Inside/Hit Tests with Transformed Shapes Mouse and shape model must use same coordinate system Two options: - Transform mouse to model coordinates - Transform shapes to mouse coordinates 13 Transform Mouse to Model Coordinates Only one transformation Within 3 pixels of a line in screen coordinates is how far in model coordinates? Uniform scaling Maintaining the inverse 14 7

Transform Model to Mouse Coordinates Many transformations Manipulations (e.g. dragging) must be transformed back into model coordinates 15 Demo: ShapeDemo3 public boolean contains(graphics2d g, Point2D mouse) { AffineTransform inverse = null; try { inverse = g.gettransform().createinverse(); catch (NoninvertibleTransformException e) { System.out.print(e); // move mouse back to origin Point2D mouset = new Point2D.Float(); inverse.transform(mouse, mouset); // compare un- translated polygon to mouse coordinates Polygon p = new Polygon(x_points, y_points, points.size()); return p.contains(mouset.getx(), mouset.gety()); 16 8