|
Lab7d Animation, movement and buttons
(again)
Create Lab7d1, remove init and
paint methods
Paste this in the applet showing an animated circle.
|
// global variables private Image
dbImage; private Graphics dbg; int scene = 1;
public void init() { setSize(200,
150); setBackground(Color.lightGray); }//end init
public void
circle1(Graphics g, int x, int
y) { g.setColor(Color.gray); g.fillArc(x,y,30,30,0,120); g.setColor(Color.blue); g.fillArc(x,y,30,30,120,120); g.setColor(Color.yellow); g.fillArc(x,y,30,30,240,120); }//end circle1
public void
circle2(Graphics g, int x, int
y) { g.setColor(Color.gray); g.fillArc(x,y,30,30,120,120); g.setColor(Color.blue); g.fillArc(x,y,30,30,240,120); g.setColor(Color.yellow); g.fillArc(x,y,30,30,0,120); }//end circle2
public void
circle3(Graphics g, int x, int
y) { g.setColor(Color.gray); g.fillArc(x,y,30,30,240,120); g.setColor(Color.blue); g.fillArc(x,y,30,30,0,120); g.setColor(Color.yellow); g.fillArc(x,y,30,30,120,120); }//end circle3
public void update (Graphics g) { if
(dbImage == null) { dbImage = createImage
(this.getSize().width, this.getSize().height); dbg =
dbImage.getGraphics (); } dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width,
this.getSize().height); dbg.setColor (getForeground());
paint (dbg); g.drawImage (dbImage, 0, 0, this); } //end update
public void delay(int p)
{ try {Thread.sleep(p);} catch (InterruptedException
e){} }//end delay
public void paint(Graphics g)
{ if(scene == 1) { circle3(g,k,60); scene =
2; } else if(scene ==
2) {
circle2(g,k,60); scene
= 3; } else
{
circle1(g,k,60); scene
=
1; }
repaint(); delay(150);
}
// end of paint
|
Notice in this example, repaint and delay are not inside the nested
if.. elses - instead of giving each section its own repaint and delay I
used one set at the bottom - It does not compile yet. |
|
Moving horizontally in a wrap motion
1. Add this to the global variable section int k = 10;
2. Add this after the delay in paint - familiar wrap code
k += 4; if(k > 200)
k = -10;
|
Do you have any idea why i went circle3, circle2, circle1 ?
|
|
Adding Buttons to change directions
Now we are are going to add
Buttons from Unit 6 Lab6e - since there is room at the top, we will go
with the default FlowLayout
1. add with the import
section import java.awt.event.*;
2. add after the word applet
- at the top implements
ActionListener ( it
will not compile until step 5)
3. add with the global
variables
String motion = "right"; Button
b1,b2; |
4. add to the init
method after the
background color is set
setVisible(true); //Make Window
Visible
b1 = new
Button("Right"); b1.setBackground(Color.green); b1.addActionListener(this);
b2
= new Button("
Left"); b2.setBackground(Color.red); b2.addActionListener(this);
add(b1); add(b2); |
5. add this method
BEFORE the paint method - you
can compile and see the buttons
public void actionPerformed(ActionEvent e) { if
(e.getSource() == b1) {
motion =
"right"; repaint(); } //
end b1
if (e.getSource() ==
b2) { motion="left"; repaint(); }
// end b2 }// end of actionPerformed
|
6. This step is tricky - To make the
buttons work, we will need another if ... else inside
paint around the if.. else we already have
|
if(motion.equals("right")) { // all the code you
already have in paint
}// end of if else { //paste another copy
of the code but switch circle1 and circle3 // and change the
conditionals at the end
repaint(); } // end of else
|
Try it - it should now work
- call me if you are having problems --you should see this
|
|
Check list of what you should be familiar with
- Move an object across the screen horizontally or vertically in a
warpping or a bouncing style(Lab7a)
- Add buttons on the top (flow Layout) or on the bottom, top, right or
left sides with a panel and a Border Layout(Lab7b)
- Use the buttons to start and stop (Lab7b), use buttons to change
directions (Lab7d)
- Use nested if..else to create the impression of animation( Lab7c)
and (Lab7d)
| We are now going to use the buttons one more time to
do this |
|
1. Copy your Java file from Lab7d1. Close the
workspace
2. Create applet Lab7d2. Paste over the entire Java
file, change the name and compile. It should still work
3. Add these lines at the beginning of paint method.
g.setColor(Color.black); g.fillRect(180,0,3,150);
// finish line |
4. Erase the entire else section from
paint.
5. Change the message on the buttons. b1 should say start and b2 should say replay
6. I like my Strings to have meaning to me. a. I changed the value
of the global variable motion to "stop" at the top of the program b.
When b1 is clicked, inside the actionPerformed, change the assignment -
motion becomes "go" c.
Inside paint - change the if line to "go"
Compile and make sure the ball is stopped and rolls when you click
start.
7. To stop the ball when it gets to the line, we will need to make
changes. There are many ways to stop the ball from rolling. Here is a
simple way.
Remove the wrap code, the delay and the repaint. Paste this where the
removed lines where - read the comments.
if(k < 160) // if the
ball has not reached the line { k += 4; // add to k
repaint(); delay(100); } |
My ball rolls a little too far. Can you fix this problem?
8. To make the replay button to work, add this in the
conditional for b2
in the actionPerformed method. k =
1;// start k over again
Call me if you need help at this point.
Extra stuff: Bad things happen if you click the replay button while the
ball is rolling. Try it.
The sample turns the start and replay button off and on. I used these 2 lines
to accomplish that. You will need to figure out where to put them. The
replay button doesn't turn on till the ball crosses the line! Please see if you can figure where they
go - you will add them several times.
b1.setEnabled(false); and
b1.setEnabled(true);
b2.setEnabled(false); and
b2.setEnabled(true);
Knowing how to do this will be
worth points in the Unit project
|
|
Assignment
Complete Each lab below
You may use it for the Unit project later if you want to. There are
notes below the samples.
Crawling Bug Lab7d3

|
Running Man Lab7d4

|
Tank Lab7d5

|
1. Create each applet
2. In the bug, man and tank samples I have shown you
the 3 ( or 4) positions. Please leave those turned on (show the test
code) when you get the Lab graded the first time.
3. Use code from Lab7d2 to
complete the three labs.
| Points |
Description |
| 70 points |
drawn pics only |
| 80 points |
wrapping code to move the
objects (bug, man, tank) across the screen |
| 90 points |
Add a start button, a
replay button and a finish line
both buttons work... doesn't turn
on and off |
| 100 points |
Add a start button, a
replay button and a finish line
both buttons work... turns on and
off! You will need these for the Unit project
for Unit 7. Get your help now |
4. Here are some methods to help you get started.
|
| There are 3 bug methods--This is a shell code... you will need to change the drawOval to fillOval
after you have completed the 3 bug methods. I tested my pictures first to see how they looked
bug1(g,10,10);
bug2(g,90,60);
bug3(g,170,60);
public void bug1(Graphics g, int x, int y)
{
//legs left side
g.setColor(Color.green);
g.drawLine(x+20,y,x+25,y+30);//back leg
g.drawLine(x+35,y,x+25,y+30);//middle leg
g.drawLine(x+25,y+30,x+50,y);//front leg
//legs right side
g.setColor(Color.green);
g.drawLine(x+20,y+60,x+25,y+25);//back leg
g.drawLine(x+25,y+25,x+35,y+60);//middle leg
g.drawLine(x+25,y+30,x+50,y+55);//front leg
//body
g.setColor(Color.yellow);
g.drawOval(x,y+20,50,20);
} |
There are 4 man methods I tested my pictures first to see
how they looked man1(g,20,20);
man2(g,80,20);
man3(g,140,20);
man4(g,200,20);
public void
man1(Graphics g, int x, int
y) { g.setColor(Color.blue); g.fillOval(x+15,y+5,20,20); //body g.drawLine(x+25,y+25,x+25,y+45); //arms
g.drawLine(x+25,y+30,x,y+35); g.drawLine(x+25,y+30,x+50,y+35); //legs g.drawLine(x+25,y+45,x,y+70); g.drawLine(x+25,y+45,x+50,y+70);
} |
| There are 3 tank methods I tested my pictures first to see how they looked
tank1(g,10,50);
tank2(g,70,50);
tank3(g,130,50);
public void tank1(Graphics
g, int x, int y) { //body g.setColor(Color.gray); g.fillRoundRect(0+x,
10+y,50,30,20,20); g.setColor(Color.gray.darker()); g.fillOval(10+x,10+y,30,30); //top
wheel g.setColor(Color.black); g.fillRoundRect(10+x,
5+y,30,5,2,2); g.setColor(Color.red); g.fillRect(11+x,
5+y,10,5);
//bottom
wheel g.setColor(Color.black); g.fillRoundRect(10+x,
40+y,30,5,2,2); g.setColor(Color.red); g.fillRect(30+x,
40+y,9,5);
//death
ray g.setColor(Color.yellow); g.drawLine(25+x,25+y,
60+x,0+y); g.drawLine(25+x,25+y,60+x,1+y); g.drawLine(25+x,25+y,
60+x,2+y); g.drawLine(25+x,25+y, 60+x,3+y); }
|