0

我有一个二维网格数组(JPanels),它们使用 GridLayout 添加到另一个 JPanel。该 JPanel 被添加到 JFrame。每当在 JFrame 上发生点击时,我都会尝试获取点击点并确定二维数组中的任何网格是否包含该点。

我正在尝试在 frame.addMouseListener 中执行此操作...

我知道框架正在注册鼠标点击。由于某种原因,网格没有注册它们应该包含该点。谁能解释一下?if(theView[i][j].contains(me.getPoint())){这是似乎让我失望的代码行。

我最初试图让网格知道何时单击它们,这样我就不必在框架和网格之间进行协调,但我无法让它工作。

这是关卡设计师。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import javax.swing.*;

public class LevelDesigner extends JPanel implements ButtonListener{
    private final int SIZE = 12;
    private int [][] thePit;    
    private Grid [][] theView;
    private ButtonPanel bp;
    public static int val;
    private int rows, cols;
    private JPanel gridPanel;
    private JFrame frame;

    public LevelDesigner(int r, int c){    
        frame = new JFrame();        
        int h = 10, w = 10;
        setVisible(true);
        setLayout(new BorderLayout());
        setBackground(Color.BLUE);
        rows = r;
        cols = c;
        thePit = new int[r][c];
        theView = new Grid[r][c];   
        gridPanel = new JPanel();
        gridPanel.setVisible(true);
        gridPanel.setBackground(Color.BLACK);
        gridPanel.setPreferredSize(getMaximumSize());
        GridLayout gridLayout = new GridLayout();
        gridLayout.setColumns(cols);
        gridLayout.setRows(rows);
        gridPanel.setLayout(gridLayout);

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                theView[i][j] = new Grid(i, j, SIZE, this);
                gridPanel.add(theView[i][j]);
            }
        }

        String test [] = {"0", "1","2","3","4","save"};
        bp =  new ButtonPanel(test,  this);
        this.add(bp, BorderLayout.SOUTH);
        this.add(gridPanel, BorderLayout.CENTER);

        frame.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent me) {

                for(int i = 0; i < rows; ++i){
                    for(int j = 0; j < cols; ++j){
                        if(theView[i][j].contains(me.getPoint())){
                            theView[i][j].actionPerformed(null);
                            return;
                        }
                    }
                }
            } 
        });

        frame.setVisible(true);
        frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        frame.setTitle("Epic Crawl - Main Menu");
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.repaint();
        frame.add(this);
    }

    public String toString(){
        int noRows = thePit.length;
        int noColumns = thePit[0].length;

        String s="";
        for (int r=0;r<noRows;r++){
            for (int c=0;c<noColumns;c++){
                s=s + thePit[r][c] + " ";
            }
            s=s+"\n";
        }
        return(s);
    }

    public void notify( int i, int j){
        thePit[i][j] = val;
    }

    public void print(){
        final JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new java.io.File("."));
        int returnVal = fc.showSaveDialog( null);

        if( returnVal == JFileChooser.APPROVE_OPTION ){
            try{
                PrintWriter p = new PrintWriter( 
                new File( fc.getSelectedFile().getName() ) );
                System.out.println(" printing");

                p.println( this );
                p.close();   
            }
            catch( Exception e){
                System.out.println("ERROR: file not saved");
            }
        }
    }

    public void buttonPressed(String buttonLabel, int id){
        if(id == 5)
            print();
        else
            val = id;
    }

    public void buttonReleased( String buttonLabel, int buttonId ){}
    public void buttonClicked( String buttonLabel, int buttonId ){}

    public static void main(String arg[]){ 
        LevelDesigner levelDesigner = new LevelDesigner(4, 4);
    }
}

这是网格。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Grid extends JPanel implements ActionListener{
    LevelDesigner grid;    
    int myI, myJ;
    private String[] imageNames = {"dirt.png", "grass.png", "Door.png", "woodfloor.png", "32x32WoodFloor.png"};
    BufferedImage gridImage;
    private String imagePath;

    public Grid(int i, int j, int size, LevelDesigner m){
        imagePath = "";
        grid = m;
        myI = i;
        myJ = j;
        setBackground(Color.RED);
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        this.setVisible(true);
    }

   @Override
   public void actionPerformed(ActionEvent ae){
        grid.notify(myI, myJ);
        imagePath = "Images/" + imageNames[LevelDesigner.val];
        gridImage = null;
        InputStream input = this.getClass().getClassLoader().getResourceAsStream(imagePath);
        try{
            gridImage = ImageIO.read(input);
        }catch(Exception e){System.err.println("Failed to load image");}
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g); // Important to call super class method
        g.clearRect(0, 0, getWidth(), getHeight()); // Clear the board
        g.drawImage(gridImage, 0, 0, getWidth(), getHeight(), null);
    }
}
4

2 回答 2

3

contains 方法检查 Point 是否在 JPanel 的边界内,但使用相对于 JPanel的坐标系。而是考虑使用findComponentAt(Point p).

例如:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class TestMouseListener {
   private static final int SIDE_COUNT = 4;
   private JPanel mainPanel = new JPanel();
   private MyGridCell[][] grid = new MyGridCell[SIDE_COUNT][SIDE_COUNT];

   public TestMouseListener() {
      mainPanel.setLayout(new GridLayout(SIDE_COUNT, SIDE_COUNT));
      for (int i = 0; i < grid.length; i++) {
         for (int j = 0; j < grid[i].length; j++) {
            grid[i][j] = new MyGridCell();
            mainPanel.add(grid[i][j].getMainComponent());
         }
      }

      mainPanel.addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Component c = mainPanel.findComponentAt(p);
            for (MyGridCell[] gridRow : grid) {
               for (MyGridCell myGridCell : gridRow) {
                  if (c == myGridCell.getMainComponent()) {
                     myGridCell.setLabelText("Pressed!");
                  } else {
                     myGridCell.setLabelText("");
                  }
               }
            }
         }
      });
   }

   public Component getMainComponent() {
      return mainPanel;
   }

   private static void createAndShowGui() {
      TestMouseListener mainPanel = new TestMouseListener();

      JFrame frame = new JFrame("TestMouseListener");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyGridCell {
   private static final int PREF_W = 200;
   private static final int PREF_H = PREF_W;
   @SuppressWarnings("serial")
   private JPanel mainPanel = new JPanel() {
      public Dimension getPreferredSize() {
         return MyGridCell.this.getPreferredSize();
      };
   };
   private JLabel label = new JLabel();

   public MyGridCell() {
      mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.setLayout(new GridBagLayout());
      mainPanel.add(label);
   }

   public Component getMainComponent() {
      return mainPanel;
   }

   public void setLabelText(String text) {
      label.setText(text);
   }

   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}
于 2013-07-19T03:19:55.593 回答
2

Component#contains “检查此组件是否“包含”指定点,该点的xy坐标被定义为相对于该组件的坐标系”

这意味着只有在的范围内contains才会返回。truePoint0 x 0 x width x height

因此,如果组件的位置为 400x200,尺寸为 200x200(例如)。当您在内部单击时,鼠标点将在 400x200 和 600x400 之间,这实际上是在组件的相对位置 (200x200) 之外 - 感到困惑......

基本上,您要么需要将点击点转换为您正在检查的组件上下文中的相对坐标...

Point p = SwingUtilities.convertPoint(frame, me.getPoint(), theView[i][j])
if (theView[i][j].contains(p)) {...

或者使用组件Rectangle边界...

if (theView[i][j].getBounds().contains(me.getPoint())) {...

所以,请记住,鼠标事件是相对于它们生成的组件而言的

于 2013-07-19T03:20:05.930 回答