Open BlueJ --> open Walker --> save as Walker2 (we are copying
this into a new project!)
Run the "Sampler" applet
Open the "Sampler" applet and let's examine the code
Experiment with the following methods inside of the PAINT method:
- drawLine(int x1, int y1, int x2, int y2)
Draws a line, using the current color, between the points (x1, y1) and
(x2, y2).
- drawOval(int x, int y, int width, int height)
Draws the outline of an oval.
- drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle.
- fillArc(int x, int y, int width, int height, int startAngle,
int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
- fillOval(int x, int y, int width, int height)
Fills an oval bounded by the specified rectangle with the current color.
-----------------------------------------------------
DISCUSS
- The use of methods to simply repetition
- Mixing colors
- The value of using variables to track an object's location.
GETTING TO THE FUN PART
Decide on what your walker will be (what do you want to draw?)
Sketch it on graph paper.
Be DETAILED! Use a straight-edge and keep it to scale.
Have me approve it.
--------------------------------FOR NEXT CLASS---------------------------------
Draw your walker on graph paper and have it okayed.
•Begin to program your Walker in the draw() method
–break Walker into parts and have a separate method for each.
–comment your code well, so it is clear what parts you are drawing
--Start Walker 3. |
TIPS, TRICKS, AND TIDBITS
Changing the Font
You need to import the Font class
import java.awt.Font;
Create a new font:
Font f = new Font("TimesRoman",Font.BOLD,36);
Change it using setFont:
g.setFont(f); // g is Graphics object
|
Creating Polygons
You can either draw the outline of a polygon or
fill one. Both require an array of coordinates. Use both the API
and the sample code below to experiment.
int[] x = {175,50,150,175,200,300};
int[] y = {50,250,250,200,250,250};
g.fillPolygon(x,y,6); |
Changing the Color
Colors in Java are based upon the RGB levels. You can use the code
below to create new colors. Visit here
for RGB combinations. Make sure the Color class has been imported.
private Color olive = new
Color(107,142,35);
|
Adding a Pause to your Animations
/**
* Used between drawing the different Walkers to provide
* animation
*/
private static void pause (int wait)
{ long timeToQuit = System.currentTimeMillis() + wait;
while (System.currentTimeMillis() < timeToQuit)
{ } // take no action
} //======================
Include this method in your class.
To use it, just write pause(some number);
|
|
|