Title: Agenda For Feb 23
1Agenda For Feb 23
1. PowerPoint Presentation on Colors and
Polygons
2. Finish up Unit 3 Exercises on page 20
- 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.
4. Unit 4 Exercises on page 33. Question 2
2Using 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.
3Creating Your Own Color
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
- 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 new Color (255, 0, 0)
screen.setColor (myColor)
4RGB Colors
- These three parameters can be any integer
between and including 0 to 255. The 255s in the
images above mean I want a particular color to be
turned on full blast and the zeros mean I dont
want to add any of that particular color. What
color would the RGB combination (255, 0, 255)
make?
5Drawing Polygons
- So far weve 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 isnt 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 thats
the price you pay for being able to customizing
your program so that it may better suite the
needs of your users.
6Drawing Your Own Shapes
- Drawing your own shape requires four steps.
1. Declare a polygon variable.
2. Initialize your 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
7Initializing 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()
8Adding 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)
9Drawing Your Polygon
4. Now finally you can draw your polygon using
the method fillPolygon or drawPolygon.
public void paint (Graphics screen) screen.fill
Polygon (myPoly)
10The Whole Program
import java.applet. public class HelloWorld
extends Applet
Polygon myPoly
public void init() myPoly new Polygon()
mypoly.addPoint (10, 10)
mypoly.addPoint (110, 10)
mypoly.addPoint (60, 100)
public void paint(Graphics screen)
screen.fillPolygon (myPoly)
11mypoly.addPoint (110, 10)
screen.fillPolygon (myPoly)
mypoly.addPoint (10, 10)
mypoly.addPoint (60, 100)
screen.drawPolygon (myPoly2)