How to rotate an image gradually in Swing?

In addition to @tulskiy's helpful observations, I would add two points.

In addition to @tulskiy's helpful observations, I would add two points: Always construct your GUI on the event dispatch thread, as shown below. An sscce should be a Short, Self Contained, Correct (Compilable), Example. As a convenience, don't require others to recreate multiple public classes; use top-level (package-private) or nested classes.As this is a graphics problem, use a public or synthetic image that reflects your problem.

In the example below, paintComponent() alters the graphics context's transform to effect the rotation. Note that the operations are performed in the (apparent) reverse of the declaration order: First, the image's center is translated to the origin; second, the image is rotated; third, the image's center is translated to the center of the panel. You can see the effect by resizing the panel.

Addendum: See also this alternative approach using AffineTransform. Package overflow; import java.awt. *; import java.awt.event.

*; import java.awt.image. BufferedImage; import java.util. Random; import javax.swing.

*; /** * @see stackoverflow.com/questions/3371227 * @see stackoverflow.com/questions/3405799 */ public class RotateApp { private static final int N = 3; public static void main(String args) { EventQueue. InvokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame. SetLayout(new GridLayout(N, N, N, N)); frame.

SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); for (int I = 0; I Add(new RotatePanel()); } frame.pack(); frame. SetVisible(true); } }); } } class RotatePanel extends JPanel implements ActionListener { private static final int SIZE = 256; private static double DELTA_THETA = Math.

PI / 90; private final Timer timer = new Timer(25, this); private Image image = RotatableImage. GetImage(SIZE); private double dt = DELTA_THETA; private double theta; public RotatePanel() { this. SetBackground(Color.

LightGray); this. SetPreferredSize(new Dimension( image. GetWidth(this), image.

GetHeight(this))); this. AddMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { image = RotatableImage. GetImage(SIZE); dt = -dt; } }); timer.start(); } @Override public void paintComponent(Graphics g) { super.

PaintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d. Translate(this.getWidth() / 2, this.getHeight() / 2); g2d. Rotate(theta); g2d.

Translate(-image. GetWidth(null) / 2, -image. GetHeight(null) / 2); g2d.

DrawImage(image, 0, 0, null); } @Override public void actionPerformed(ActionEvent e) { theta += dt; repaint(); } } class RotatableImage { private static final Random r = new Random(); static public Image getImage(int size) { BufferedImage bi = new BufferedImage( size, size, BufferedImage. TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); g2d. SetRenderingKEY_ANTIALIASING, RenderingVALUE_ANTIALIAS_ON); g2d.

SetPaint(new Color(r.nextInt())); g2d. SetStroke(new BasicStroke(size / 8)); g2d. DrawLine(0, size / 2, size, size / 2); g2d.

DrawLine(size / 2, 0, size / 2, size); g2d.dispose(); return bi; } }.

Thanks for your post. I appreciate it. – Edy Moore Aug 6 '10 at 9:32 3 @Kap: Please click on the green check-mark (to accept) or the gray triangle (to up-vote) this or other answers you found helpful, – trashgod Aug 6 '10 at 10:41.

This.crossingP. PaintComponent(comp2D); Never do this! Your CrossingPane is not added to any component, so repaint() doesn't have any effect.

You can check it by adding prints in the paintComponent() method. SO you need to add CrossingPane to the VisualizationPane: setLayout(new BorderLayout()); add(crossingP, BorderLayout. CENTER); There are some issues with centering the image, but this shouldn't be hard to fix.PS.

Read again about layouts and painting.

The code for Rotated Icon uses the AffineTransform to rotate about its center.

1 This is also a nice example of implementing the Icon interface. – trashgod Apr 20 at 1:33 @trashgod, Oops, I posted in this thread by mistake. I meant to answer in this posting (stackoverflow.Com/questions/5722058/…).

But I see you have the answer covered with two examples. – camickr Apr 20 at 2:00 Most fortuitous! It complements my answer nicely, and I've added a link above.

The question may have been abandoned, but I'll flog the answers for a time, yet. :-) – trashgod Apr 20 at 2:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions