拖动以绘制更多形状

时间:2015-03-03 08:08:07

标签: java mouseevent listener

所以现在我有一个程序,当我右键单击时绘制一个圆圈,当我左键单击时绘制一个正方形,当我移动单击它时清除屏幕。

•我想要做的是能够拖动并让鼠标在拖动时留下一串数字。我怎么做?这是我的计划。

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

    public class SimpleStamper extends JPanel implements MouseListener {

    public static void main(String[] args) {
        JFrame window = new JFrame("Simple Stamper");
        SimpleStamper content = new SimpleStamper();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(120,70);
        window.setSize(400,300);
        window.setVisible(true);
    }

    // ------------------------------------------------------------------

    public SimpleStamper() {
        setBackground(Color.BLACK);
        addMouseListener(this);
    }


    public void mousePressed(MouseEvent evt) {

        if ( evt.isShiftDown() ) {



            repaint();
            return;
        }

        int x = evt.getX();  
        int y = evt.getY();  

        Graphics g = getGraphics(); 

        if ( evt.isMetaDown() ) {

            g.setColor(Color.RED);  
            g.fillOval( x - 30, y - 30, 60, 60 );
            g.setColor(Color.RED); 
            g.drawOval( x - 30, y - 30, 60, 60 );
        }
        else {

            g.setColor(Color.PINK);   
            g.fillRect( x - 15, y - 15, 30, 30 );
            g.setColor(Color.PINK); 
            g.drawRect( x - 15, y - 15, 30, 30 );
        }

        g.dispose();  

    } 

    public void mouseEntered(MouseEvent evt) { }
    public void mouseExited(MouseEvent evt) { }
    public void mouseClicked(MouseEvent evt) { }
    public void mouseReleased(MouseEvent evt) { }

} 

1 个答案:

答案 0 :(得分:0)

基本上你需要添加一个MouseMotionListener,与MouseListener一样。

public class SimpleStamper 
extends JPanel implements MouseListener, MouseMotionListener {
    List<Shape> shapes = new ArrayList<>();
    public void mouseDragged(MouseEvent evt) {
        int x = evt.getX();  
        int y = evt.getY();
        //... create another shape and add it to the list of shapes
        // (see Adrian's comment)
    }
    public void mouseMoved(MouseEvent evt) {
    }
//...
}

要继续相同的形状,必须将“当前”形状存储在类变量中,以便在mousePressed中完成。

请注意,这只会产生跟踪鼠标移动的宽线。如果要绘制不同的形状,则必须跟踪最后的x和y,并且仅当距离大于某个参数DMIN时才创建另一个形状。

以下是完全重写。

public class Drag 
extends JPanel implements MouseListener, MouseMotionListener {

public static void main(String[] args) {
  JFrame window = new JFrame("Simple Stamper");
  Drag content = new Drag();
  window.setContentPane(content);
  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  window.setLocation(120,70);
  window.setSize(400,300);
  window.setVisible(true);
}

public Drag() {
    setBackground(Color.BLACK);
    addMouseListener(this);
    addMouseMotionListener( this );
}

// ------------------------------------------------------------------
private java.util.List<Shape> shapes = new ArrayList<>();
  private enum Form {
    CIRCLE, SQUARE;
  }
  private Form lastForm;
  private int lastX;
  private int lastY;
  private static final int DMIN = 20;

  private void addShape( Form form, int x, int y, int a ){
    switch( form ){
    case CIRCLE:
      shapes.add( new Ellipse2D.Double( x-a/2.0, y-a/2.0, a, a ) );
      break;
    case SQUARE:
      shapes.add( new Rectangle2D.Double( x-a/2.0, y-a/2.0, a, a ) );
      break;
    }
    repaint();
  }

  public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
    for( Shape s: shapes ){
      if( s instanceof Rectangle2D ){
        g2.setColor(Color.PINK);
      } else {
        g2.setColor(Color.RED);  
      }
      g2.fill( s );
      g2.draw( s );
    }
  }

  public void mouseDragged(MouseEvent evt) {
    int x = evt.getX();  
    int y = evt.getY();
    int dx = lastX - x; 
    int dy = lastY - y; 
    if( dx*dx + dy*dy > DMIN*DMIN ){
        lastX = x;
        lastY = y;
        addShape( lastForm, lastX, lastY, 60 );
    }
  }
  public void mouseMoved(MouseEvent evt) {
}

public void mousePressed(MouseEvent evt) {
  if ( evt.isShiftDown() ) {
    shapes.clear();
    repaint();
    return;
  }
  lastX = evt.getX();
  lastY = evt.getY();
  if ( evt.isMetaDown() ) {
     lastForm = Form.CIRCLE;
  } else {
     lastForm = Form.SQUARE;
  }
  addShape( lastForm, lastX, lastY, 60 );
} 

public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}