Why does my MouseListener not increment a variable

2019-09-02 01:18发布

问题:

In my Class X I have extended JPanel and implemented MouseListener, MouseMotionListener

int numPoints = 0;
Point controlPoints[] = new Point[10];

public void MousePressed (MouseEvent arg0) {
  if (numPoints < 5) 
    controlPoints[numPoints] = arg0.getPoint();
  numPoints++;
}

*When I System.out.println(numPoints); *

output after 3 CLICKS;

  • 0
  • 0
  • 2
  • 2
  • 3
  • 3
  • 5

WHY Does it not INCREMENT by 1, 2 ,3 ???

output (for numPoints) (first mouseclick)

  • 1
  • 2

***I will provide more information if necessary.


Thanks for the response. Here is the full edited javacode. The problem is still unsolved for me. I look forward to new ideas and corrections. Thanks in advance

package work2014.java.all;

java.awt.*;
import java.awt.event.*
import java.util.ArrayList;

import javax.swing.*;


 class jdrawPanel extends JPanel implements MouseListener, MouseMotionListener,            ActionListener
 {  


Graphics g;
boolean paint = false, edit = false;
Point d;
Point curvePoints[]= new Point[100];
Point[] controlPoints = new Point[99];
int n = 99, i = 3;
private int numPoints = 0;


public void paintComponent(Graphics g){
    addMouseListener(this);
    addMouseMotionListener(this);

    super.paintComponent(g);
      if(numPoints >= 0 && numPoints < controlPoints.length)
        Dot(numPoints,controlPoints,g);
         if(paint == true && numPoints >= 3){
            drawBezier(n, i, controlPoints, g);
    }

    this.requestFocus();
}


public void Dot(int numPoints, Point controlPoints[], Graphics g){

    draw red dot
}


public void drawBezier(int n, int i, Point controlPoints[], Graphics g)
{
    draw spline

} // End drawBezier




@Override
public void mouseMoved(MouseEvent arg0) {
    Point p = arg0.getPoint(); 
    if (equalPoint(p) == p)

        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    else
        setCursor(Cursor.getDefaultCursor());

}



@Override
public void mousePressed(MouseEvent arg0) {
    paint = true;
    Point d = arg0.getPoint();

    if(numPoints <= 10) {
        controlPoints[numPoints] = d;
                    numPoints++;
        System.out.println(numPoints);
    }
    repaint();

}



}

class Oving5_Frame extends JFrame
{  public Oving5_Frame()
 {  setTitle("Hello draW v2");
    setSize(600, 600);

  addWindowListener(new WindowAdapter()
     {  public void windowClosing(WindowEvent e)
        {  System.exit(0);
        }
     } );

  Container contentPane = getContentPane();
  contentPane.setLayout(new BorderLayout());
  contentPane.add(new jdrawPanel, BorderLayout.CENTER);
  contentPane.add(class_menu(), BorderLayout.NORTH);
}




    public class Oving5_launch
    { 
     public static void main(String[] args)
    {  
  JFrame frame = new Oving5_Frame();
     frame.show();
    }



}

回答1:

int numPoints = 0;
Point controlPoints[] = new Point[10];

public void MousePressed (MouseEvent arg0) {
  if (numPoints < 5) 
    controlPoints[numPoints++] = arg0.getPoint();
  System.out.println(numPoints);
}

This works fine for me (I didnt use the mouse event, and just used a loop to call the method 10 times.)



回答2:

You should probably be checking the properties of the event object to see what type of event you are responding to. You most likely need if (arg0.getID() == MouseEvent.MOUSE_CLICKED) in there somewhere.