当前位置: 首页 > 知识库问答 >
问题:

在java中将图像链接到MouseEvent

充鑫鹏
2023-03-14

一些背景,我正在开发Java游戏,我正在使用Netbeans来构建它,我目前有2个java文件

App.java

董事会.java

现在我可以创建和显示一个简单的棋盘与所有的棋子在正确的位置我的问题是分配任何鼠标事件到这些棋子

目前,我使用textpad编写测试代码,并且没有图像的文件夹链接,并且已经能够让鼠标事件在那里工作,因此我知道事件的代码没有问题。

但是现在我正在Netbean中编写程序清理器,鼠标事件不再起作用了

所以我觉得我没有把它们正确地连接到新的棋盘和新的棋盘上

这是netbean中的棋盘(不工作版本)

package chessgame;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Board extends JFrame implements MouseListener, MouseMotionListener{

    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;

    public Board() {
        Dimension boardSize = new Dimension(600, 600);

        //  This is a Layered Pane for this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        //Add a chess board to the Layered Pane
        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout(new GridLayout(8, 8));
        chessBoard.setPreferredSize(boardSize);
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel(new BorderLayout());
            chessBoard.add(square);

            int row = (i / 8) % 2;
            if (row == 0) {
                square.setBackground(i % 2 == 0 ? Color.white : Color.gray);
            } else {
                square.setBackground(i % 2 == 0 ? Color.gray : Color.white);
            }
        }
        // Setting up the Initial Chess board.
        //White Side
        for (int i = 8; i < 16; i++) {
            pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhitePawn.png")));
            panels = (JPanel) chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
        panels = (JPanel) chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKnight.png")));
        panels = (JPanel) chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKnight.png")));
        panels = (JPanel) chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteBishup.png")));
        panels = (JPanel) chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteBishup.png")));
        panels = (JPanel) chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKing.png")));
        panels = (JPanel) chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteQueen.png")));
        panels = (JPanel) chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
        panels = (JPanel) chessBoard.getComponent(7);
        panels.add(pieces);

        //Black Side
        for (int i = 48; i < 56; i++) {
            pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackPawn.png")));
            panels = (JPanel) chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackRook.png")));
        panels = (JPanel) chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKnight.png")));
        panels = (JPanel) chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKnight.png")));
        panels = (JPanel) chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackBishup.png")));
        panels = (JPanel) chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackBishup.png")));
        panels = (JPanel) chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKing.png")));
        panels = (JPanel) chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackQueen.png")));
        panels = (JPanel) chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackRook.png")));
        panels = (JPanel) chessBoard.getComponent(63);
        panels.add(pieces);
    }

    //Mouse Events For All Piece Interactions 

    //This method checks if there is a piece present on a particular square.
    private Boolean piecePresent(int x, int y) {
        Component c = chessBoard.findComponentAt(x, y);
        if (c instanceof JPanel) {
            return false;
        } else {
            return true;
        }
    }

    //This is a method to check if a piece is a Black piece.
    private Boolean checkWhiteOponent(int newX, int newY) {
        Boolean oponent;
        Component c1 = chessBoard.findComponentAt(newX, newY);
        JLabel awaitingPiece = (JLabel) c1;
        String tmp1 = awaitingPiece.getIcon().toString();
        if (((tmp1.contains("Black")))) {
            oponent = true;
        } else {
            oponent = false;
        }
        return oponent;
    }

    /*
     This method is called when we press the Mouse. So we need to find out what piece we have
     selected. We may also not have selected a piece!
     */
    public void mousePressed(MouseEvent e) {
        chessPiece = null;
        Component c = chessBoard.findComponentAt(e.getX(), e.getY());
        if (c instanceof JPanel) {
            return;
        }

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel) c;
        initialX = e.getX();
        initialY = e.getY();
        startX = (e.getX() / 75);
        startY = (e.getY() / 75);
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }

    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) {
            return;
        }
        chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
    }

    /*
     This method is used when the Mouse is released...we need to make sure the move was valid before
     putting the piece back on the board.
     */
    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}

这是我用textpad编写的测试代码(Working Mousevents版本)

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;


public class ChessProject extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;


    public ChessProject(){
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);
        layeredPane.addMouseListener(this);
        layeredPane.addMouseMotionListener(this);

        //Add a chess board to the Layered Pane
        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.white : Color.gray );
            else
                square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
        }

        // Setting up the Initial Chess board.
        for(int i=8;i < 16; i++){
            pieces = new JLabel( new ImageIcon("WhitePawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKing.png") );
        panels = (JPanel)chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
        panels = (JPanel)chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(7);
        panels.add(pieces);
        for(int i=48;i < 56; i++){
            pieces = new JLabel( new ImageIcon("BlackPawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKing.png") );
        panels = (JPanel)chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackQueen.png") );
        panels = (JPanel)chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(63);
        panels.add(pieces);
    }

    /*
        This method checks if there is a piece present on a particular square.
    */
    private Boolean piecePresent(int x, int y){
        Component c = chessBoard.findComponentAt(x, y);
        if(c instanceof JPanel){
            return false;
        }
        else{
            return true;
        }
    }

    /*
        This is a method to check if a piece is a Black piece.
    */
    private Boolean checkWhiteOponent(int newX, int newY){
        Boolean oponent;
        Component c1 = chessBoard.findComponentAt(newX, newY);
        JLabel awaitingPiece = (JLabel)c1;
        String tmp1 = awaitingPiece.getIcon().toString();
        if(((tmp1.contains("Black")))){
            oponent = true;
        }
        else{
            oponent = false;
        }
        return oponent;
    }

    /*
        This method is called when we press the Mouse. So we need to find out what piece we have
        selected. We may also not have selected a piece!
    */
    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
        if (c instanceof JPanel)
            return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        initialX = e.getX();
        initialY = e.getY();
        startX = (e.getX()/75);
        startY = (e.getY()/75);
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }

    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
     }

    /*
        This method is used when the Mouse is released...we need to make sure the move was valid before
        putting the piece back on the board.
    */
    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

    }
    public void mouseMoved(MouseEvent e) {
   }
    public void mouseEntered(MouseEvent e){

    }
    public void mouseExited(MouseEvent e) {

    }

    /*
        Main method that gets the ball moving.
    */
    public static void main(String[] args) {
        JFrame frame = new ChessProject();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}

目前,mosueevents设置的唯一功能是允许用户在棋盘上拖动任何他们想要的棋子

希望有人能看到我在事件链接方面出了什么问题

提前感谢您能提供的任何帮助

共有1个答案

巫马承德
2023-03-14

所以,在您的< code>ChessProject中,您做到了...

layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);

但是在您的< code>Board项目中,您没有进行这样的调用。记住,如果你想被告知鼠标事件,你必须为它们注册一个监听器

 类似资料:
  • 我有一个使用Maven创建的spring项目。下面是我的目录结构。 我想添加css图像到这个项目。为此,我在web-inf目录中创建了一个resources文件夹,并在其中放置了一个图像。对于我的dispatcher servlet xml,我添加了3行与MVC有关的内容。其中一行位于xmlns:mvc中,另外2行位于模式位置的最后2行: http://www.springframework.or

  • 本文向大家介绍如何将图像用作HTML中的链接?,包括了如何将图像用作HTML中的链接?的使用技巧和注意事项,需要的朋友参考一下 要将图像用作HTML中的链接,请使用<img>标记以及带有href属性的<a>标记。<img>标记用于在网页中使用图像,<a>标记用于添加链接。在图片标签src属性下,添加图片的网址。这样,还可以添加高度和宽度。 示例 您可以尝试运行以下代码以将图像用作HTML中的链接

  • 在本章中,我们将了解Grav中的图像链接。 Grav允许您将图像从一个页面链接到另一个页面,甚至链接到远程页面。 如果您使用HTML链接文件,那么在Grav中很容易理解图像链接。 使用此结构,我们将了解如何使用不同类型的链接在页面中显示媒体文件。 此结构下的每个文件夹都包含图像,并且/02.green/img01下有一个特殊目录,它充当页面但仅包含媒体文件。 让我们看一下基于Grav markdo

  • 我们已经了解了如何使用文本创建超文本链接,我们还学习了如何在我们的网页中使用图像。 现在,我们将学习如何使用图像来创建超链接。 例子 (Example) 将图像用作超链接很简单。 我们只需要在文本位置的超链接内使用图像,如下所示 - <!DOCTYPE html> <html> <head> <title>Image Hyperlink Example</title> </

  • 问题内容: 这是我用于在Excel中将图像作为图标插入的方法: 这是我使用此方法创建图标的方法: 是否可以在此图标中放置一个超链接以转到同一电子表格中的另一个工作表或网站? 我读到的内容显然没有POI支持,但是可以使用底层的低级API来实现。但是我还不能真正成功地使用它。 有什么建议? 问题答案: 如果只支持可以,那么实际上可以使用底层的低级对象来完成。 如何开始?使用其中包含超链接的图片来创建工

  • 我可以在Apache POI中为单元格设置超链接,但我不知道如何将超链接放入图像中(我正在使用XSSF) 以下是放置单元格超链接的功能: 下面是将图像放入指定单元格的函数: = 提前感谢!