Search code examples
javagraphic

How to add a mouselistener to images which are inserted using graph.drawimage()?


    public class buttonActionStart implements ActionListener {      // overwritten ActionListener

        public void actionPerformed (ActionEvent e){
        JFrame window2 = new JFrame("Main Game"); //create empty frame

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        window2.setSize(screenSize.width,screenSize.height);
        window2.add(new JPanel(){

        public void paintComponent(Graphics graph){
            super.paintComponent(graph);

            Board board = new Board();

            graph.drawString(board.sqaures[1].getName(),440,560);
            graph.drawString(board.sqaures[2].getName(),335,560);
            graph.drawString(board.sqaures[3].getName(),225,560);
            graph.drawString(board.sqaures[4].getName(),145,560);
            graph.drawString(board.sqaures[5].getName(),30,560);
            graph.drawString(board.sqaures[6].getName(),25,460);
            graph.drawString(board.sqaures[7].getName(),25,360);
            graph.drawString(board.sqaures[8].getName(),35,260);
            graph.drawString(board.sqaures[9].getName(),25,160);
            graph.drawString(board.sqaures[10].getName(),25,60);
            graph.drawString(board.sqaures[11].getName(),140,60);
            graph.drawString(board.sqaures[12].getName(),240,60);
            graph.drawString(board.sqaures[13].getName(),340,60);
            graph.drawString(board.sqaures[14].getName(),440,60);
            graph.drawString(board.sqaures[15].getName(),530,60);
            graph.drawString(board.sqaures[16].getName(),550,160);
            graph.drawString(board.sqaures[17].getName(),545,260);
            graph.drawString(board.sqaures[18].getName(),540,360);
            graph.drawString(board.sqaures[19].getName(),550,460);

            for (int i = 0; i < 6; i++)
                graph.drawRect(10 + (i*100),10,100,100);
            for (int j = 0; j < 4; j++)
                graph.drawRect(10,110 + (j*100),100,100);
            for (int k = 0; k < 4; k++)
                graph.drawRect(510,110 + (k*100),100,100);
            for (int l = 0; l < 6; l++)
                graph.drawRect(10 + (l*100),510,100,100);

            InputStream resource = UI.class.getResourceAsStream("player1.png");
            Image player1Image = null;
            try{
                player1Image = ImageIO.read(resource);
            }catch (IOException e){
                e.printStackTrace();
            }

            resource = UI.class.getResourceAsStream("player2.png");
            Image player2Image = null;
            try{
                player2Image = ImageIO.read(resource);
            }catch (IOException e){
                e.printStackTrace();
            }

            resource = UI.class.getResourceAsStream("player3.png");
            Image player3Image = null;
            try{
                player3Image = ImageIO.read(resource);
            }catch (IOException e){
                e.printStackTrace();
            }

            resource = UI.class.getResourceAsStream("player4.png");
            Image player4Image = null;
            try{
                player4Image = ImageIO.read(resource);
            }catch (IOException e){
                e.printStackTrace();
            }

            graph.drawImage(player1Image, 511, 511, null);
            graph.drawImage(player2Image, 585, 510, null);
            graph.drawImage(player3Image, 511, 585, null);
            graph.drawImage(player4Image, 585, 585, null);
            }
        });
        window2.setVisible(true);
    }
}

I am developing a kind of board game and these four images will serve as the player icon. I want the player to click the image which is assigned to them manually, then click somewhere else on the screen and the image will be "updated" to that position.

The "square" and board" will be my self-defined class.

I got another window which will have a "start" button and triggering the above graphic to be drawn on a new window.

I do found a lot of people suggesting to add image using JLabel and add the JLabel to the JFrame but I found that the image will cover my graphic.


Solution

  • Two options:

    1. Do as suggested and use a JLabel. By saying "the image will cover my graphics", you probably mean that transparent parts of the image will not show the background. If so, call JLabel.setOpaque(false).
    2. Add a MouseListener to the component for which you implement the paintComponent() method (probably a JFrame implementation). You will then get mouse events whenever the user clicks into the component. In the listener implementation, you then have to manually check whether the coordinates fall into the area of one of the images.

    Generally speaking, for a game I would prefer option 2: You use a blank canvas and draw your game scene into the canvas. User interactions are then managed by a general game logic object which delegates to game objects such as 'player', 'NPC', etc., based on the coordinates / pressed keys / etc.