Đoạn mã sau sẽ minh họa cách xoay một ảnh (xoay 90 độ)
Đoạn mã sau sẽ minh họa cách xoay một ảnh (xoay 90 độ)
Đoạn mã sau sẽ minh họa cách xoay một ảnh (xoay 90 độ)
import java.applet.Applet;
import java.net.*;
tracker.addImage(img, 0);
tracker.waitForAll();
PixelGrabber grabber = new PixelGrabber(img, 0, 0,
32,
rot = createImage(
new MemoryImageSource(32, 32, rotate,
0, 32));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void update( Graphics g)
{
paint(g);
}
Đoạn mã tiếp theo sẽ xoay một ảnh liên tục, mỗi lúc xoay ảnh một góc 5 độ. Chúng ta sử
dụng gói java2D cho công việc này:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
image.getHeight(this));
}
Toolkit.getDefaultToolkit().getImage("c:/temp/gumby.gif");
final RotatePanel rotatePanel = new
RotatePanel(testImage);
JButton b = new JButton ("Rotate");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
rotatePanel.rotate();
}
});
cp.add(rotatePanel, BorderLayout.CENTER);
cp.add(b, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
Gói Java2D hỗ trợ rất nhiều đối tượng, phương thức cho việc thao tác trên ảnh đồ họa. Như ở
ví dụ trên, để xoay một ảnh, đơn giản chỉ dùng đối tượng Graphics2D. Đối tượng
AffineTransForm định nghĩa phép biến đổi mà cụ thể là phép xoay ảnh