2014-12-03 96 views
0

爲了繪製和旋轉一些圖像,我重寫了一個Jpanel的paintcomponent,但它具有旋轉其他東西的不必要的副作用,例如JLabel添加到JPanel等。我已經嘗試在繪製後旋轉回來圖像但JLabels似乎抖動。Java Swing PaintComponent旋轉jlabels

請注意,我在圖像中的不同點(不是圖像的中心)周圍旋轉每個圖像,因此在圖像緩衝區內旋轉圖像不適合?

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 

    g2.rotate(Math.toRadians(+(angle)), 137, 188); 
    g2.drawImage(image1, 125, 131, this); 
    g2.rotate(Math.toRadians(-(angle)), 137, 188); 

    g2.rotate(Math.toRadians(+(angle2)), 137, 188); 
    g2.drawImage(image2, 125, 131, this); 
    g2.rotate(Math.toRadians(-(angle2)), 137, 188); 

} 

回答

2

有幾件事情可以做

  1. 創建Graphics上下文的副本,使用類似Graphics2D g2d = (Graphics2D)g.create();。完成繪畫後,請複製副本上的Graphics#dispose以釋放它可能分配的任何資源。這基本上允許您更改副本的屬性,而不影響原始內容,但仍繪製到相同的緩衝區。
  2. Graphics2D獲取原始AffineTransform的副本,那麼你可以申請自己的AffineTransform和重置它,當你完成後,見Graphics2D#get/setTransform

例如...

Spinny

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.AffineTransform; 
import java.awt.geom.Path2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestRotate01 { 

    public static void main(String[] args) { 
     new TestRotate01(); 
    } 

    public TestRotate01() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private CrossShape prop; 

     private double angle; 

     public TestPane() { 
      prop = new CrossShape(50, 50); 
      Timer timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        angle += 5; 
        repaint(); 
       } 
      }); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      AffineTransform current = g2d.getTransform(); 

      int x = 25; 
      int y = (getHeight() - prop.getBounds().height)/2; 

      AffineTransform at = new AffineTransform(); 
      at.translate(x, y); 
      at.rotate(Math.toRadians(angle), prop.getBounds().width/2, prop.getBounds().height/2); 
      g2d.setTransform(at); 
      g2d.setColor(Color.RED); 
      g2d.draw(prop); 

      // Reset... 
      // Equally, you could dispose of the g2d and create a new copy 
      g2d.setTransform(current); 

      x = getWidth() - 25 - prop.getBounds().width; 
      y = (getHeight() - prop.getBounds().height)/2; 

      at = new AffineTransform(); 
      at.translate(x, y); 
      at.rotate(Math.toRadians(-angle), prop.getBounds().width/2, prop.getBounds().height/2); 
      g2d.setTransform(at); 
      g2d.setColor(Color.BLUE); 
      g2d.draw(prop); 

      g2d.dispose(); 
     } 

    } 

    public class CrossShape extends Path2D.Double { 

     public CrossShape(int width, int height) { 

      moveTo(0, 0); 
      lineTo(width, height); 
      moveTo(width, 0); 
      lineTo(0, height); 

     } 

    } 

} 
+0

相關示例見[這裏](http://stackoverflow.com/a/3420651/230513)。 – trashgod 2014-12-03 21:58:42