gpt4 book ai didi

java JLabel 在 90 度旋转后无法正确重新绘制调整大小的标签

转载 作者:行者123 更新时间:2023-12-01 13:49:15 26 4
gpt4 key购买 nike

我有一个用例,例如将 JLabelComponent 添加到托盘后,我必须将其大小调整到自定义级别(调整 bcz 数据的大小非常大)添加的标签组件。一旦我完成了重新调整大小设置组件。setBounds all该坐标。当我尝试将调整大小的标签组件旋转到 90 度时,我没有获得正确的形状。它的头被砍掉了。请建议

这是我的代码:

if (selectedComponent instanceof LabelComponent) {
LabelComponent lbls = (LabelComponent) selectedComponent;
lbls.setAngle(Integer.parseInt(value));
lbls.repaint();
lbls.setSize(lbls.getPreferredSize());

我的绘画方法是

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
AffineTransform aT = g2.getTransform();
double sin = Math.abs(Math.sin(getAngle()));
double cos = Math.abs(Math.cos(getAngle()));
int originalWidth = getWidth();
int originalHeight = getHeight();
int newWidth = (int) Math.floor(originalWidth * cos + riginalHeight * sin);
int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);

if(getAngle() == Integer.parseInt("90"))
{
g2.translate((newWidth-originalWidth)/2, (newHeight-orginalHeight)/2);
}
g2.rotate(Math.toRadians(getAngle()), originalWidth/2, originalHeight/2);
super.paint(g);
}

最佳答案

when i try to rotate the re sized label component to 90 degrees i am not getting proper shape.

一个问题是您需要重置标签的首选尺寸。即由于旋转而导致的宽度和高度的变化。

您可以尝试使用Rotated Icon,而不是进行自定义绘画。 .

关于java JLabel 在 90 度旋转后无法正确重新绘制调整大小的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20088429/

26 4 0