2012-12-04 63 views
0

您好,我正在用Java中的mousewheel事件進行練習,因此當鼠標滾輪移動時,我製作了一個長大的蝦仁。現在我還想在鼠標指針旁邊的屏幕上顯示「MousWheel」的大小。任何人都可以告訴我一個如何做到這一點的例子嗎?在屏幕上獲得鼠標滾輪的大小

這就是我現在得到的。

public class MouseWheelPanel extends JPanel implements MouseWheelListener { 

private int grootte = 50; 

public MouseWheelPanel() { 
    this.addMouseWheelListener(this); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(Color.YELLOW); 
    g.fillOval(10, 10, grootte, grootte); 
} 


public void mouseWheelMoved(MouseWheelEvent e) { 
    // TODO Auto-generated method stub 
    String 
    grootte += e.getWheelRotation(); 
    repaint(); 
} 

} 
+1

'g.drawString(arguments)'只需在Graphics api中查找該方法即可。 – thatidiotguy

回答

1

假設你也對定位文本感興趣。查找FontMetrics。這會將大小字符串居中在圓圈中。

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.setColor(Color.YELLOW); 
    g.fillOval(10, 10, grootte, grootte); 

    String str = ""+grootte; 
    FontMetrics fm = g.getFontMetrics(); 
    Rectangle2D strBounds = fm.getStringBounds(str, g); 

    g.setColor(Color.BLACK); 
    g.drawString(str, 10 + grootte/2 - (int)strBounds.getWidth()/2, 10 + grootte/2 + (int)strBounds.getHeight()/2); 
} 
相關問題