2011-02-07 10 views
17

Non riesco a visualizzare BufferedImage ruotato. Penso che la rotazione funzioni bene, ma non riesco a disegnarla sullo schermo. Il mio codice:Rotating BufferedImage instance

Class extends JPanel { 
    BufferedImage img; 
    int rotation = 0; 

    public void paintComponent(Graphics g) { 
     g.clearRect(0, 0, getWidth(), getHeight()); 
     img2d = img.createGraphics(); 
     img2d.rotate(Math.toRadians(rotation), img.getWidth()/2, img.getHeight()/2); 
     g.drawImage(img, imgx, imgy, null); 
     this.repaint(); 
    } 
} 

Questo non funziona per me. Non ho trovato alcun modo per disegnare lo img2d ruotato su g.

MODIFICA: ho più oggetti che vengono disegnati su g, quindi non posso ruotarlo. Devo essere in grado di ruotare le cose individualmente.

risposta

18

Vorrei utilizzare Graphics2D.drawImage(image, affinetranform, imageobserver).

L'esempio di codice riportato di seguito ruota e converte un'immagine al centro del componente. Questo è uno screenshot del risultato:

screenshot

public static void main(String[] args) throws IOException { 
    JFrame frame = new JFrame("Test"); 

    frame.add(new JComponent() { 

     BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")); 

     @Override 
     protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 

       // create the transform, note that the transformations happen 
       // in reversed order (so check them backwards) 
       AffineTransform at = new AffineTransform(); 

       // 4. translate it to the center of the component 
       at.translate(getWidth()/2, getHeight()/2); 

       // 3. do the actual rotation 
       at.rotate(Math.PI/4); 

       // 2. just a scale because this image is big 
       at.scale(0.5, 0.5); 

       // 1. translate the object so that you rotate it around the 
       // center (easier :)) 
       at.translate(-image.getWidth()/2, -image.getHeight()/2); 

       // draw the image 
       Graphics2D g2d = (Graphics2D) g; 
       g2d.drawImage(image, at, null); 

       // continue drawing other stuff (non-transformed) 
       //... 

     } 
    }); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
} 
+0

@Squareoot Quanto illuminante. –

32

Forse si dovrebbe provare a utilizzare AffineTransform come questo:

AffineTransform transform = new AffineTransform(); 
    transform.rotate(radians, bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); 
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); 
    bufferedImage = op.filter(bufferedImage, null); 

Spero che questo aiuti.

4

Si sta ruotando la grafica per il disegno nell'immagine, non l'immagine. Ecco perché non vedi alcun effetto. Applicare la rotazione per la grafica che si sta pittura su e sarà disegnare l'immagine ruotata:

public void paintComponent(Graphics g) { 
    g.clearRect(0, 0, getWidth(), getHeight()); 
    g.rotate(Math.toRadians(rotation), img.getWidth()/2, img.getHeight()/2); 
    g.drawImage(img, imgx, imgy, null); 
    this.repaint(); 
} 

Questo non sarà probabilmente disegnare tutto ciò che vi aspettate, la rotazione ruoterà intorno alla origine delle coordinate. Per l'immagine da ruotare attorno al suo centro è necessario applicare una traduzione di coordinate prima della rotazione, per esempio:

g.translate(imgx >> 1, imgy >> 1); 

Il Graphics2D Tutorial ha alcuni esempi.

+0

voglio ruotare il BufferedImage separatamente e poi disegnare con la grafica. Ho altre cose sull'oggetto Graphics che non dovrebbero essere ruotate. –

+0

È possibile annullare la trasformazione della grafica dopo aver disegnato l'immagine. – Durandal

+2

@Durandal Sei sicuro che 'Graphics' ha un metodo di rotazione? O vuoi dire 'g2d.rotate()'? – user3437460