2012-12-04 2 views
0

안녕하세요, 자바에서 마우스 휠 이벤트를 연습하고 있습니다. 따라서 마우스 휠을 움직이면 자라는 원형과 새우를 만들었습니다. 이제는 마우스 포인터 옆의 화면에 "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)'그래픽 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); 
} 
관련 문제