2013-08-23 70 views
0

我正在研究我的大學項目......我有一個jlabel的旋轉問題。我想旋轉的JLabel是一艘船。標籤應該旋轉,取決於船的航向。我的標題顯示在單獨的jpanel上 - 我在啓動時繪製模擬控件,稍後使用getGraphics()繪製控件的「手形」(或箭頭或其他)。下面是一些代碼:java swing - JLabel不旋轉

public void drawHeading (int getheading) { 
    int course = getheading; 
    int x,y; 
    double radians; 
    // appendEvent (" heading " + Integer.toString(course)); 

    if (course != oldHeading){ 
     //HeadingControl is the jpanel where I draw the analogue heading control 
     HeadingControl.validate(); 
     HeadingControl.repaint(); 
    } 
    oldHeading = course; 
    radians = Math.toRadians(course) - Math.PI/2; 
    //this puts info in textfield 
    appendEvent (" course " + Integer.toString(course)); 

    x = 120 + (int)(70*Math.cos(radians)); 
    y = 80 + (int)(70*Math.sin(radians)); 
    //i get the graphics .. then add the "hand" 
    Graphics2D gfx = (Graphics2D) HeadingControl.getGraphics(); 
    gfx.setColor(Color.red); 
    gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 

    gfx.drawLine(x, y, 120, 80); 
    gfx.drawLine(x, y, 120, 80); 
    gfx.drawLine(x, y, 120, 80); 

    AffineTransform tx = new AffineTransform(); 
    tx.rotate(Math.toRadians(90)); 
    //the label is not rotated, tried ship.rotate(radians) (gfx2.rotate(radians) ... //didn't work 
    Graphics2D gfx2 = (Graphics2D) ship.getGraphics(); 
    gfx2.transform(tx); 

} 

IDE = 7.2的NetBeans。我讀過的getGraphics不應該使用,但是...我認爲這是爲時已晚,這樣樣的變化,該項目是太大..和netbeans提出一些限制,當編輯initComponents()..

問題是:標籤不旋轉!第一 - 爲什麼它不旋轉,以及如何旋轉它(我想留在getGraphics,這將是耗時重新重建我的項目重寫paintComponent方法等...

+0

使用耗時的路由,因爲如果你使用的是swing,這是正確的方法,getGraphics()返回的圖形對象不能保證有用 – kiheru

+0

好吧,但是如何避免neatbeans ide(窗口生成器)設置的限制......? – nilux

+0

對不起,這是我不熟悉的東西,如果netbeans不允許你正確地進行繪圖。 – kiheru

回答