井字游戏Java MouseListener [英] Tic tac toe java mouseListener

查看:111
本文介绍了井字游戏Java MouseListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在使用MVC开发井字游戏应用程序.我在实现视图页面中的鼠标侦听器时遇到困难.我需要板上的每个面板都是可单击的.有人可以帮我吗?

So I've been working on a Tic Tac Toe App using MVC. I'm having difficulties implementing the mouse Listener in the views page. I need each panel of the board to be clickable. Can someone help me?

public class TicTacToeView extends JFrame{

    private JButton oButton, xButton;
    public JPanel board;
    public ArrayList<Shape> shapes;

    public TicTacToeView(){
        shapes = new ArrayList<Shape>();
        JPanel topPanel=new JPanel();
        topPanel.setLayout(new FlowLayout());
        add(topPanel, BorderLayout.NORTH);
        add(board=new Board(), BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
    }
    private class Board extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w=getWidth();
            int h=getHeight();
            Graphics2D g2d = (Graphics2D) g;

            g2d.setPaint(Color.WHITE);
            g2d.fill(new Rectangle2D.Double(0, 0, w, h));
            g2d.setPaint(Color.BLACK);
            g2d.setStroke(new BasicStroke(4));
            g2d.draw(new Line2D.Double(0, h/3, w, h/3));
            g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
            g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
            g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
            //draw circles and xs by visiting elements in the array List.
            for(Shape shape : shapes){
                g2d.setPaint(Color.BLUE);
                g2d.draw(shape);
            }
        }
    }
    public void addMouseListener(MouseListener ml){

    }
}

推荐答案

如果每个单元格都是它自己的Shape(如Rectangle),会更容易,但是,使用当前代码,您可以使用类似...

It would be easier if each cell was it's own Shape (like a Rectangle), but, with your current code, you could use something like...

addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        int w = getWidth();
        int h = getHeight();
        selectedCell = null;
        for (int col = 0; col < 3 && selectedCell == null; col++) {
            for (int row = 0; row < 3; row++) {
                int x = (w / 3) * col;
                int y = (h / 3) * row;
                Rectangle cell = new Rectangle(x, y, w / 3, h / 3);
                if (cell.contains(e.getPoint())) {
                    System.out.println("In");
                    selectedCell = cell;
                    repaint();
                    break;
                }
            }
        }
    }
});

现在,所有这些操作就是循环遍历每一列和每一行,创建一个Rectangle并检查它是否包含MouseEvent点.如果找到匹配项,则会将单元格的矩形分配给实例字段,我曾在您的paint方法中突出显示该字段.

Now, all this does, is loops through each column and row, creating a Rectangle and checking to see if it contains the MouseEvent point. If it finds a match, it assigns the cell's rectangle to a instance field, which I used to highlight the in your paint method.

这就是为什么将每个单元格存储为RectangleList

This is why it would be easier to have each cell stored as a Rectangle in a List

作为可运行的示例...

As a runnable example...

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TicTacToeView extends JFrame {

    private JButton oButton, xButton;
    public JPanel board;
    public ArrayList<Shape> shapes;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new TicTacToeView();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public TicTacToeView() {
        shapes = new ArrayList<Shape>();
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        add(topPanel, BorderLayout.NORTH);
        add(board = new Board(), BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Board extends JPanel {

        private Rectangle selectedCell = null;

        public Board() {
    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int w = getWidth();
            int h = getHeight();
            selectedCell = null;
            for (int col = 0; col < 3 && selectedCell == null; col++) {
                for (int row = 0; row < 3; row++) {
                    int x = (w / 3) * col;
                    int y = (h / 3) * row;
                    Rectangle cell = new Rectangle(x, y, w / 3, h / 3);
                    if (cell.contains(e.getPoint())) {
                        System.out.println("In");
                        selectedCell = cell;
                        repaint();
                        break;
                    }
                }
            }
        }
    });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2d = (Graphics2D) g;

            g2d.setPaint(Color.WHITE);
            g2d.fill(new Rectangle2D.Double(0, 0, w, h));

            if (selectedCell != null) {
                g2d.setColor(Color.BLUE);
                g2d.fill(selectedCell);
            }

            g2d.setPaint(Color.BLACK);
            g2d.setStroke(new BasicStroke(4));
            g2d.draw(new Line2D.Double(0, h / 3, w, h / 3));
            g2d.draw(new Line2D.Double(0, h * 2 / 3, w, h * 2 / 3));
            g2d.draw(new Line2D.Double(w / 3, 0, w / 3, h));
            g2d.draw(new Line2D.Double(w * 2 / 3, 0, w * 2 / 3, h));
            //draw circles and xs by visiting elements in the array List.
            for (Shape shape : shapes) {
                g2d.setPaint(Color.BLUE);
                g2d.draw(shape);
            }
        }
    }

}

这篇关于井字游戏Java MouseListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆