2015-03-03 58 views
0

所以现在我有一个程序在我点击鼠标时绘制一个圆圈,当我点击鼠标时绘制一个方形图案,当我点击它时清除屏幕。拖动以绘制更多形状

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

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) { } 

} 
+0

为什么你的鼠标处理程序中有绘画代码?绘画应该只在paint()方法中完成。点击时,保存位置,然后在paint()中的该位置绘制形状。 – 2015-03-03 08:28:54

+0

@Baelynn我已经添加了一个完整的重新实现。 – laune 2015-03-03 09:41:48

回答

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) { } 
}