如何用图标正确重置JButtons的JPanel网格?

时间:2013-11-01 19:18:37

标签: java swing jbutton keylistener mouselistener

所以我有一个宝石般的游戏,它是通过让JPanel给它一个网格布局然后将按钮放在网格中并为按钮分配图标而创建的。我正试图在按下某个键时重启游戏,但我遇到了几个问题。第一个问题是,当游戏板重置时,它不会调用主方法,而且按钮会重新排列,但当鼠标悬停在图标上时,图标会恢复到原来的图标。我遇到的另一个问题是,如果没有按下按钮,则keylistener仅接收输入。任何帮助将不胜感激!!!!谢谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

public class Jewelswithec implements MouseListener, KeyListener{

  private static int rows;
  private static int columns;
  private static int numJewels;
  private static JFrame frame = new JFrame("Yakobian's Jewels");
  private JButton[][] buttonArray = null;
  private int buttonRow;
  private int buttonColumn;
  private int count = 1;
  private int[] bx = new int[2];
  private int[] by = new int[2];
  private boolean adjacent;
  private int[][] horizantal;
  private boolean[][] marked;
  private int turnCount;

  public Jewelswithec() {
    new Jewelswithec(8, 10, 4);
  }

  public Jewelswithec (int rows, int columns, int numJewels) {
    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {
    }

    this.rows = rows;
    this.columns = columns;
    this.numJewels = numJewels;

    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    marked = new boolean[rows][columns];
    JPanel board = new JPanel(new GridLayout(rows, columns));
    frame.getContentPane().add(board, "Center");
    buttonArray = new JButton[rows][columns]; 
    board.setFocusable(true);
    board.requestFocusInWindow();
    board.addKeyListener(this);

    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {       
        buttonArray[i][j] = new JButton();
        board.add(buttonArray[i][j]);
        buttonArray[i][j].addMouseListener(this);
        int randomNum = (int)(Math.random()*(numJewels)); 

        if(randomNum == 0) {
          buttonArray[i][j].setBackground(Color.BLACK);
          ImageIcon ii = new ImageIcon("circle.png");
          int scale = 7;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);

        }
        else if(randomNum == 1) {
          buttonArray[i][j].setBackground(Color.ORANGE);
          ImageIcon ii = new ImageIcon("triangle.png");
          int scale = 13;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else if(randomNum == 2) {
          buttonArray[i][j].setBackground(Color.BLUE);
          ImageIcon ii = new ImageIcon("square.png");
          int scale = 14;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else if(randomNum == 3) {
          buttonArray[i][j].setBackground(Color.DARK_GRAY);
          ImageIcon ii = new ImageIcon("star.png");
          int scale = 12;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else if(randomNum == 4) {
          buttonArray[i][j].setBackground(Color.CYAN);
          ImageIcon ii = new ImageIcon("pentagon.png");
          int scale = 14;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else if(randomNum == 5) {
          buttonArray[i][j].setBackground(Color.YELLOW);
          ImageIcon ii = new ImageIcon("diamond.png");
          int scale = 13;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else if(randomNum == 6) {
          buttonArray[i][j].setBackground(Color.GREEN);
          ImageIcon ii = new ImageIcon("hexagon.png");
          int scale = 12;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }
        else {
          buttonArray[i][j].setBackground(Color.RED);
          ImageIcon ii = new ImageIcon("octagon.png");
          int scale = 14;
          int width = ii.getIconWidth();
          int newWidth = width / scale;
          buttonArray[i][j].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                  java.awt.Image.SCALE_SMOOTH))); 
          buttonArray[i][j].setBorderPainted(false);  
          buttonArray[i][j].setFocusPainted(false);  
          buttonArray[i][j].setContentAreaFilled(false);
        }

        buttonArray[i][j].setVisible(true); 
      }
    }

    frame.setVisible(true);
  }

  public int findRow(JButton b) {

    for(int i = 0; i < rows; i++) {
      for(int j = 0; j < columns; j++) {
        if(b == buttonArray[i][j]) {
          buttonRow = i;
        }
      }
    }
    return buttonRow;
  }

  public int findColumn(JButton b) {

    for(int i = 0; i < rows; i++) {
      for(int j = 0; j < columns; j++) {
        if(b == buttonArray[i][j]) {
          buttonColumn = j;
        }
      }
    }
    return buttonColumn;
  }

  public void keyPressed(KeyEvent e) { }

  public void keyReleased(KeyEvent e) { }

  public void keyTyped(KeyEvent e) {

    if(e.getKeyChar() == 'R') {
      System.out.println("dfjaljfda");
      new Jewelswithec();
    }
  }          

  public void mouseEntered(MouseEvent event) { }

  public void mouseExited(MouseEvent e) { }

  public void mousePressed(MouseEvent e) {

    JButton b = (JButton) e.getSource();
    int pressY = this.findColumn(b);
    int pressX = this.findRow(b);
    this.highlightShape(pressX, pressY);
  }

  public void mouseReleased(MouseEvent e) { 

    JButton b = (JButton) e.getSource();
    int pressY = this.findColumn(b);
    int pressX = this.findRow(b);
    this.originalColor(pressX, pressY);
  }

  public void mouseClicked(MouseEvent e) {

    JButton b = (JButton) e.getSource();
    by[count - 1] = this.findColumn(b);
    bx[count - 1] = this.findRow(b);
    count++;

    if(count > 2) {
      count = 1;

      if((bx[1] == bx[0] && by[0] == by[1]) || ((Math.abs(bx[0] - bx[1]) > 1) || (Math.abs(by[0] - by[1]) > 1))) {
        adjacent = false;
      }
      else {
        adjacent = true;
        swapColors(bx[0], bx[1], by[0], by[1]);
      }
    }
    this.checkForWin();
  }

  public void highlightShape(int bx, int by) {

    if(buttonArray[bx][by].getBackground() == Color.BLACK) {

      ImageIcon ii = new ImageIcon("highlightcircle.png");
      int scale = 7;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.ORANGE) {

      ImageIcon ii = new ImageIcon("highlighttriangle.png");
      int scale = 13;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.BLUE) {

      ImageIcon ii = new ImageIcon("highlightsquare.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.DARK_GRAY) {

      ImageIcon ii = new ImageIcon("highlightstar.png");
      int scale = 12;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.CYAN) {

      ImageIcon ii = new ImageIcon("highlightpentagon.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.YELLOW) {

      ImageIcon ii = new ImageIcon("highlightdiamond.png");
      int scale = 13;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.GREEN) {

      ImageIcon ii = new ImageIcon("highlighthexagon.png");
      int scale = 12;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.RED) {

      ImageIcon ii = new ImageIcon("highlightoctagon.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.GRAY) {

      ImageIcon ii = new ImageIcon("highlightmark.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }
  }

  public void originalColor(int bx, int by) {

    if(buttonArray[bx][by].getBackground() == Color.BLACK) {

      ImageIcon ii = new ImageIcon("circle.png");
      int scale = 7;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.ORANGE) {

      ImageIcon ii = new ImageIcon("triangle.png");
      int scale = 13;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.BLUE) {

      ImageIcon ii = new ImageIcon("square.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.DARK_GRAY) {

      ImageIcon ii = new ImageIcon("star.png");
      int scale = 12;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.CYAN) {

      ImageIcon ii = new ImageIcon("pentagon.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.YELLOW) {

      ImageIcon ii = new ImageIcon("diamond.png");
      int scale = 13;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.GREEN) {

      ImageIcon ii = new ImageIcon("hexagon.png");
      int scale = 12;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.RED) {

      ImageIcon ii = new ImageIcon("octagon.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }

    if(buttonArray[bx][by].getBackground() == Color.GRAY) {

      ImageIcon ii = new ImageIcon("marksquare.png");
      int scale = 14;
      int width = ii.getIconWidth();
      int newWidth = width / scale;
      buttonArray[bx][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                java.awt.Image.SCALE_SMOOTH))); 
      buttonArray[bx][by].setBorderPainted(false);  
      buttonArray[bx][by].setFocusPainted(false);  
      buttonArray[bx][by].setContentAreaFilled(false);
    }
  }


  public void swapColors(int bx1, int bx2, int by1, int by2) {

    if(marked[bx1][by1] != true && marked[bx2][by2] != true) {

      Color buttonColor1 = buttonArray[bx1][by1].getBackground();
      Color buttonColor2 = buttonArray[bx2][by2].getBackground();
      buttonArray[bx1][by1].setBackground(buttonColor2);
      buttonArray[bx2][by2].setBackground(buttonColor1);
      Icon buttonImage1 = buttonArray[bx1][by1].getIcon();
      Icon buttonImage2 = buttonArray[bx2][by2].getIcon();
      buttonArray[bx1][by1].setIcon(buttonImage2);
      buttonArray[bx2][by2].setIcon(buttonImage1);
      this.checkMatch(bx2, by2);
      turnCount++;
    }
  }

  public void checkMatch(int bx, int by) {

    int grayCount = 0;
    int endX = bx;
    int startX = bx;
    int endY = by;
    int startY = by;

    for(int i = bx; rows - 1 > i && buttonArray[i][by].getBackground() == buttonArray[i+1][by].getBackground(); i++) {
      endX = i + 1;
    }

    for(int i = bx; i > 0 && buttonArray[i][by].getBackground() == buttonArray[i - 1][by].getBackground(); i--) {
      startX = i - 1;
    }

    for(int i = by; columns - 1> i &&  buttonArray[bx][i].getBackground() == buttonArray[bx][i + 1].getBackground(); i++) {
      endY = i + 1;
    }

    for(int i = by;columns > i && i> 0 && buttonArray[bx][i].getBackground() == buttonArray[bx][i - 1].getBackground(); i--) {
      startY = i - 1;
    }

    if(Math.abs(endY - startY) > 1) {
      for(int i = startY; i <= endY; i++) {
        buttonArray[bx][i].setBackground(Color.GRAY);
        ImageIcon ii = new ImageIcon("marksquare.png");
        int scale = 13;
        int width = ii.getIconWidth();
        int newWidth = width / scale;
        buttonArray[bx][i].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                 java.awt.Image.SCALE_SMOOTH))); 
        buttonArray[bx][i].setBorderPainted(false);  
        buttonArray[bx][i].setFocusPainted(false);  
        buttonArray[bx][i].setContentAreaFilled(false);
        marked[bx][i] = true;
      }
    }

    if(Math.abs(endX - startX) > 1) {
      for(int i = startX; i <= endX; i++) {
        buttonArray[i][by].setBackground(Color.GRAY);
        ImageIcon ii = new ImageIcon("marksquare.png");
        int scale = 13;
        int width = ii.getIconWidth();
        int newWidth = width / scale;
        buttonArray[i][by].setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
                                                                                 java.awt.Image.SCALE_SMOOTH))); 
        buttonArray[i][by].setBorderPainted(false);  
        buttonArray[i][by].setFocusPainted(false);  
        buttonArray[i][by].setContentAreaFilled(false);
        marked[i][by] = true;
      }
    }
  }

  public void checkForWin() {

    int grayCount = 0;

    for(int i = 0; i < rows; i++) {
      for(int j = 0; j < columns; j++) {

        if(buttonArray[i][j].getBackground() == Color.GRAY) {
          grayCount++;
        }
      }
    }

    if(grayCount == (rows*columns)) {
      System.out.println("YOU WON in " + turnCount + " legal moves.");
    }
  }

  public static void main (String args[]) {

    JFrame promptFrame = new JFrame();
    JOptionPane prompt = new JOptionPane();
    String row = prompt.showInputDialog(promptFrame, "Please enter the number of rows", "Welcome to Yakobian's Jewels!", JOptionPane.PLAIN_MESSAGE);
    String column = prompt.showInputDialog(promptFrame, "Please enter the number of columns", "Welcome to Yakobian's Jewels!", JOptionPane.PLAIN_MESSAGE);
    String numberJewels = prompt.showInputDialog(promptFrame, "Please enter the number of different colors for your Jewels", "Welcome to Yakobian's Jewels!", JOptionPane.PLAIN_MESSAGE);

    try {
      Jewelswithec j = new Jewelswithec(Integer.parseInt(row), Integer.parseInt(column), Integer.parseInt(numberJewels));
    }

    catch(NumberFormatException e) {
      Jewelswithec j = new Jewelswithec();
    }
  }
}

0 个答案:

没有答案