|
Lab6e The first problem is that you cannot use JButton in an applet. Graphics comes from the awt class, an early version of Java. JButtons, JTextfields, etc come from the swing class which appeared in a later version of Java. There is a JApplet that can use these but for the time being, we will use the early version - Button. The differences are minor
Create applet Lab6e1
4. Compile now to save, fix any errors. You have a bunch of little program that are of size 200 by 200 that use the delay method. Lab6a4, Lab6a5, (best one) Lab6b1, Lab6b2, etc... Lab6c3. Open another version of JCreator and find one that works. Try Lab6b1. 6. Add this method to Lab6e1. Do
not put it inside init or paint methods
7. Add this line to the paint method
myPic1(g,0,0);//the zeros are dummy
numbers for later use Now, we are ready to continue. | |||
|
Adding a Button The JButton required a container. The Button does not, so some of this will look different then what we did in the GUI's 1. Add this before the init
method: Button b1, b2,
b3;
3. We want the Button to appear below the graphics. The original size
was 200 by 200. This applet has space under the applet for the Button. If
you say nothing about a layout, you get the Button at the top. 4. Add this after the Button stuff - try it. the Button should appear on the bottom of the window.
Program works but button shows up kind of fun. | |||
|
Make the Button work 1. Add this to the import section at the
top import
java.awt.event.*;
Analysis of the problem 1. The picture draws before we click the button. We need a conditional to make it wait for a button click Add this declaration to the area where we declared the
Buttons int
pick = 0; // 0 means off
2. Compile and run. Now, nothing draws because when we click the
play Button, pick needs to
change value. Add this
| |||
|
more methods, moreButtons 1. Add another method. Call it myPic2. Add the loop
code from another of the Lab6b's or
Lab6c's. 3. Create the second Button. Easy is to copy the b1 stuff, paste and change it to b2 stuff. We know that you cannot place 2 Buttons in the SOUTH ( or any location
) so now you need a Panel Compile, run, you should see 2 Buttons - only the first one works. - call me if you do not. the tricky part. 1. Add another conditional to the actionPerformed method
for b2. Let pick become 2 in the
conditional If you cannot this to work proper - call me | |||
|
Lab6e Assignment Our new methods always draw in the same location because we never used
the integers x and y. After you have done this, make changes in the window size as indicated and in the pain method.
When these work, continue on to the Unit Assignment | |||
|
Unit 6 Programs 85 points - Create applet
U6P1 <----------------------------------------------------------------------------------> 100 points - Create applet U6P2
|