Does the mouseClicked() method work differently from mouseDragged() when it draws an existing object?

Hi,

Thanks to alot of help from generous souls, I now have a working mouseDragged() method. However, my question is why won't the mouseClicked() work the same way, enabling a user to double click in the window and draw a new version of the object created in the graphics method?

Surely, all it needs to know is the location of the mouseclicks and then repaint. I'm getting really confused with this concept of the mouse clicks and redrawing an existing object.

My current code is as follows,

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

public class PacMan2Clicks extends JFrame implements ActionListener,
        MouseMotionListener {
   
    public static int mode = 0;
    private static JButton rightButton;
    private static JButton leftButton;
    private Image pacmanImage;
    private int pacmanX, pacmanY;
    Area pacmanarea = new Area();
   
    public PacMan2Clicks() {
       
        Container contentPane = getContentPane();
       
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                setPacMan(g);
            }
        };
       
        panel.addMouseMotionListener(this);
        JPanel buttonPanel = new JPanel();
        leftButton = new JButton("Left");
        leftButton.addActionListener(this);
        buttonPanel.add(leftButton);
       
        rightButton = new JButton("Right");
        rightButton.addActionListener(this);
        buttonPanel.add(rightButton);
       
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
    }
   
    private void setPacMan(Graphics g) {
        Dimension d = getSize();
        int size = 100;
        Ellipse2D.Double head = new Ellipse2D.Double(0, 0, size, size);
        Ellipse2D.Double eye = new Ellipse2D.Double(size / 2 - 1, size / 5 - 1,
                size / 10, size / 10);
        GeneralPath mouth = new GeneralPath();
        mouth.moveTo(size, size / 4);
        mouth.lineTo(size / 8, size / 2);
        mouth.lineTo(size, size * 3 / 4);
        mouth.closePath();
       
        Area pacman = new Area(head);
        pacman.subtract(new Area(eye));
        pacman.subtract(new Area(mouth));
       
        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pacmanX, pacmanY);
        g2.setPaint(Color.yellow);
        g2.fill(pacman);
        g2.draw(pacman);
       
        pacmanarea.add(pacman);
    }
   
    public void mouseDragged(MouseEvent e) {
        pacmanX = e.getX();
        pacmanY = e.getY();
        repaint();
    }
   
    public void mouseReleased(MouseEvent e) {}
    public void mouseMoved(MouseEvent e){}
    public void mouseClicked(MouseEvent e){
        if (e.getClickCount() == 2) {
            pacmanX = e.getX();
            pacmanY = e.getY();
            repaint();
        }
    }
    public void mouseExited(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mousePressed(MouseEvent e) {}
   
    public void actionPerformed(ActionEvent event) {
        if (event.getSource().equals(rightButton)) {
           
        } else if (event.getSource().equals(leftButton)) {
           
        }
    }
   
    public static void main(String[] args) {
       
        PacMan2Clicks frame = new PacMan2Clicks();
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Any help or advice really appreciated.

Thanks
nhay59Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
YOu need

 panel.addMouseListener(this);

too
CEHJCommented:
And of course

public class PacMan2Clicks extends JFrame implements ActionListener,
        MouseMotionListener, MouseListener {
   
 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan ThiagarajanSenior consultantCommented:
yes is it differ form mousedrag method

mousedrag- click and drag the cursor at that time particular action to be done.

mouse clicked- whenever click, may be single or double at that time particular action to be done.
nhay59Author Commented:
Hi,

Thanks again for your help. I totally missed that one. It's nice to finally have a straightforward solution to a problem.

It's been a long day already!

Have a great weekend!

CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.