如果你用Java在桌子上旋转一个硬币就会旋转,我怎么能旋转图像呢?
与this Gif完全一样。
我尝试使用AffineTransform
进行剪切。它并没有给我我想要的输出。
正如您从两张图片中看到的那样,剪切会使图像变大而不仅仅是旋转图像而使图像扭曲。
答案 0 :(得分:2)
警告:你不会得到你所显示的相同结果,但你可以假装它
基本上,这个例子简单地将x轴从-1缩放到1然后再缩回......
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ShearTest {
public static void main(String[] args) {
new ShearTest();
}
public ShearTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage img;
private float xScale = 1f;
private float xDelta = 0.05f;
public TestPane() {
try {
img = ImageIO.read(new File("C:\\hold\\thumbnails\\megatokyo.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
final Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xScale += xDelta;
if (xScale > 1) {
xDelta *= -1;
} else if (xScale < -1) {
xDelta *= -1;
}
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (int)((getWidth() - (img.getWidth() * xScale)) / 2);
int y = (getHeight() - img.getHeight()) / 2;
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.scale(xScale, 1);
g2d.setTransform(at);
g2d.drawImage(img, 0, 0, this);
g2d.dispose();
}
}
}
}