【发布时间】:2017-12-07 02:27:24
【问题描述】:
我尝试的代码是旋转图像,但我想垂直旋转图像,就像以 0 倾角旋转地球一样 360 度
我试过的代码是
public class MainClass extends JPanel {
static ImageIcon icon = null;
static RotatedIcon rotate = null;
static JLabel label = null;
public MainClass() {
try {
BufferedImage wPic = ImageIO.read(this.getClass().getResource(
"globe.png"));
icon = new ImageIcon(wPic);
rotate = new RotatedIcon(icon, 180);
label = new JLabel(rotate);
} catch (Exception e) {
System.out.println("raise exception");
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
label.repaint();
}
public static void main(String[] args) throws IOException,
InterruptedException {
MainClass mainClass = new MainClass();
JFrame frame = new JFrame();
mainClass.add(label);
frame.add(mainClass);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
ActionListener taskPerformer = new ActionListener() {
int degree = 360;
public void actionPerformed(ActionEvent evt) {
rotate.setDegrees(degree);
degree = degree + 90;
label.repaint();
mainClass.repaint();
}
};
Timer timer = new Timer(1000, taskPerformer);
// timer.setRepeats(false);
timer.start();
Thread.sleep(5000);
}
}
https://tips4java.wordpress.com/2009/04/06/rotated-icon/ 我使用的 RotatedIcon 类的参考链接。 正如解释的那样,可以旋转图像,但不是垂直的。
【问题讨论】:
-
你真的应该给出你想要实现的具体例子,因为这个类所做的旋转不是像你声称的那样围绕 x 轴,而是围绕图标的中心。所以你想要达到的目标还很不清楚。
-
我的意思是像沿 y 轴垂直旋转地球,保持中心不变
-
你的意思是像this这样的东西吗?
-
提示,您可以添加@JBNizet(或任何您想回复的人,以便他们收到通知,
@很重要) -
@Frakcool 所以唯一的解决方案是为数组列表中的每个角度获取一组图标,循环它并使用paintComponent方法中的drawImage方法一个一个地绘制图像。对吗?