gpt4 book ai didi

Java Swing PaintComponent 旋转 jlabel

转载 作者:行者123 更新时间:2023-12-02 05:11:44 30 4
gpt4 key购买 nike

我正在重写 Jpanel 的绘图组件,以便绘制和旋转一些图像,但是它会产生旋转其他东西(例如添加到 JPanel 的 JLabels 等)的不良副作用。我尝试在绘制图像后旋转回来,但 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);

}

最佳答案

您可以做一些事情

  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);

}

}

}

关于Java Swing PaintComponent 旋转 jlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281099/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com