"Draw a grid of 20 streets horizontally and 20 streets vertically. Represent the simulated drunkard by a dot, placed in the middle of the grid to start. For 100 times, have the simulated drunkard randomly pick a direction, move one block in the chosen direction, and draw the dot. Use classes for the grid and the drunkard."

I have the Grid class, the RandomWalk class, and the Driver class. Right now, the program runs a blank JFrame, with no button or anything else showing up.

My specific questions are: How do I show the grid using the driver, how do I show the JButton, and how do I create the Run method?

I have posted the three classes below.
Thank you very much :)

Recommended Answers

All 4 Replies

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class RandomWalk {

public static final int NORTH = 0;

public static final int EAST = 90;

public static final int SOUTH = 180;

public static final int WEST = 270;
double startX = 0;
double startY = 0;

Point2D.Double point = new Point2D.Double(startX, startY);
	
public RandomWalk(Point2D.Double point){
this.point = point;
}

public void setLocation(){
RandomWalk random = new RandomWalk(point);
startX = 0;
startY = 0;
}

public void Walk(){
int dir = (int) Math.random()*4+1;	

for(int i = 0; i < 100; i++){
if(dir==1){

}

else if(dir==2){
	
}

else if(dir==3){
	
}

else if(dir==4){
	
}
}
}

public void Paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;

Ellipse2D.Double drunk = new Ellipse2D.Double(startX, startY, 30, 30);
g2.setColor(Color.black);
g2.draw(drunk);
g2.fill(drunk);

}
	
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;

public class Grid extends JFrame{

int wide = getWidth();
int tall = getHeight();

public Grid(int wide, int tall){
this.wide = wide;
this.tall = tall;

}
	
public void drawGrid(Graphics g){
Graphics2D g2 = (Graphics2D)g;

g.setColor(Color.white);
g.fillRect(0, 0, wide, tall);

int w = wide / 20;
int h = tall / 20;
g2.setColor(Color.gray);

for (int i = 1; i < 10; i++) {
g2.drawLine(i * w, 0, i * w, tall);
}

for (int i = 1; i < 10; i++) {
g2.drawLine(0, i * h, wide, i * h);
}

g2.setColor(Color.red);
double rowH = getHeight() / 10.0;
for (int i = 1; i < 10; i++) {
Line2D line = new Line2D.Double(0.0, (double) i * rowH,
(double) getWidth(), (double) i * rowH);
g2.draw(line);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class RandomWalkDriver{

private static void createAndShowGUI()  {
RandomWalk drunk = new RandomWalk();
Grid streets = new Grid(600,600);
drunk.setLocation();
drunk.Walk();

JFrame frame = new JFrame();
frame.setSize(600,600);
frame.setTitle("Tipsy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(streets);

 
        JButton button = new JButton(" >> Walk! <<");
        //Add action listener to button
        button.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent Walk)
            {
                //Execute when button is pressed
                System.out.println("The drunk is walking.");
            }
        });      
 
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
    }
 
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

You could use the Graphics class's drawLine method to make a grid.

You show a component like a JButton by adding it to a container that is in a GUI that is visible.

This would create a run method:
public void run() {
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.