我假设您只是将 Swing/AWT 框架用于图形。如果这是错误的,请更新您的问题。
如果您使用 Swing 和 Graphics2D 类(这是 Swing 组件使用的类),那么您正在处理一个 2D 框架。这只是意味着没有内置花哨的 3D 东西 - 您必须自己实现转换(或开始抓取 3D 类来完成您的工作)。
所以,您走在正确的轨道上 - 您必须先设置剪辑(使其适合您的形状),然后执行旋转(使其以正确的角度显示)。
话虽如此,进行基本的旋转变换并不太难。 (基本)旋转有一个很好的轮廓here。当然,当您的旋转不仅仅基于一个轴时,它会变得更加复杂。但正如文章稍后解释的那样,如果将矩阵 (Rx)(Ry)(Rz) 相乘,则可以使用得到的矩阵来确定像素位置。
我创建了一个在 Y 轴上旋转的快速示例。请注意,我编造了一个愚蠢的算法(Magic Vanishingpoint Technology®)来给出一种模糊的深度错觉。我假设您已经对此有所了解 - 这可能更正确。
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Rotation3D extends JPanel{
Image img;
BufferedImage rotatedImage;
final int ROTATION_DEGREES = 70;
int vanishX = 0;
int vanishY = 0;
int vanishZ = -1000;
public Rotation3D(){
try {
//Grabbed an image from the java folder - hopefully your computer has it
img = ImageIO.read(new File(System.getProperty("java.home") + "/lib/deploy/splash.gif"));
setPreferredSize(new Dimension(img.getWidth(this) * 2,img.getHeight(this) * 2));
//Create a buffered image with the appropriate size, and draw the image on it
BufferedImage shadedImage = new BufferedImage(img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_INT_ARGB);
shadedImage.getGraphics().drawImage(img, 0, 0, this);
Raster r = shadedImage.getData();
//Not really necessary unless you're using Magic Vanishingpoint Technology®
vanishX = shadedImage.getWidth() /2;
vanishY = shadedImage.getHeight() /2;
//Create a Wraster for the transformed image
WritableRaster wr = r.createCompatibleWritableRaster();
//Do the transformation
for(int i = 0; i < shadedImage.getWidth(); i++){
for(int j = 0; j < shadedImage.getHeight(); j++){
//Remapping the pixel based on a matrix rotation
int[] result = r.getPixel(i, j, new int[4]);
Double radians = Math.toRadians(ROTATION_DEGREES);
Double newX, newY, newZ;
//newX = ((i-vanishX) * Math.cos(radians)) + vanishX; // places the rotation in the middle of the image
// x * cos(θ) + y * 0 + z * sin(θ)
newX = i * Math.cos(radians); //places the rotation in the y=0 axis
// x * 0 + y * 1 + z * 0
newY = j * 1.0;
// x * -sin(θ) + y * 0 + z * cos(θ)
newZ= i * Math.sin(radians) * -1;
//Apply Magic Vanishingpoint Technology®
//(Not actually trademarked or correct - just something thrown together)
if(newZ < vanishZ){
newX = 0.0;
newY = 0.0;
}else if(newZ < 0){
double magicVanish = newZ / vanishZ;
newX += magicVanish * newX;
newY += magicVanish * newY;
}
//Print the pixel if it fits on the screen to the new Raster
if(newX > 0 && newX < shadedImage.getWidth() && newY > 0 && newY < shadedImage.getHeight())
wr.setPixel(newX.intValue(), newY.intValue(), result);
}
}
//Create an image based on the raster.
rotatedImage = new BufferedImage(img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_INT_ARGB);
rotatedImage.setData(wr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(rotatedImage, 0, 0, this);
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Rotation3D());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}