You are on page 1of 4

Đoạn mã sau sẽ minh họa cách xoay một ảnh (xoay 90 độ).

import java.applet.Applet;
import java.net.*;

public class rotateGumby extends Applet


{
Image img = null;
Image rot = null;
int buffer[] = new int[32 * 32];
int rotate[] = new int[32 * 32];

public void init()


{
try
{
MediaTracker tracker = new MediaTracker(this);
img = getImage(new URL(getDocumentBase(),
"gumby.gif"));

tracker.addImage(img, 0);
tracker.waitForAll();
PixelGrabber grabber = new PixelGrabber(img, 0, 0,
32,

32, buffer, 0, 32);


try
{
grabber.grabPixels();
}
catch(InterruptedException e)
{
e.printStackTrace();
}

for(int y = 0; y < 32; y++)


{
for(int x = 0; x < 32; x++)
{
rotate[((32–x-1)*32) + y] = buffer[(y *
32) + x];
}
}

rot = createImage(
new MemoryImageSource(32, 32, rotate,
0, 32));
}

catch (Exception e)
{
e.printStackTrace();
}
}
public void update( Graphics g)
{
paint(g);
}

public void paint(Graphics g)


{
g.drawImage(img, 0, 0,this);
g.drawImage(rot, 0, 40, this);
}
}

Đ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.*;

public class RotatePanel extends JPanel


{
private Image image;
private double currentAngle;
public RotatePanel(Image image)
{
this.image = image;
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void rotate()


{
// Quay ảnh mỗi lần 5 độ
currentAngle += 5.0;
if (currentAngle >= 360.0)
{
currentAngle = 0;
}
repaint();
}

protected void paintComponent(Graphics g)


{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform =
(AffineTransform)
(origXform.clone());

// Tâm quay chính là tâm của panel


int xRot = this.getWidth() / 2;
int yRot = this.getHeight() / 2;
newXform.rotate(Math.toRadians(currentAngle), xRot,
yRot);
g2d.setTransform(newXform);

// Vẽ ảnh tại tâm của panel


int x = (getWidth() - image.getWidth(this)) / 2;
int y = (getHeight() - image.getHeight(this)) / 2;
g2d.drawImage(image, x, y, this);
g2d.setTransform(origXform);
}

public Dimension getPreferredSize()


{
return new Dimension ( image.getWidth(this),

image.getHeight(this));
}

public static void main(String[] args)


{
JFrame f = new JFrame();
Container cp = f.getContentPane();
cp.setLayout(new BorderLayout());
Image testImage =

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

You might also like