2014-01-21 21 views
0

請問我正在研究在java中繪製二維圖形的問題,但我希望在鼠標移動時移動圖形,而不是拖動。您知道,隨着鼠標在畫布上移動,圖形也隨之移動。我該怎麼做呢?當鼠標移動不被拖動時,如何在畫布上移動2D圖形

import java.awt.Cursor; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.Image; 
    import java.awt.Point; 
    import java.awt.PointerInfo; 
    import java.awt.Toolkit; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; 
    import java.awt.event.MouseMotionListener; 
    import java.awt.geom.QuadCurve2D; 
    import java.awt.geom.Rectangle2D; 
    import java.awt.image.BufferedImage; 
    import java.awt.image.MemoryImageSource; 

    public class Pointer2QuadCurve extends javax.swing.JFrame { 

    private javax.swing.JPanel jPanel1; 
    double x1; 
    double y1; 

    public Pointer2QuadCurve() { 
    initComponents(); 
    jPanel1.addMouseListener(new MouseListener() { 

     @Override 
     public void mouseClicked(MouseEvent e) { 

     } 

     @Override 
     public void mousePressed(MouseEvent e) { 

     } 

     @Override 
     public void mouseReleased(MouseEvent e) { 

     } 

     @Override 
     public void mouseEntered(MouseEvent e) { 
      int[] pixels = new int[16 * 16]; 
      Image image = Toolkit.getDefaultToolkit().createImage(
        new MemoryImageSource(16, 16, pixels, 0, 16)); 
      Cursor transparentCursor 
        = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "invisibleCursor"); 
      //paintWithPointer(getGraphics()); 
      setCursor(transparentCursor); 
     } 

     @Override 
     public void mouseExited(MouseEvent e) { 

     } 
    }); 
    jPanel1.addMouseMotionListener(new MouseMotionListener() { 

     @Override 
     public void mouseDragged(MouseEvent e) { 

     } 

     @Override 
     public void mouseMoved(MouseEvent e) { 
      //Point p = getMousePosition(); 
      x1 = e.getX(); 
      y1 = e.getY(); 
      jPanel1.repaint(); 
     } 
    }); 
} 



private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 

    pack(); 
}      


public static void main(String args[]) { 

    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Pointer2QuadCurve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 

    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new Pointer2QuadCurve().setVisible(true); 
     } 
    }); 
} 


public void paint(Graphics g) { 
    Graphics2D g2d = (Graphics2D) g; 
    // draw Rectangle2D.Double 
    g2d.draw(new Rectangle2D.Double(x1, y1, 
      Math.random() + 10, 
      Math.random() + 15)); 
} 

}

+1

從['MouseMoitionListener'](http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html)開始。當'mouseMoved'被觸發時,更新「對象」的位置並重新繪製 – MadProgrammer

+0

謝謝,但將繪製的代碼放在paint方法內嗎? –

+0

@MadProgrammer Ugg ..我的回答最初建議使用'MouseListener',但在我腦海裏我在想..我確定有一個'mouseMoved(..)'事件! –

回答

2
  • 添加MouseMotionListener到自定義彩繪的組成部分。
  • 收聽mouseMoved事件。
  • MouseEvent獲取Point
  • Point處繪製形狀。
+0

爲什麼第三點?看起來像過度殺傷'MouseEvent'會給你點,它可以根據需要轉換爲其他上下文,然後源組件上下文...只是說... – MadProgrammer

+0

@MadProgrammer第三點是使用'MouseListener' 。 :P編輯.. –

+1

是的,只是看起來像矯枉過正給我...但褲子看起來像一個矯枉過正的我;)+1 – MadProgrammer