Download presentation
Presentation is loading. Please wait.
Published byLynette Hodge Modified over 9 years ago
1
Agenda For Feb 23 2. Finish up Unit 3 Exercises on page 20 4. Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class). Criteria: Use lines for the hair (at least 5 lines) Use ovals for the eyes (fill the ovals with a color you create from scratch). The nose must be a polygon. Use at least one arc for the mouth. Your Happy Face must use at least 3 different colors. 1. PowerPoint Presentation on Colors and Polygons
2
Using Colors There are two ways you can set the current drawing color. You can use pre-defined Java colors or you can define your own color from scratch. This is how you can set the current drawing using the pre-defined Java color red. screen.setColor (Color.red); You can replace the word red with any other Java pre- defined color.
3
Creating Your Own Color You can also set the current drawing color using a color you created from scratch. This is how you can create and set your own drawing color from scratch: Color myColor;// goes above the init method myColor = new Color (100, 60, 255); // goes inside the init method screen.setColor (myColor); // goes inside the paint method The following is a list of the available pre-defined Java colors (found on page 27): red, blue, cyan, gray, darkGray, lightGray, green, magenta, orange, pink, white, yellow
4
These three parameters can be any integer between and including 0 to 255. The 255’s in the images above mean I want a particular color to be turned on full blast and the zeros mean I don’t want to add any of that particular color. What color would the RGB combination (255, 0, 255) make? RGB Colors (255,0,0)(0,0,255)(0,255,0)(0,255,255)
5
Drawing Polygons So far we’ve seen that Java provides us with pre-defined colors (red, green, etc) and shapes (circles, rectangles, etc). However, if the need arises we are able to make up our own color up from scratch. What happens when we need to create a shape that isn’t like any of the ones Java provides? You guessed it! We create our own shape from scratch. It takes more work because we have to define the shape ourselves, but that’s the price you pay for being able to customizing your program so that it may better suite the needs of your users.
6
Drawing Your Own Shapes Drawing your own shape requires four steps. 1. Declare a polygon variable. 3. Define the vertices of your new polygon. 4. Draw your polygon. 1. In your variable section declare a Polygon variable: public void init () { Polygon mypoly; } 2. Initialize your polygon variable.
7
Initializing Your Polygon 2. In the init method, initialize your variable. The textbook initializes this variable in the paint method (page 29). This is not a good idea because your applet program executes the paint method many times and there is no point in initializing the same variable over and over again. Variables only need to be initialized once. This is how you initialize the polygon variable within the init method. mypoly = new Polygon();
8
Adding Points To Your Polygon 3.Use the addPoint method to define all the point of your polygon (do this inside your init method). NOTE: The last point will automatically connect itself to the first point you defined. Your init method should now look like this: public void init() { myPoly = new Polygon();// step 2 mypoly.addPoint (10, 10); mypoly.addPoint (110, 10); mypoly.addPoint (60, 100); }
9
4.Now finally you can draw your polygon using the method fillPolygon or drawPolygon. public void paint (Graphics screen) { screen.fillPolygon (myPoly); } Drawing Your Polygon
10
The Whole Program import java.awt.*; import java.applet.*; public class HelloWorld extends Applet { public void init() { myPoly = new Polygon(); public void paint(Graphics screen) { screen.fillPolygon (myPoly); } } mypoly.addPoint (10, 10); mypoly.addPoint (110, 10); mypoly.addPoint (60, 100); Polygon myPoly; }
11
mypoly.addPoint (10, 10); mypoly.addPoint (110, 10); mypoly.addPoint (60, 100); screen.drawPolygon (myPoly2); screen.fillPolygon (myPoly);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.