2014-12-03 51 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