4

So I wrote this code in java. It should print "Mouse Click" when I press mouse button, but it does nothing.

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class mainPanel extends JPanel implements MouseListener {

    List<Integer> listLoc;
    List<ImageIcon> listImg;

    ImageIcon objPpc;
    ImageIcon ttlSpace;

    public mainPanel(){
        loadImg();
        fillBck(ttlSpace);
        addTtl(objPpc, 1);
        repaint();
    }

    public void fillBck(ImageIcon fillingImg){
        for(int i = 0; i < 16; i++){
            addTtl(fillingImg, i);
        }
    }

    public void loadImg(){
        objPpc = new ImageIcon ("img/obj/ppcBuild1.png");
        ttlSpace = new ImageIcon ("img/ttl/spaceBuild1.png");
        listLoc = new ArrayList<Integer>();
        listImg = new ArrayList<ImageIcon>();
    }

    public void addTtl(ImageIcon addedImg, int addedLoc){

        System.out.println("----- addTtl -----");
        System.out.println(addedImg);
        listImg.add(addedImg);
        System.out.println(addedLoc);
        listLoc.add(addedLoc);
        System.out.println("----- /addTtl -----");
        System.out.println("");
    }

    public void paint (Graphics g){
        super.paint(g);
        for (int i = 0; i < listImg.size(); i ++) {
            g.drawImage((listImg.get(i)).getImage(), (listLoc.get(i) * (this.getWidth() / 16)), 0, (this.getWidth() / 16) , this.getHeight(), null);

        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Mouse Click");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

The java console only prints this, and no error codes:

----- addTtl -----

img/ttl/spaceBuild1.png
0
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
1
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
2
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
3
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
4
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
5
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
6
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
7
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
8
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
9
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
10
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
11
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
12
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
13
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
14
----- /addTtl -----

----- addTtl -----
img/ttl/spaceBuild1.png
15
----- /addTtl -----

----- addTtl -----
img/obj/ppcBuild1.png
1
----- /addTtl -----

When I press any mouse button (even wheel[MOUSE3]) on screen, it does nothing.

How could I fix that?

2
  • 1
    please learn java naming conventions and stick to them
    – kleopatra
    Dec 26, 2011 at 11:40
  • 2
    btw, in Swing the method to override for custom painting is paintComponent (not paint)
    – kleopatra
    Dec 26, 2011 at 11:43

2 Answers 2

6

Your code never adds any MouseListener to the panel. You should have this line somewhere:

this.addMouseListener(this);

When you implement MouseListener, that means that you're able to listen to mouse events. To actually listen to mouse events, the listener must be added (registered) to some component that produces mouse events.

0
3

You need to register mouseListener add

addMouseListener(this);

Also See

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.