⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise14_5.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise14_5.java: The Maze problem
// Comment the block() and unblock() methods to show Figure 10.30 for jbBook
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Exercise14_5 extends JApplet implements ActionListener {
  private Cell[][] board = new Cell[8][8];
  private JButton jbtFindPath = new JButton("Find Path");
  private JButton jbtClearPath = new JButton("Clear Path");
  private JPanel jpBoard, jpButton;
  private JLabel jlblStatus = new JLabel();

  public void init() {
    jpBoard = new JPanel();
    jpBoard.setLayout(new GridLayout(8, 8, 2, 2));

    jpButton = new JPanel();
    jpButton.setLayout(new FlowLayout());
    jpButton.add(jbtFindPath);
    jpButton.add(jbtClearPath);

    // Add cells to jpBoard
    for (int row = 0; row < board.length; row++)
      for (int col = 0; col < board[row].length; col++) {
        board[row][col] = new Cell();
        jpBoard.add(board[row][col]);
      }

    // Add jpBoard, jpButtons and jlblStatus to the applet
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(jlblStatus, BorderLayout.NORTH);
    getContentPane().add(jpBoard, BorderLayout.CENTER);
    getContentPane().add(jpButton, BorderLayout.SOUTH);

    // Register listeners
    jbtFindPath.addActionListener(this);
    jbtClearPath.addActionListener(this);
  }

  // Main method
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("Exercise14_5");

    // Create an instance of MortgageApplet
    Exercise14_5 applet = new Exercise14_5();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    String arg = e.getActionCommand();
    if (e.getSource() instanceof JButton)
      if ("Find Path".equals(arg)) {
      	findPath();
      }
      else if ("Clear Path".equals(arg)) {
      	clearPath();
      }
  }

  public void findPath() {
    if (findPath(0,0))
      jlblStatus.setText("path found");
    else
      jlblStatus.setText("No path exists");
  }

  public boolean findPath(int row, int col) {
    board[row][col].visit();

    if ((col == 7) && (row == 7)) {
      board[row][col].selectCell();
      return true;
    }

    if ((row > 0) && !board[row - 1][col].marked() &&
      !board[row - 1][col].blocked() && !board[row - 1][col].visited()) {
//      block(row,col);

      if (findPath(row - 1, col)) {
      	board[row][col].selectCell();
	return true;
      }

//      unblock(row,col);
    }

    if ((row < 7) && !board[row + 1][col].marked() &&
      !board[row+1][col].blocked() && !board[row+1][col].visited()) {
//      block(row,col);

      if (findPath(row+1,col)) {
      	board[row][col].selectCell();
	return true;
      }

//      unblock(row,col);
    }

    if ((col>0) && !board[row][col-1].marked() &&
      !board[row][col - 1].blocked() && !board[row][col - 1].visited()) {
//      block(row,col);
      if (findPath(row, col - 1)) {
      	board[row][col].selectCell();
	return true;
      }

//      unblock(row,col);
    }

    if ((col < 7) && !board[row][col + 1].marked() &&
      !board[row][col + 1].blocked() && !board[row][col + 1].visited()) {
//      block(row,col);
      if (findPath(row, col + 1)) {
      	board[row][col].selectCell();
	return true;
      }

//      unblock(row,col);
    }

    return false;
  }

  // Temporary block the neighbor to prevent neighboring path
  public void block(int row, int col) {
    if (row > 0) board[row - 1][col].block();

    if (row < 7) board[row + 1][col].block();

    if (col > 0) board[row][col - 1].block();

    if (col<7) board[row][col+1].block();
  }

  // Remove the temporary block
  public void unblock(int row, int col) {
    if (row > 0) board[row-1][col].unblock();

    if (row < 7) board[row + 1][col].unblock();

    if (col > 0) board[row][col - 1].unblock();

    if (col < 7) board[row][col + 1].unblock();
  }

  public void clearPath() {
    for (int row = 0; row < board.length; row++)
      for (int col = 0; col < board[row].length; col++) {
      	board[row][col].deselectCell();
      }
  }

  // Inner class
  class Cell extends JPanel implements MouseListener {
    private boolean marked = false;
    private boolean visited = false;
    private boolean blocked = false;
    private boolean cellSelected = false;

    public Cell() {
      setBackground(Color.white);
      addMouseListener(this);
    }

    public boolean marked() {
      return marked;
    }

    public void visit() {
      visited = true;
    }

    public boolean visited() {
      return visited;
    }

    public boolean blocked() {
      return blocked;
    }

    public void block() {
      blocked = true;
    }

    public void unblock() {
      blocked = false;
    }

    public void selectCell() {
      setBackground(Color.red);

      repaint();
    }

    public void deselectCell() {
      setBackground(Color.white);
      repaint();
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g);

      if (marked) {
        g.drawLine(0, 0, getSize().width, getSize().height);
        g.drawLine(getSize().width, 0, 0, getSize().height);
      }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
      marked = !marked;
      repaint();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -