0

I have a program which at the moment I am trying to find the position of co-ordinates on the panel when clicked. So far I'm currently get 0,0. Any suggestions?

P.S - Sorry about the lack of comments...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.lang.Math;

public class ShapePanel extends JPanel{

  private JButton startButton, stopButton;
  private JTextField textField;
  private JLabel label;
  private Timer timer;
  private final int DELAY = 5;


  ArrayList<Shape> obj = new ArrayList<Shape>();


  public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new ShapePanel());
    frame.pack();
    frame.setVisible(true);
  }

  public ShapePanel(){

    DrawingPanel dpanel = new DrawingPanel();
    JPanel cpanel = new JPanel();

    startButton = new JButton("Start");
    stopButton = new JButton("Stop");

    cpanel.setLayout(new GridLayout(2,1));
    cpanel.add(startButton);
    cpanel.add(stopButton);


    dpanel.addMouseListener(new MouseListen());

    TimerListener tListen = new TimerListener();

    startButton.addActionListener(tListen);
    stopButton.addActionListener(tListen);
    add(cpanel);
    add(dpanel);

    timer = new Timer(DELAY, tListen);
    timer.start();

  }
  private class TimerListener implements ActionListener{


    public void actionPerformed(ActionEvent e){

      if (e.getSource() == timer){

        for (int i = 0; i < obj.size(); i++){
          obj.get(i).move();
        }

      }else if (e.getSource() == startButton){

        timer.start();
      }else if (e.getSource() == stopButton){

        timer.stop();
      }


      repaint();
    }
  }

  private class MouseListen implements MouseListener {


    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mouseClicked(MouseEvent e) {


      System.out.println(getX());
    }

}

  private class DrawingPanel extends JPanel{

    DrawingPanel(){

      setPreferredSize(new Dimension(400,400));
      setBackground(Color.pink);

    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      for(int i = 0; i < obj.size(); i++){

        obj.get(i).display(g);
      }

    }
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Paldan
  • 395
  • 1
  • 6
  • 16

2 Answers2

4

Check the following portion of your code:

public void mouseClicked(MouseEvent e) {


  System.out.println(getX());  
       // you are putting here only getX() which get the postion of panel
        // put e.getX() instead
}
Sage
  • 15,290
  • 3
  • 33
  • 38
1

You need to implement the mouselistener correctly. The MouesEvent carries the position of the MouseClick.

public void mouseClicked(MouseEvent e) {
    System.out.println(e.getPoint().x);
}
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84