用上面的图片实现一个程序,实现3个小图片逆时针旋转,大图片做背景顺时针旋转。
看起来挺简单,其实并不容易,有兴趣的朋友可以试试。
------解决方案--------------------
抛砖引玉一下,只有最重要的一些代码,重点是用好AffineTransform这个类:
- Java code
public class Test extends JLabel { private BufferedImage[] images = new BufferedImage[16]; private int index; public Test(BufferedImage original) { for (int i = 0; i < images.length; i++) { double theta = -2 * Math.PI * i / images.length; BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.drawRenderedImage(original, AffineTransform .getRotateInstance(theta, original.getWidth() / 2, original.getHeight() / 2)); images[i] = img; } } public void rotate() { index = (index + 1) % images.length; setIcon(new ImageIcon(images[index])); } public static void main(String[] args) throws Exception { final Test test = new Test(ImageIO.read(Test.class .getResourceAsStream("1335971717_7428.png"))); JFrame jf = new JFrame("Test"); jf.add(test); jf.setSize(100, 100); jf.setVisible(true); new Thread() { public void run() { try { for (;;) { test.rotate(); sleep(100); } } catch (InterruptedException ex) { ex.printStackTrace(); } } }.start(); }}