2011-02-07 6 views
17

回転して表示するには問題があります。BufferedImageローテーションはうまく動いていると思いますが、実際にはそれを画面に描画することはできません。私のコード:回転するBufferedImageインスタンス

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(); 
    } 
} 

これは私のために働いていません。私は回転させたimg2dgに描画する方法を見つけることができませんでした。

編集:私はgに描画されている複数のオブジェクトがあるので、回転できません。私は物事を個別に回転できる必要があります。

答えて

18

Graphics2D.drawImage(image, affinetranform, imageobserver)を使用します。

以下のコード例は、画像を回転し、コンポーネントの中心に移動します。これは、結果のスクリーンショットです:

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どのように照らします。 –

32

たぶん、あなたはこのようなAffineTransformを使用してみてください。

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); 

は、この情報がお役に立てば幸いです。

4

イメージではなく、イメージを描画するためにグラフィックを回転しています。それはあなたが何の効果も見ない理由です。あなたは上の塗装されているグラフィックに回転を適用し、それが画像を回転描画します:

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(); 
} 

これはおそらく完全にあなたが期待するものを描きませんが、回転は座標原点を中心に展開します。あなたは、たとえば、回転前の座標変換を適用する必要があり、その中心の周りを回転する画像の場合:

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

Graphics2D Tutorialは、いくつかのより多くの例があります。

+0

私は別にBufferedImageを回転させ、その後、グラフィックスでそれを描きたいです。私は、回転させるべきではない他のものをGraphicsオブジェクトに持っています。 –

+0

イメージを描画した後、Graphicsの変換を元に戻すことができます。 – Durandal

+2

@Durandal 'Graphics'にはrotateメソッドがありますか?あるいは 'g2d.rotate()'を意味しますか? – user3437460