2013-08-23 4 views
0

Je travaille sur mon projet universitaire ... J'ai un problème avec la rotation d'un jlabel. Le JLabel que j'aimerais faire tourner est un vaisseau. L'étiquette devrait tourner, en fonction du cap du navire. J'ai l'en-tête affiché sur un jpanel séparé - je dessine le contrôle analogique au démarrage, la "main" (ou la flèche ou quoi que ce soit) du contrôle est dessiné plus tard avec getGraphics(). Voici le code:java swing - JLabel ne tourne pas

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 = NetBeans 7.2 .. J'ai lu que getGraphics ne doivent pas être utilisés, mais ... Je pense qu'il est trop tard pour ce genre de changements, le projet est trop grand .. et netbeans met quelques limitations quand il s'agit d'éditer initComponents() ..

Le problème est: L'étiquette ne tourne pas !!! 1er - Pourquoi n'est-il pas en rotation, et comment le faire pivoter (je voudrais rester avec getGraphics, il faudra beaucoup de temps pour reconstruire mon projet à nouveau avec la méthode paintComponent prioritaire etc ...

+0

L'objet graphique retourné par 'getGraphics()' n'est pas garanti pour être utile dans le cas où vous utilisez le mode swing. – kiheru

+0

D'accord, mais comment éviter les limitations imposées par l'idée de neatbeans (le constructeur de la fenêtre) ...?! – nilux

+0

Désolé, c'est quelque chose que je ne connais pas, je serais surpris si netbeans ne vous permettait pas – kiheru

Répondre