1

基本的には、「マウスが入力された場所」というテキストに続けて座標を描画するだけです。mouseMoved が実装されていない場合、私のコードはこれを正常に実行します。その場合、マウスが入ったことを示すことはなく、「マウスが移動した場所」に直接移動します。パネルに入るとその場所でマウスも移動するため、mouseMoved がこれを行う方法を理解できます。動きをベクトルに保存して表示しようとしましたが (このプロジェクトにはまだ必要ありません)、マウスが入力されたことをまだペイントしていません。それは、これを引き起こしているもっと深い何かがあると私に思わせます。簡単な修正はありますか?

生の未完成のコードをお詫びします (必要なすべてのことをまだ実行していないという点で未完成ですが、対応する GUI でコンパイルして実行します)。

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

public class DrawingArea extends JPanel {

int x1, x2, y1, y2;
int shapeType;
char mouseAction;
Vector<String> eventList = new Vector<String>();

public DrawingArea() {
    initialize();
}

public void initialize() {


    addMouseListener(new MouseAdapter() {

      public void mouseEntered(MouseEvent m) {
          x1 = m.getX();
          y1 = m.getY();
          mouseAction = 'a';
          shapeType = 0;
          repaint();

      }

     public void mouseExited (MouseEvent m) {
         x1 = m.getX();
         y1 = m.getY();
         mouseAction = 'b';
         shapeType = 0;
         repaint();
     }

      public void mousePressed(MouseEvent m) {

          x1 = m.getX();
          y1 = m.getY();
          mouseAction = 'd';
          shapeType = 0;
          repaint();
      }
      public void mouseReleased(MouseEvent m) {

          x2 = m.getX();
          y2 = m.getY();
          repaint();
      }
    });

  addMouseMotionListener(new MouseMotionAdapter() {

      public void mouseMoved (MouseEvent m) {

          x2 = m.getX();
          y2 = m.getY();
          mouseAction = 'c';
          shapeType = 0;
          repaint();
      }

       public void mouseDragged(MouseEvent m) {

             x2 = m.getX();
             y2 = m.getY();
             repaint();
          }
      });
}

public void output(String event, MouseEvent m) {

}

public void setShapeType(int num) {

    if (num == 1) {
        shapeType = 1;
        mouseAction = 'z';
        repaint();
    }
    else if (num == 2) {
        shapeType = 2;
        mouseAction = 'z';
        repaint();
    }
    else if (num == 3) {
        shapeType = 0;
        mouseAction = 'z';
        repaint();
    }
}

public void paint(Graphics g) {
    super.paint(g);

    if (shapeType == 1) {

        g.drawString("Rectangle", 25,25);

    }
    else if (shapeType == 2)
        g.drawString("Circle", 25, 25);

    if (mouseAction == 'a') {
        g.drawString("Mouse entered at (" + x1+ ", " + y1 + ")", 25, 25);
    }

    else if (mouseAction == 'b') {
        g.drawString("Mouse exited at (" + x1 + ", " + y1 + ")", 25, 25);

    }

    else if (mouseAction == 'c') {
        g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25);

    }

    else if (mouseAction == 'd') 
        g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25);
}
}

参考までに、GUI コードを次に示します。

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

public class SimpleGUI extends JFrame {

DrawingArea drawArea = new DrawingArea();


public SimpleGUI() {
    createGUI();
}


public void createGUI() {

    JFrame main = new JFrame();
    main.setVisible(true);
    main.setSize(500, 600);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel right = new JPanel(new GridLayout(20,1));
    JPanel bottom = new JPanel();

    JButton rect = new JButton("Rectangle");
    JButton circ = new JButton("Circle");
    JButton erase = new JButton("Erase");
    JButton send = new JButton("Send");

    JTextField text = new JTextField(30);

    right.add(rect);
    right.add(circ);
    right.add(erase);

    bottom.add(text);
    bottom.add(send);

    drawArea.setBackground(Color.WHITE);

    main.add(drawArea, BorderLayout.CENTER);
    main.add(right, BorderLayout.EAST);
    main.add(bottom, BorderLayout.SOUTH);

    rect.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(1);
        }
    });

    circ.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(2);
        }

    });

    erase.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(3);
        }
    });

}




public static void main (String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SimpleGUI();
        }
    });
}

}
4

1 に答える 1

2

問題はリスナーにあるのではなく、レンダリングロジックにあります...

if (mouseAction == 'a') {
    g.drawString("Mouse entered at (" + x1 + ", " + y1 + ")", 25, 25);
} else if (mouseAction == 'b') {
    g.drawString("Mouse exited at (" + x1 + ", " + y1 + ")", 25, 25);
} else if (mouseAction == 'c') {
    g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25);
} else if (mouseAction == 'd') {
    g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25);
}

基本的に、マウスが入ると、a mouseActionofがトリガーされ、その後にaaofが続きます。mouseActionc

再描画マネージャーは、それをオーバーライドするaため、レンダリングする時間がないことを推測します。c

ペイントコードを更新してモーションとアクションのロジックを分離すると、違いがわかるはずです。

g.drawString("Mouse " + (mouseEntered ? "entered" : "exited") + " at (" + x1 + ", " + y1 + ")", 25, 50);
if (mouseAction == 'c') {
    g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25);
} else if (mouseAction == 'd') {
    g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25);
}

fromとfromに設定された単純な値でmouseEnteredあるフィールドを追加しましたbooleantruemouseEnteredfalsemouseExited

paintComponentまた、慣例では、よりもメソッドをオーバーライドすることを好みますpaintpaintComponentダブルバッファリングされていますが、そうでpaintはありません。

于 2012-12-01T20:39:56.700 に答える