日期:2014-05-20 浏览次数:20833 次
@SuppressWarnings("serial")
class RotateIcon extends IconUIResource {
private int degree;
public RotateIcon(Icon icon, int degree) {
super(icon);
this.degree = degree;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D) g;
// 获取当前的坐标系状态
AffineTransform atf = g2d.getTransform();
AffineTransform clone = (AffineTransform) atf.clone();
// 以原始Icon的中心点为中心进行坐标系旋转
atf.rotate(Math.PI / 180 * degree, x + getIconWidth() / 2, y + getIconHeight() / 2);
g2d.setTransform(atf);
// 绘制
super.paintIcon(c, g, x, y);
// 重置坐标系
g2d.setTransform(clone);
}
}
------解决方案--------------------
测试代码
public class IconTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ImageIcon icon = new ImageIcon(ImageIO.read(new File("icon.jpg")));
JFrame frame = new JFrame();
BorderLayout layout = new BorderLayout();
Container container = frame.getRootPane();
container.setLayout(layout);
container.add(new JButton(new RotateIcon(icon, 0)), BorderLayout.SOUTH);
container.add(new JButton(new RotateIcon(icon, 90)), BorderLayout.WEST);
container.add(new JButton(new RotateIcon(icon, 180)), BorderLayout.NORTH);
container.add(new JButton(new RotateIcon(icon, 270)), BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
------解决方案--------------------
绘图完成后,你可以得到一个Image 吧
把这个Image生成 ImageIcon,然后再使用上面的RotateIcon包装,分别传入不同的旋转参数,设置到其它格子里
或者
自定义一个Image组件,根据坐标转换参数,执行对Image不同的绘制操作.
在九宫格的其它格子里,分别填上这个组件,并设置不同的坐标转换参数
------解决方案--------------------
如果你愿意,甚至可以给你的绘图部分加上监听,在绘制内容更改时,随时反映到其它格子里。而不是点一个按钮才可以看到