Due 2:15 PM, Tuesday, November 28th.
Your AI will provide you with a password that you will type into the Java source file you submit in lab. You will submit one file via the A112 Vincent submission system (linked from the A112 home page).
You do not have to understand the material in the "Using buttons" section of chapter 7 or any of chapter 6. You will be given whatever graphic user interface (GUI) code you need.
Do exercises 7.5, defining an applet called Betting.java. Throw the dice every time your applet is painted and display the dice value and the resulting winnings.. By covering up the applet window and exposing it again, you can make repeated bets with different results.
Do exercise 7.7 by inserting code in place of the last comment and the drawString line.. First run the code as is.
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class RockScissorsPaper extends Applet implements ActionListener {
private Button rockButton;
private Button scissorsButton;
private Button paperButton;
private String buttonPressed = ""; // the label of the button pressed
public void init() {
rockButton = new Button("Rock");
scissorsButton = new Button("Scissors");
paperButton = new Button("Paper");
add(rockButton);
add(scissorsButton);
add(paperButton);
rockButton.addActionListener(this);
scissorsButton.addActionListener(this);
paperButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
buttonPressed = ((Button)event.getSource()).getLabel();
repaint();
}
// paint is called each time a button is pressed
public void paint(Graphics g) {
// replace the following line with lines of code that implement the game
g.drawString(buttonPressed, 20, 100);
}
}
Do exercises 7.11 by inserting code in place of the last comment and the drawString line. Each time the slider is moved or a button is pressed the paint method is called and can use the values of the stepSize and buttonPressed variables.
You can start with this program skeleton. Note the need for the update method. Try out this sample solution applet.
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class TurtleGraphics extends Applet
implements ActionListener, AdjustmentListener {
private Button upButton;
private Button downButton;
private Button leftButton;
private Button rightButton;
private Button forwardButton;
private Scrollbar bar;
private String buttonPressed = ""; // the label of the button pressed
private int stepSize = 25; // value indicated by the slider
public void init() {
upButton = new Button("Up");
downButton = new Button("Down");
leftButton = new Button("Left");
rightButton = new Button("Right");
forwardButton = new Button("Forward");
add(upButton);
add(downButton);
add(leftButton);
add(rightButton);
add(forwardButton);
upButton.addActionListener(this);
downButton.addActionListener(this);
leftButton.addActionListener(this);
rightButton.addActionListener(this);
forwardButton.addActionListener(this);
bar = new Scrollbar(Scrollbar.HORIZONTAL, stepSize, 1, 0, 50);
add(new Label("step size"));
add(bar);
bar.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
stepSize = bar.getValue();
repaint();
}
public void actionPerformed(ActionEvent event) {
buttonPressed = ((Button)event.getSource()).getLabel();
repaint();
}
// Add this method to your code. It will cause your
// drawing to stay. If you don't add this, every time
// an action happens, your las line will go away.
public void update(Graphics g){
paint(g);
requestFocus();
}
public void paint(Graphics g) {
// replace the following line to implement turtle graphics
g.drawString(buttonPressed + " " + stepSize, 20, 100);
}
}
Submit your TurtleGraphics.java file under Assignment 4 on using Vincent submission system. Be sure to read and carefully follow the Vincent submission instructions on the course web page.