0

在我的课堂上,X我扩展JPanel并实施了MouseListenerMouseMotionListener

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

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

*当我 System.out.println(numPoints); *

3 CLICKS 后输出;

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

为什么它不增加 1, 2 ,3 ???

输出(对于 numPoints)(第一次鼠标点击)

  • 1
  • 2

***如有必要,我将提供更多信息。


感谢您的回复。这是完整编辑的javacode。这个问题对我来说仍然没有解决。我期待新的想法和更正。提前致谢

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();
    }



}
4

2 回答 2

0

您可能应该检查事件对象的属性以查看您正在响应的事件类型。你很可能需要if (arg0.getID() == MouseEvent.MOUSE_CLICKED)在那里的某个地方。

于 2013-10-08T09:54:15.333 回答
0
int numPoints = 0;
Point controlPoints[] = new Point[10];

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

这对我来说很好(我没有使用鼠标事件,只是使用循环调用该方法 10 次。)

于 2013-10-08T09:52:24.130 回答