我有一個輸入大腦圖像的代碼,我想知道如何使用第三個軸,即z軸(遠近軸),以便我可以將圖像朝上放置(不面向用戶),以便圖像輪廓可見。如何使用z軸?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import java.net.URL;
public class Brain extends JComponent {
static int x = 200;
static int y = 200;
static int currentAngle = 0;
static double hspeed, vspeed;
static int speed = 0;
double hangle, vangle;
Image brain;
Image getImage(String filename) {
URL url = getClass().getResource(filename);
ImageIcon icon = new ImageIcon(url);
return icon.getImage();
}
public Brain() {
brain = getImage("brainz.png");
}
public void CreateBase() {
JFrame frame = new JFrame("BRAIN");
frame.setBounds(70, 30, 650, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
c.add(new Brain());
frame.addKeyListener(new adapter());
c.setBackground(Color.BLACK);
}
public void rotate() {
currentAngle += 1;
if (currentAngle >= 360) {
currentAngle = 0;
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) (origXform.clone());
newXform.rotate(Math.toRadians(currentAngle), x, y);
g2d.setTransform(newXform);
g2d.drawImage(brain, x, y, this);
g2d.setTransform(origXform);
repaint();
}
class adapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
currentAngle--;
repaint();
break;
case KeyEvent.VK_RIGHT:
currentAngle++;
repaint();
break;
case KeyEvent.VK_UP:
speed++;
hangle = Math.toRadians(currentAngle);
vangle = Math.toRadians(currentAngle);
hspeed = ((double) speed) * Math.cos(hangle);
vspeed = ((double) speed) * Math.sin(vangle);
x = x - (int) hspeed;
y = y - (int) vspeed;
repaint();
break;
case KeyEvent.VK_DOWN:
speed--;
hangle = Math.toRadians(currentAngle);
vangle = Math.toRadians(currentAngle);
hspeed = ((double) speed) * Math.cos(hangle);
vspeed = ((double) speed) * Math.sin(vangle);
x = x + (int) hspeed;
y = y + (int) vspeed;
repaint();
break;
}
}
}
public static void main(String[] args) {
Brain br = new Brain();
br.CreateBase();
//br.setDoubleBuffered(true);
}
}
感謝您的幫助,通過使用分層窗格,我可以疊加圖像一個上另一個頂部,但如果我想看到堆疊起來的100個圖像的輪廓,那麼該怎麼辦呢?即這些圖像的頂視圖.. – Riz
@Riz,也許你應該查看[Java 3D API](http:// www。 oracle.com/technetwork/java/javase/tech/index-jsp-138252.html)? – mre
我已經嘗試過使用Java 3D API,但由於硬件限制,無法在我的當前PC上運行它,我嘗試使用JOGL,但由於知識較少,我似乎無法在那裏使用z軸,我試過從這裏的程序http://wiki.tankaar.com/index.php?title=Displaying_an_Image_in_JOGL_%28Part_1%29 – Riz