Hello need help implementing paintComponent. Render the maze graphically. The me
ID: 3574711 • Letter: H
Question
Hello need help implementing paintComponent. Render the maze graphically. The method is passed a “graphics” object. This object has methods of which fillRect(int x, int y, int width, int height) and setColor(Color c) are the only ones you will need.
public class Maze {
private final int _rows, _columns;
private boolean[][] _ropen, _copen;
private boolean[][] _marked;
/*
* _ropen[i,j] is true if one can get from cell [i,j] to cell [i+1,j]
* _copen[i,j] is true is one can get from cell [i,j] to cell[i,j+1];
*/
public Maze(int rows, int columns) {
_rows = rows;
_columns = columns;
_ropen = new boolean[rows-1][columns];
_copen = new boolean[rows][columns-1];
_marked = new boolean[rows][columns];
}
public int rows() { return _rows; }
public int columns() { return _columns; }
/**
* Read in contents of a maze printed with ASCII graphics from the file.
* The rows and columns are already set (do not read these).
* Example (assuming rows = 3, columns = 4)
* <pre>
* +-+-+-+ +
* | | |
* + + + +-+
* | | |
* + +---+ +
* | |
* +-----+-+
* </pre>
* @param r buffered reader to read lines from
* @throws IOException if a problem happens with reading
* @throws ParseException if the maze is badly formatted.
* (The implementation is also permitted to simply overlook
* format errors)
*/
public void read(BufferedReader r) throws IOException {
// TODO: Implement this method
try{
String currentLine;
int col = 0;
while ((currentLine = r.readLine())!= null){
if (currentLine.charAt(0)=='|'){
_ropen[0][col] = false;
}
else{
_ropen[0][col] = true;
}
if (currentLine.charAt(1)=='|'){
_ropen[1][col] = false;
}
else{
_ropen[1][col] = true;
}
if (currentLine.charAt(2)=='|'){
_ropen[2][col] = false;
}
else{
_ropen[2][col] = true;
}
if (currentLine.charAt(3)=='|'){
_ropen[3][col] = false;
}
else{
_ropen[3][col] = true;
}
col++;
}
int row = 0;
while ((currentLine = r.readLine())!= null){
if (currentLine.charAt(0)=='-'){
_ropen[row][0] = false;
}
else{
_ropen[row][0] = true;
}
if (currentLine.charAt(1)=='-'){
_ropen[row][1] = false;
}
else{
_ropen[row][1] = true;
}
if (currentLine.charAt(2)=='-'){
_ropen[row][2] = false;
}
else{
_ropen[row][2] = true;
}
row++;
}
r.close();
}
catch(IOException e){
e.printStackTrace();
}
}
/**
* Use non-recursive
* depth-first search to find a path from the lower-left corner to the upper-right.
* If a path is found, mark all the cells on the route and return true.
* Otherwise, mark everything reachable and return false.
* @return whether a path was found
*/
public boolean findPath()
{
// TODO: Implement this method.
boolean pathFound=false; //flag to tell whether path is found or not
//all cells marked as unvisited initially
for (int i = 0; i < _rows; ++i) {
for (int j = 0; j < _columns; ++j) {
_marked[i][j] = false;
}
}
//location to start search - lower left corner
//it is assumed there will always be an open entry at lower left corner.
int currentRow=_rows;
int currentColumn=0;
int previousRow=_rows+1, previousColumn=-1; //with respect to current row and columns
//loop to find the path
while(currentRow!=0 && currentColumn!=_columns)
{
if(_ropen[currentRow][currentColumn]==true)
{
_marked[currentRow][currentColumn]=true;
previousRow=currentRow;
previousColumn=currentColumn;
currentRow=currentRow-1;
pathFound=true;
}
else if(_copen[currentRow][currentColumn]==true)
{
_marked[currentRow][currentColumn]=true;
previousRow=currentRow;
previousColumn=currentColumn;
currentColumn=currentColumn+1;
pathFound=true;
}
else //no path found, backtrack to previous position
{
pathFound=false;
currentRow=previousRow;
currentColumn=previousColumn;
}
}
return pathFound; //returns whether path is found or not.
}
public void clear() {
for (int i = 0; i < _rows; ++i) {
for (int j=0; j < _columns; ++j) {
_marked[i][j] = false;
if (i+1 != _rows) _ropen[i][j] = false;
if (j+1 != _columns) _copen[i][j] = false;
}
}
}
private JComponent panel;
public JComponent getPanel() {
if (panel == null) panel = new Panel();
return panel;
}
@SuppressWarnings("serial")
public static class ParseException extends IOException {
public ParseException(String s) { super(s); }
}
/**
* A representation of an [row,column] coordinate.
* You may this useful when implementing methods, but there's
* no requirement that you use it.
*/
public class Cell {
public final int row, column;
public Cell(int i, int j) {
if (i < 0 || i >= _rows) throw new IllegalArgumentException("row " + i + " out of range [0," + _rows + ")");
if (j < 0 || j >= _columns) throw new IllegalArgumentException("column " + j + " out of range [0," + _columns + ")");
this.row = i;
this.column = j;
}
public String toString() {
return "[" + row + "," + column + "]"; // useful for debugging
}
}
public class Panel extends JPanel {
private static final int SQUARESIZE = 10;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// TODO: Implement this method:
// 1. Draw the black border
// 2. Draw the block corner squares and every place NOT open
// 3. Switch color to Yellow
// 4. If the start is marked, draw entrance squares.
// 5. If the end is marked, draw the exit squares.
// 6. Draw every marked center, and every open wall between two marked centers.
}
}
Explanation / Answer
package samplepro;
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class Maze {
private final int _rows, _columns;
private boolean[][] _ropen, _copen;
private boolean[][] _marked;
/*
* _ropen[i,j] is true if one can get from cell [i,j] to cell [i+1,j]
* _copen[i,j] is true is one can get from cell [i,j] to cell[i,j+1];
*/
public Maze(int rows, int columns) {
_rows = rows;
_columns = columns;
_ropen = new boolean[rows-1][columns];
_copen = new boolean[rows][columns-1];
_marked = new boolean[rows][columns];
}
public int rows() { return _rows; }
public int columns() { return _columns; }
/**
* Read in contents of a maze printed with ASCII graphics from the file.
* The rows and columns are already set (do not read these).
* Example (assuming rows = 3, columns = 4)
* <pre>
* +-+-+-+ +
* | | |
* + + + +-+
* | | |
* + +---+ +
* | |
* +-----+-+
* </pre>
* @param r buffered reader to read lines from
* @throws IOException if a problem happens with reading
* @throws ParseException if the maze is badly formatted.
* (The implementation is also permitted to simply overlook
* format errors)
*/
public void read(BufferedReader r) throws IOException {
// TODO: Implement this method
try{
String currentLine;
int col = 0;
while ((currentLine = r.readLine())!= null){
if (currentLine.charAt(0)=='|'){
_ropen[0][col] = false;
}
else{
_ropen[0][col] = true;
}
if (currentLine.charAt(1)=='|'){
_ropen[1][col] = false;
}
else{
_ropen[1][col] = true;
}
if (currentLine.charAt(2)=='|'){
_ropen[2][col] = false;
}
else{
_ropen[2][col] = true;
}
if (currentLine.charAt(3)=='|'){
_ropen[3][col] = false;
}
else{
_ropen[3][col] = true;
}
col++;
}
int row = 0;
while ((currentLine = r.readLine())!= null){
if (currentLine.charAt(0)=='-'){
_ropen[row][0] = false;
}
else{
_ropen[row][0] = true;
}
if (currentLine.charAt(1)=='-'){
_ropen[row][1] = false;
}
else{
_ropen[row][1] = true;
}
if (currentLine.charAt(2)=='-'){
_ropen[row][2] = false;
}
else{
_ropen[row][2] = true;
}
row++;
}
r.close();
}
catch(IOException e){
e.printStackTrace();
}
}
/**
* Use non-recursive
* depth-first search to find a path from the lower-left corner to the upper-right.
* If a path is found, mark all the cells on the route and return true.
* Otherwise, mark everything reachable and return false.
* @return whether a path was found
*/
public boolean findPath()
{
// TODO: Implement this method.
boolean pathFound=false; //flag to tell whether path is found or not
//all cells marked as unvisited initially
for (int i = 0; i < _rows; ++i) {
for (int j = 0; j < _columns; ++j) {
_marked[i][j] = false;
}
}
//location to start search - lower left corner
//it is assumed there will always be an open entry at lower left corner.
int currentRow=_rows;
int currentColumn=0;
int previousRow=_rows+1, previousColumn=-1; //with respect to current row and columns
//loop to find the path
while(currentRow!=0 && currentColumn!=_columns)
{
if(_ropen[currentRow][currentColumn]==true)
{
_marked[currentRow][currentColumn]=true;
previousRow=currentRow;
previousColumn=currentColumn;
currentRow=currentRow-1;
pathFound=true;
}
else if(_copen[currentRow][currentColumn]==true)
{
_marked[currentRow][currentColumn]=true;
previousRow=currentRow;
previousColumn=currentColumn;
currentColumn=currentColumn+1;
pathFound=true;
}
else //no path found, backtrack to previous position
{
pathFound=false;
currentRow=previousRow;
currentColumn=previousColumn;
}
}
return pathFound; //returns whether path is found or not.
}
public void clear() {
for (int i = 0; i < _rows; ++i) {
for (int j=0; j < _columns; ++j) {
_marked[i][j] = false;
if (i+1 != _rows) _ropen[i][j] = false;
if (j+1 != _columns) _copen[i][j] = false;
}
}
}
private JComponent panel;
public JComponent getPanel() {
if (panel == null) panel = new Panel();
return panel;
}
@SuppressWarnings("serial")
public static class ParseException extends IOException {
public ParseException(String s) { super(s); }
}
/**
* A representation of an [row,column] coordinate.
* You may this useful when implementing methods, but there's
* no requirement that you use it.
*/
public class Cell {
public final int row, column;
public Cell(int i, int j) {
if (i < 0 || i >= _rows) throw new IllegalArgumentException("row " + i + " out of range [0," + _rows + ")");
if (j < 0 || j >= _columns) throw new IllegalArgumentException("column " + j + " out of range [0," + _columns + ")");
this.row = i;
this.column = j;
}
public String toString() {
return "[" + row + "," + column + "]"; // useful for debugging
}
}
public class Panel extends JPanel {
private static final int SQUARESIZE = 10;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// TODO: Implement this method:
// 1. Draw the black border
// 2. Draw the block corner squares and every place NOT open
// 3. Switch color to Yellow
// 4. If the start is marked, draw entrance squares.
// 5. If the end is marked, draw the exit squares.
// 6. Draw every marked center, and every open wall between two marked centers.
g.setColor(Color.BLACK);
g.fillRect(0, 0, rows()*SQUARESIZE, columns()*SQUARESIZE);
g.setColor(Color.yellow);
if(_marked[0][0])
g.fillRect(0, 0, SQUARESIZE, SQUARESIZE);
if(_marked[rows()][columns()])
g.fillRect(rows()*SQUARESIZE, columns()*SQUARESIZE, SQUARESIZE, SQUARESIZE);
for(int i=0; i<rows(); i++)
{
for(int j=0; j<columns(); j++)
{
if(_marked[i][j])
{
g.fillRect(i ,j, SQUARESIZE, SQUARESIZE);
if(i != 0 && j!=0)
{
if(_ropen[i-1][j-1])
g.fillRect(i ,j, SQUARESIZE, SQUARESIZE);
}
}
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.