Drawing in Java part 3 Barb Ericson Georgia
Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006 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. Affine. Transform 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
Inheritance • Class Graphics 2 D inherits from Graphics • It inherits fields and methods – Graphics has a draw. Image method – Graphics 2 D 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 Graphics 2 D – http: //java. sun. com/j 2 se/1. 5. 0/docs/api/index. html Georgia Institute of Technology
General Scaling • Scaling a picture is to make it larger or smaller – Scale up is larger – Scale down is smaller • We can use the class Affine. Transform in the java. awt. geom package – Create an object of the class Affine. Transform – Set up the scaling using the method scale – Use the Affine. Transform object when you draw the image Georgia Institute of Technology
General Scale Method public Picture scale(double x. Factor, double y. Factor) { // set up the scale transform Affine. Transform scale. Transform = new Affine. Transform(); scale. Transform. scale(x. Factor, y. Factor); // create a new picture object that is the right size Picture result = new Picture((int) (get. Width() * x. Factor), (int) (get. Height() * y. Factor)); // get the graphics 2 d object to draw on the result Graphics graphics = result. get. Graphics(); Graphics 2 D g 2 = (Graphics 2 D) graphics; // draw the current image onto the result image scaled g 2. draw. Image(this. get. Image(), scale. Transform, 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 = File. Chooser. get. Media. Path("matt. Door. jpg"); Picture p = new Picture(file); Picture p 1 = p. scale(2. 0, 0. 5); p 1. show(); Georgia Institute of Technology
Drawing with a Gradient Paint • Instead of filling with one color you can fill with a paint that changes from one color to another Point 1, Color 1 – java. awt. Gradient. Paint • 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 Georgia Institute of Technology Point 2, Color 2
The draw. Sun Method public void draw. Sun(int x, int y, int width, int height) { // get the graphics 2 D object for this picture Graphics g = this. get. Graphics(); Graphics 2 D g 2 = (Graphics 2 D) g; // create the gradient for painting from yellow to red with // yellow at the top of the sun and red at the bottom float x. Mid = (float) (width / 0. 5 + x); Gradient. Paint g. Paint = new Gradient. Paint(x. Mid, y, Color. yellow, x. Mid, y + height, Color. red); // set the gradient and draw the ellipse g 2. set. Paint(g. Paint); g 2. fill(new Ellipse 2 D. Double(x, y, width, height)); } Georgia Institute of Technology
Testing draw. Sun • String file = File. Chooser. get. Media. Path(“beach. jpg”); • Picture p = new Picture(file); • p. draw. Sun(201, 80, 40); • p. show(); Georgia Institute of Technology
Paint is an Interface • Look up the API for Graphics 2 D – Find the set. Paint 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. Gradient. Paint 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
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
Clipping to a Shape • You can specify a shape to clip the image to when you draw it – Ellipse 2 D. Double ellipse = new Ellipse 2 D. Double(0, 0, width, height); – g 2. set. Clip(ellipse); • And only the portion of the image that is inside that shape will be drawn – g 2. draw. Image(this. get. Image(), 0, 0, width, height, null); Georgia Institute of Technology
Clipping to an Ellipse Method public Picture clip. To. Ellipse() { int width = this. get. Width(); int height = this. get. Height(); Picture result = new Picture(width, height); // create an ellipse for clipping Ellipse 2 D. Double ellipse = new Ellipse 2 D. Double(0, 0, width, height); // use the ellipse for clipping g 2. set. Clip(ellipse); // get the graphics 2 D object Graphics g = result. get. Graphics(); Graphics 2 D g 2 = (Graphics 2 D) g; // draw the image g 2. draw. Image(this. get. Image(), 0, 0, width, height, null); // return the result return result; } Georgia Institute of Technology
Testing clip. To. Ellipse • 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 = File. Chooser. get. Media. Path(“beach. jpg”); Picture p = new Picture(file); Picture p 2 = p. clip. To. Ellipse(); p 2. show(); Georgia Institute of Technology
Clipping Exercise • Write a method that will clip a picture to a triangle – You can use the java. awt. geom. General. Path class to create the path to clip to – You can create a new General. Path passing it a • Line 2 D. Double object – You can append more • Line 2 D. Double objects 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 Graphics 2 D • You can use a class to do general scaling and rotation – java. awt. geom. Affine. Transform • 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
- Slides: 16