Barb Ericson Georgia Institute of Technology September 2005

Slides:



Advertisements
Similar presentations
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Advertisements

Cosc 5/4730 Blackberry Drawing. Screen size With Blackberry devices, they have a known screen size in pixels. If you are programming for specific device.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
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.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Drawing in Java part 1 Barb Ericson Georgia Institute of Technology September 2006.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
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.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Drawing-Turtle-and-AWT1 Drawing in Java Barb Ericson Georgia Institute of Technology Sept 21, 2009.
Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Creating Your Own Widgets CompSci 230 S Software Construction.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Drawing in Java Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Georgia Institute of Technology Drawing in Java – part 1 Barb Ericson Georgia Institute of Technology September 2005.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
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
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Creating Your Own Widgets
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Java Graphics CS 2511.
Barb Ericson Georgia Institute of Technology Dec 2009
Barb Ericson Georgia Institute of Technology September 2005
Chapter 4 Interface Types and Polymorphism Part 1
Workshop for Programming And Systems Management Teachers
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.
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology April 2006
Workshop for Programming And Systems Management Teachers
Two-Dimensional Arrays and Nested Loops – part 2
Chapter 49 Java 2D.
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Creating and Modifying Text part 3
Handout-14 Applets and Graphics
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Barb Ericson Georgia Institute of Technology September 2005 Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2005 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level What inheritance means How to do a general scale method using the java.awt.geom.AffineTransform class How to draw with a gradient paint Paint that changes from one color to another What an interface is used for How to clip a picture to a shape Georgia Institute of Technology

Georgia Institute of Technology Inheritance Class Graphics2D inherits from Graphics It inherits fields and methods Graphics has a drawImage method Graphics2D inherits this method from Graphics The API shows the parent class And the inherited methods Look in package java.awt and then at the class Graphics2D http://java.sun.com/j2se/1.5.0/docs/api/index.html Georgia Institute of Technology

Georgia Institute of Technology General Scaling We scaled a picture up or down But what if we want to scale to a specified size? Or what if we want to scale up in x and down in y? We can use the class AffineTransform in the java.awt.geom package Create an object of the class AffineTransform Set up the scaling using the method scale Use the AffineTransform object when you draw the image Georgia Institute of Technology

Georgia Institute of Technology General Scale Method public Picture scale(double xFactor, double yFactor) { // set up the scale transform AffineTransform scaleTransform = new AffineTransform(); scaleTransform.scale(xFactor,yFactor); // create a new picture object that is the right size Picture result = new Picture((int) (getWidth() * xFactor), (int) (getHeight() * yFactor)); // get the graphics 2d object to draw on the result Graphics graphics = result.getGraphics(); Graphics2D g2 = (Graphics2D) graphics; // draw the current image onto the result image scaled g2.drawImage(this.getImage(),scaleTransform,null); return result; } Georgia Institute of Technology

Testing General Scale Method Notice that the general scale method creates and returns a new picture of the appropriate size So to see the result we need to save a reference to it in a variable String file = Picture(FileChooser.getMediaPath("mattDoor.jpg"); Picture p = new Picture(file); Picture p1 = p.scale(2.0,0.5); p1.show(); Georgia Institute of Technology

Drawing with a Gradient Paint Point 1, Color 1 Instead of filling with one color you can fill with a paint that changes from one color to another java.awt.GradientPaint Create by specifying a point and the color at that point and then a second point and the color at that point. There will be a change from one color to the other Point 2, Color 2 Georgia Institute of Technology

Georgia Institute of Technology The drawSun Method public void drawSun(int x, int y, int width, int height) { // get the graphics2D object for this picture Graphics g = this.getGraphics(); Graphics2D g2 = (Graphics2D) g; // create the gradient for painting from yellow to red with // yellow at the top of the sun and red at the bottom float xMid = (float) (width / 0.5 + x); GradientPaint gPaint = new GradientPaint(xMid, y, Color.yellow, xMid, y + height, Color.red); // set the gradient and draw the ellipse g2.setPaint(gPaint); g2.fill(new Ellipse2D.Double(x,y,width,height)); } Georgia Institute of Technology

Georgia Institute of Technology Testing drawSun String file = FileChooser.getMediaPath(“beach.jpg”); Picture p = new Picture(file); p.drawSun(201,80,40,40); p.show(); Georgia Institute of Technology

Georgia Institute of Technology Paint is an Interface Look up the API for Graphics2D Find the setPaint method Notice that it takes a Paint object as a parameter How come we can pass this method a java.awt.Color object or a java.awt.GradientPaint object? They both implement the Paint interface Objects can be passed to a method that requires an object of an interface type As long as the object is from a class that implements that interface Or inherits from a class that implements the interface Georgia Institute of Technology

Georgia Institute of Technology Why Use an Interface? A USB interface lets you plug in different devices Camera, disk drive, key drive, etc The computer doesn’t care what the device is Just that it uses the USB interface Java interfaces are the same They let you plug in different classes as long as they implement the interface This means the implementing class must include all the methods defined in the interface Georgia Institute of Technology

Georgia Institute of Technology Clipping to a Shape You can specify a shape to clip the image to when you draw it Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,width,height); g2.setClip(ellipse); And only the portion of the image that is inside that shape will be drawn g2.drawImage(this.getImage(),0,0,width, height,null); Georgia Institute of Technology

Clipping to an Ellipse Method public Picture clipToEllipse() { int width = this.getWidth(); int height = this.getHeight(); Picture result = new Picture(width,height); // get the graphics2D object Graphics g = result.getGraphics(); Graphics2D g2 = (Graphics2D) g; // create an ellipse for clipping Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,width,height); // use the ellipse for clipping g2.setClip(ellipse); // draw the image g2.drawImage(this.getImage(), 0,0,width, height,null); // return the result return result; } Georgia Institute of Technology

Testing clipToEllipse This method creates a new picture and returns it so in order to see if we will need to save a reference to it String file = FileChooser.getMediaPath(“beach.jpg”); Picture p = new Picture(file); Picture p2 = p.clipToEllipse(); p2.show(); Georgia Institute of Technology

Georgia Institute of Technology Clipping Exercise Write a method that will clip a picture to a star You can use the java.awt.geom.GeneralPath class to create the path to clip to You can create a new GeneralPath passing it a Line2D.Double object You can append more Line2D.Double objects The method is append(Shape shape, boolean connectFlag); A Line2D.Double object is a Shape object. Shape is an interface. You want to pass true for the connect flag. Georgia Institute of Technology

Georgia Institute of Technology Summary Inheritance means that the child class gets all the fields and methods of the parent class See this in the API for Graphics2D You can use a class to do general scaling and rotation java.awt.geom.AffineTransform You can draw with a gradient paint That changes from one color to another Objects of classes that implement an interface can be said to be of that type And can be passed as parameters to methods that take that type You can clip a picture to a geometric object Or a path Georgia Institute of Technology