0

私のクラスXでは、拡張JPanelして実装MouseListenerしました。MouseMotionListener

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クリック後の出力;

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

なぜ1、2、3ずつ増加しないのですか???

出力 (numPoints 用) (最初のマウスクリック)

  • 1
  • 2

***必要に応じて、さらに情報を提供します。


返信ありがとうございます。完全に編集された Java コードは次のとおりです。問題はまだ解決されていません。新しいアイデアと修正を楽しみにしています。前もって感謝します

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
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 に答える
0

おそらく、イベント オブジェクトのプロパティをチェックして、応答しているイベントの種類を確認する必要があります。あなたはおそらくif (arg0.getID() == MouseEvent.MOUSE_CLICKED)どこかにそこにいる必要があります。

于 2013-10-08T09:54:15.333 に答える