嘿,我实现了旋转和翻转图像的代码。
当我翻转图像然后旋转时,它会消失。
BUT
当我翻转和翻转图像时(所以它就像翻转前一样)我可以正常旋转。
我试着理解错误,我认为问题在于变换或缩放。
您是否知道如何修复此代码?
/**
* Paint the icons of this compound icon at the specified location
*
* @param c
* The component on which the icon is painted
* @param g
* the graphics context
* @param x
* the X coordinate of the icon's top-left corner
* @param y
* the Y coordinate of the icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
AffineTransform af = g2.getTransform();
int cWidth = icon.getIconWidth() / 2;
int cHeight = icon.getIconHeight() / 2;
int xAdjustment = (icon.getIconWidth() % 2) == 0 ? 0 : -1;
int yAdjustment = (icon.getIconHeight() % 2) == 0 ? 0 : -1;
if (rotate == Rotate.DOWN) {
g2.translate(x + cHeight, y + cWidth);
g2.rotate(Math.toRadians(90));
icon.paintIcon(c, g2, -cWidth, yAdjustment - cHeight);
} else if (rotate == Rotate.UP) {
g2.translate(x + cHeight, y + cWidth);
g2.rotate(Math.toRadians(-90));
icon.paintIcon(c, g2, xAdjustment - cWidth, -cHeight);
} else if (rotate == Rotate.UPSIDE_DOWN) {
g2.translate(x + cWidth, y + cHeight);
g2.rotate(Math.toRadians(180));
icon.paintIcon(c, g2, xAdjustment - cWidth, yAdjustment - cHeight);
} else if (rotate == Rotate.VERTICAL) {
g2.translate(0, getIconHeight());
g2.scale(1, -1);
icon.paintIcon(c, g2, x, y);
vert = !vert; //boolean flag
} else if (rotate == Rotate.HORIZONTAL) {
g2.translate(getIconWidth(), 0);
g2.scale(-1, 1);
icon.paintIcon(c, g2, x, y);
hor = !hor; //boolean flag
} else if (rotate == Rotate.VERTICALLY_HORIZONTAL) {
g2.translate(getIconWidth(), getIconHeight());
g2.scale(-1, -1);
icon.paintIcon(c, g2, x, y);
hor = !hor;
vert = !vert;
} else if (rotate == Rotate.CENTER) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform original = g2.getTransform();
AffineTransform at = new AffineTransform();
at.concatenate(original);
at.translate((getIconWidth() - icon.getIconWidth()) / 2,
(getIconHeight() - icon.getIconHeight()) / 2);
at.rotate(Math.toRadians(angle), x + cWidth, y + cHeight);
g2.setTransform(at);
icon.paintIcon(c, g2, x, y);
g2.setTransform(original);
}
}
答案 0 :(得分:1)
如果你想让他们一起工作,你必须保存你用变换所做的图像。这是一个例子
Image orig = ...;
Image transformedCopy = ...;
AffineTransformation at = ...;
transformedCopy.getGraphics().setTransform(at).drawImage(orig);
//transformedCopy will now have a copy of the transformed image