Question
Lab 7: Random Walks The RandomWalk Class Task Simulate a random walk using a Graphics object. Ask the user for information. Similar to previous programs,
Lab 7: Random Walks
The RandomWalk Class
Task
Simulate a random walk using a Graphics object. Ask the user for information.
Similar to previous programs, your program should start by printing
Lab 7 written by YOURNAME
Details
A random walk begins at a point and repeatedly takes a step in a randomly chosen direction. In our version, the random walk will start at the center of a circle and continue until it goes outside the circle. Each step will be randomly chosen from one pixel up, one pixel down, one pixel left, and one pixel right (this kind of random walk is called the "drunkard's walk"). Call the nextInt method on a Random object to generate an integer between 0 and 3 and map each value to one of the steps. The random walk will be drawn on a Graphics object. Here is an example for a circle with a radius of 100.
The user should be asked for the radius of the circle and the color of the circle (from at least two choices for colors). The program should print the values that the user enters. The program should draw the circle on a DrawingPanel that is big enough to hold the circle, leaving some space (but not a lot of space) around the circle. Call the drawOval on your Graphics object to draw the circle. Each step of the random walk should drawn by calling the drawLine method on your Graphics object. To animate the walk, call the sleep method on your DrawingPanel to pause a millisecond between each step. After the random walk is finished, the number of steps of the random walk should be printed.
Loops
This lab must have a while loop to ensure that the radius of the circle is between 50 and 400.
This lab must have a while loop to ensure that the user selects one of the color choices.
This lab must have a while loop to continue drawing the random walk until the walk leaves the circle.
Methods
Write and use the following methods to support your random walk.
Write a boolean method to determine whether the current position of the random walk is outside the circle. What values do you need to make this decision? These values need to be parameters of the method. How will you calculate the distance from the center of the circle? You did something similar in Lab 5.
Write a boolean method to determine whether a String answer entered by the user matches a String choice for the question.
public static boolean matchesChoice(String answer, String choice)
For example, if "blue" is one of the choices for a color, then the user should be able to enter "blue", "Blue", "BLUE", "b", or "B" to answer with this color. However, "bluish", "navyBlue", or "blueGreen" should not match. That is,
matchesChoice("blue", "blue") should return true. matchesChoice("Blue", "blue") should return true. matchesChoice("B", "blue") should return true. matchesChoice("bluish", "blue") should return false. matchesChoice("navyBlue", "blue") should return false. matchesChoice("blueGreen", "blue") should return false.
Rubric
Your program should compile without any errors. A program with more than one or two compile errors will likely get a zero for the whole assignment.
The following criteria will also be used to determine the grade for this assignment:
[2 points] If your submission includes the following:
The main method prints "Lab 7 written by [...]".
Your submission was a Zip file named lab7.zip containing a folder named lab7, which contains the other files.
If your Java program was in a file named RandomWalk.java.
If the output of your Java program was in a file named RandomWalkOutput.java.
If your program contains a comment that describes what the program does and contains a comment describing each method.
If your program is indented properly.
[3 Points] For a while loop ensuring that the user enters a number between 50 and 400 for the radius of the circle.
[3 Points] For a correct implementation of the method that determines if the random walk is outside the circle.
[3 Points] For a correct implementation of the matchesChoice method.
[2 Points] For selecting the right circle/line color based on the user's input.
[2 Points] For printing what the user entered and printing the number of steps of the random walk.
[5 Points] For a correct random walk simulation.
Here Is the code for the drawing panel:
/*
Stuart Reges and Marty Stepp
February 24, 2007
Changes by Tom Bylander in 2010 (no anti-alias, repaint on sleep)
Changes by Tom Bylander in 2012 (track mouse clicks and movement)
Changes by Tom Bylander in 2013 (fix bug in tracking mouse clicks)
Changes by S. Robbins in 2014 (getters for width and height)
Changes by S. Robbins in 2014 (addKeyListener added)
Changes by S. Robbins in 2014 (catch exception on default close so that it works in an applet)
Changes by S. Robbins in 2015 (buffer key events)
Changes by S. Robbins in 2015 (show mouse status by default is off)
The DrawingPanel class provides a simple interface for drawing persistent
images using a Graphics object. An internal BufferedImage object is used
to keep track of what has been drawn. A client of the class simply
constructs a DrawingPanel of a particular size and then draws on it with
the Graphics object, setting the background color if they so choose.
To ensure that the image is always displayed, a timer calls repaint at
regular intervals.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
public class DrawingPanel implements ActionListener {
private static final String versionMessage =
"Drawing Panel version 1.1, January 25, 2015";
private static final int DELAY = 100; // delay between repaints in millis
private static final boolean PRETTY = false; // true to anti-alias
private static boolean showStatus = false;
private static final int MAX_KEY_BUF_SIZE = 10;
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private volatile MouseEvent click; // stores the last mouse click
private volatile boolean pressed; // true if the mouse is pressed
private volatile MouseEvent move; // stores the position of the mouse
private ArrayListkeys;
// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
keys = new ArrayList();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
statusBar = new JLabel(" ");
statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
statusBar.setText(versionMessage);
panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width, height));
panel.add(new JLabel(new ImageIcon(image)));
click = null;
move = null;
pressed = false;
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
pressed = false;
move = e;
if (showStatus)
statusBar.setText("moved (" + e.getX() + ", " + e.getY() + ")");
}
public void mousePressed(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("pressed (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseDragged(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("dragged (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseReleased(MouseEvent e) {
click = e;
pressed = false;
if (showStatus)
statusBar.setText("released (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {
// System.out.println("mouse entered");
panel.requestFocus();
}
};
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
new DrawingPanelKeyListener();
g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.BLACK);
if (PRETTY) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.1f));
}
frame = new JFrame("Drawing Panel");
frame.setResizable(false);
try {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that this works in an applet
} catch (Exception e) {}
frame.getContentPane().add(panel);
frame.getContentPane().add(statusBar, "South");
frame.pack();
frame.setVisible(true);
toFront();
frame.requestFocus();
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
public void showMouseStatus(boolean f) {
showStatus = f;
}
public void addKeyListener(KeyListener listener) {
panel.addKeyListener(listener);
panel.requestFocus();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
panel.repaint();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// close the drawing panel
public void close() {
frame.dispose();
}
// makes drawing panel become the frontmost window on the screen
public void toFront() {
frame.toFront();
}
// return panel width
public int getWidth() {
return width;
}
// return panel height
public int getHeight() {
return height;
}
// return the X position of the mouse or -1
public int getMouseX() {
if (move == null) {
return -1;
} else {
return move.getX();
}
}
// return the Y position of the mouse or -1
public int getMouseY() {
if (move == null) {
return -1;
} else {
return move.getY();
}
}
// return the X position of the last click or -1
public int getClickX() {
if (click == null) {
return -1;
} else {
return click.getX();
}
}
// return the Y position of the last click or -1
public int getClickY() {
if (click == null) {
return -1;
} else {
return click.getY();
}
}
// return true if a mouse button is pressed
public boolean mousePressed() {
return pressed;
}
public synchronized int getKeyCode() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyCode;
}
public synchronized char getKeyChar() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyChar;
}
public synchronized int getKeysSize() {
return keys.size();
}
private synchronized void insertKeyData(char c, int code) {
keys.add(new KeyInfo(c,code));
if (keys.size() > MAX_KEY_BUF_SIZE) {
keys.remove(0);
// System.out.println("Dropped key");
}
}
private class KeyInfo {
public int keyCode;
public char keyChar;
public KeyInfo(char keyChar, int keyCode) {
this.keyCode = keyCode;
this.keyChar = keyChar;
}
}
private class DrawingPanelKeyListener implements KeyListener {
int repeatCount = 0;
public DrawingPanelKeyListener() {
panel.addKeyListener(this);
panel.requestFocus();
}
public void keyPressed(KeyEvent event) {
// System.out.println("key pressed");
repeatCount++;
if ((repeatCount == 1) || (getKeysSize() < 2))
insertKeyData(event.getKeyChar(),event.getKeyCode());
}
public void keyTyped(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
repeatCount = 0;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started