旋轉非正方形的圖像在相同的邊界內將會裁剪圖像。
你可以改變Graphics
剪裁界限,但這是危險的,因爲你實際上可以提供給Graphics
背景下,這會導致一些非常奇怪的,一般不歡迎結果可視範圍之外的結束畫。
一個簡單的解決方案可能是隻從原始一個新的形象和它自己的範圍內旋轉,例如...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
try {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException iOException) {
iOException.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
private BufferedImage master;
private BufferedImage rotated;
public TestPane() throws IOException {
master = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/issue142.jpg"));
rotated = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
AffineTransform at = new AffineTransform();
at.rotate(Math.PI/2, master.getWidth()/2, master.getHeight()/2);
g2d.setTransform(at);
g2d.drawImage(master, 0, 0, this);
g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(master.getWidth() * 2, master.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - (master.getWidth() * 2))/2;
int y = (getHeight() - master.getHeight())/2;
g2d.drawImage(master, x, y, this);
g2d.drawImage(rotated, x + master.getWidth(), y, this);
g2d.dispose();
}
}
}
你的意思是「不改變大小「?旋轉圖像的物理行爲將改變其所需的空間量。儘管如此,at'scale'是什麼? – MadProgrammer 2015-02-09 21:07:21
查看圖片 – online6731 2015-02-09 21:12:42
您的第二張圖片應該是'174x232' ... – MadProgrammer 2015-02-09 21:22:38