2015-03-03 59 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) { } 
}