我有一个用例,比如将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);
}
答案 0 :(得分:1)