Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A drunkard in a grid of streets randomly picks one of four directions and stumbl

ID: 3807234 • Letter: A

Question

A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn’t move very far because the choices cancel each other out, but that is actually not the case.

Starting with the NB_Grafix_Thing2 project, add Java code to the paint method so that it will graphically display the drunkard's walk, using the following algorithm:

Notes:

the canvas dimensions are 800 x 572, so the center point is at (400, 286)

Optional:

draw each step of the drunkard's walk in a random color

This is the code to be use:

import java.awt.*;

public class NB_Grafix_Thing2 extends javax.swing.JFrame
{

/**
* Creates new form NB_Grafix_Thing2
*/
public NB_Grafix_Thing2()
{
initComponents();
}

@Override
public void paint(Graphics g)
{
// draw the window title, menu, buttons
super.paint(g);

// get the Graphics context for the canvas
Graphics pen = canvas.getGraphics();

/**
* * add pen Graphics method calls after this line **
*/

  
  
/**
* ***********************************************
* WARNING!!! DO NOT ADD CODE BELOW THIS LINE!!! *
************************************************
*/
} // end paint

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the NB_Grafix_Thing2 Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents()
{

canvas = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("My Drawing Form");

canvas.setBackground(new java.awt.Color(255, 255, 255));
canvas.setLayout(new java.awt.BorderLayout());
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);

resetBtn.setText("Reset");
resetBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
resetBtnActionPerformed(evt);
}
});
getContentPane().add(resetBtn, java.awt.BorderLayout.PAGE_END);

setSize(new java.awt.Dimension(816, 639));
setLocationRelativeTo(null);
}//

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {   
repaint();
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NB_Grafix_Thing2().setVisible(true);
}
});
}

// Variables declaration - do not modify   
private javax.swing.JPanel canvas;
private javax.swing.JButton resetBtn;
// End of variables declaration   
}

Sample Runs:

I need specific answer

Explanation / Answer

import java.awt.*;

public class NB_Grafix_Thing2 extends javax.swing.JFrame
{

/**
* Creates new form NB_Grafix_Thing2
*/
public NB_Grafix_Thing2()
{
initComponents();
}

@Override
public void paint(Graphics g)
{
// draw the window title, menu, buttons
super.paint(g);

// get the Graphics context for the canvas
Graphics pen = canvas.getGraphics();

/**
* * add pen Graphics method calls after this line **
*/

  
  
/**
* ***********************************************
* WARNING!!! DO NOT ADD CODE BELOW THIS LINE!!! *
************************************************
*/
} // end paint

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the NB_Grafix_Thing2 Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents()
{

canvas = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("My Drawing Form");

canvas.setBackground(new java.awt.Color(255, 255, 255));
canvas.setLayout(new java.awt.BorderLayout());
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);

resetBtn.setText("Reset");
resetBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
resetBtnActionPerformed(evt);
}
});
getContentPane().add(resetBtn, java.awt.BorderLayout.PAGE_END);

setSize(new java.awt.Dimension(816, 639));
setLocationRelativeTo(null);
}//

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {   
repaint();
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(NB_Grafix_Thing2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NB_Grafix_Thing2().setVisible(true);
}
});
}

// Variables declaration - do not modify   
private javax.swing.JPanel canvas;
private javax.swing.JButton resetBtn;
// End of variables declaration   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote