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

📄 mycanvas.java

📁 小游戏的代码。火影拼图的几种图案锻炼智力
💻 JAVA
字号:
package 小游戏;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyCanvas extends JPanel
  implements MouseListener
{
  boolean hasAddActionListener = false;
  Cell[] cell;
  Rectangle cellNull;
  public static int pictureID = 1;

  public MyCanvas()
  {
    setLayout(null);
    setSize(300, 300);
    this.cellNull = new Rectangle(200, 200, 100, 100);
    this.cell = new Cell[9];

    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) {
        Icon icon = new ImageIcon("pictrue/pic_" + pictureID + "_" + (i * 3 + j + 1) + ".jpg");
        this.cell[(i * 3 + j)] = new Cell(icon);
        this.cell[(i * 3 + j)].setLocation(j * 100, i * 100);
        add(this.cell[(i * 3 + j)]);
      }

    remove(this.cell[8]);
  }

  public void reLoadPictrue()
  {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) {
        Icon icon = new ImageIcon("pictrue/pic_" + pictureID + "_" + (i * 3 + j + 1) + ".jpg");
        this.cell[(i * 3 + j)].setIcon(icon);
      }
  }

  public boolean isFinish() {
    for (int i = 0; i < 8; ++i) {
      int x = this.cell[i].getBounds().x;
      int y = this.cell[i].getBounds().y;
      if (y / 100 * 3 + x / 100 != i)
        return false;
    }
    return true;
  }

  public void Start()
  {
    while ((this.cell[0].getBounds().x <= 100) && (this.cell[0].getBounds().y <= 100)) {
      int j;
      int x = this.cellNull.getBounds().x;
      int y = this.cellNull.getBounds().y;
      int direction = (int)(Math.random() * 4.0D);
      label409: if (direction == 0)
      {
        if (test(x -= 100, y))
          for (j = 0; j < 8; ++j)
            if ((this.cell[j].getBounds().x == x) && (this.cell[j].getBounds().y == y)) {
              this.cell[j].move("RIGHT", 100);
              this.cellNull.setLocation(x, y);
              break label409;
            }

      }
      else if (direction == 1)
      {
        if (test(x += 100, y))
          for (j = 0; j < 8; ++j)
            if ((this.cell[j].getBounds().x == x) && (this.cell[j].getBounds().y == y)) {
              this.cell[j].move("LEFT", 100);
              this.cellNull.setLocation(x, y);
              break label409;
            }

      }
      else if (direction == 2)
      {
        if (test(x, y -= 100))
          for (j = 0; j < 8; ++j)
            if ((this.cell[j].getBounds().x == x) && (this.cell[j].getBounds().y == y)) {
              this.cell[j].move("DOWN", 100);
              this.cellNull.setLocation(x, y);
              break label409;
            }



      }
      else if (test(x, y += 100)) {
        for (j = 0; j < 8; ++j)
          if ((this.cell[j].getBounds().x == x) && (this.cell[j].getBounds().y == y)) {
            this.cell[j].move("UP", 100);
            this.cellNull.setLocation(x, y);
            break;
          }


      }

    }

    if (!(this.hasAddActionListener))
      for (int i = 0; i < 8; ++i)
        this.cell[i].addMouseListener(this);
    this.hasAddActionListener = true;
  }

  private boolean test(int x, int y) {
    return (((x >= 0) && (x <= 200)) || ((y >= 0) && (y <= 200))); }

  public void mouseClicked(MouseEvent arg0) { }

  public void mouseEntered(MouseEvent arg0) { }

  public void mouseExited(MouseEvent arg0) { }

  public void mouseReleased(MouseEvent arg0) { }

  public void mousePressed(MouseEvent arg0) {
    Cell button = (Cell)arg0.getSource();
    int x1 = button.getBounds().x;
    int y1 = button.getBounds().y;

    int x2 = this.cellNull.getBounds().x;
    int y2 = this.cellNull.getBounds().y;

    if ((x1 == x2) && (y1 - y2 == 100))
      button.move("UP", 100);
    else if ((x1 == x2) && (y1 - y2 == -100))
      button.move("DOWN", 100);
    else if ((((x1 - x2 == 100) ? 1 : 0) & ((y1 == y2) ? 1 : 0)) != 0)
      button.move("LEFT", 100);
    else if ((x1 - x2 == -100) && (y1 == y2))
      button.move("RIGHT", 100);
    else
      return;

    this.cellNull.setLocation(x1, y1);
    repaint();
    if (isFinish()) {
      JOptionPane.showMessageDialog(this, "恭喜你完成拼图,加油!");
      for (int i = 0; i < 8; ++i)
        this.cell[i].removeMouseListener(this);
      this.hasAddActionListener = false;
    }
  }
}

⌨️ 快捷键说明

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