I am overriding a Jpanel's paintcomponent in order to draw and rotate a few images however it has the unwanted side effect of rotating other things such as JLabels added to the JPanel etc. I have tried rotating back after drawing the image but the JLabels seem to jitter.
Please note that I am rotating each image around different points within the image (not the center of the image) so rotating the image within the image buffer is not suitable?
@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);
}
There are a few things you can do
Graphics
context, use something like Graphics2D g2d = (Graphics2D)g.create();
. Once you have finished painting, call Graphics#dispose
on the copy to free any resources it might have allocated. This basically allows you to change the properties of the copy, without effecting the original, but still paint to the same buffer. AffineTransform
from the Graphics2D
, then you can apply your own AffineTransform
and reset it when you are finished, see Graphics2D#get/setTransform
For example...
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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.