问题
I have a code, which imports a brain image, I wanted to know that can how can I use the third axis, i-e z axis, (far and near axis) so that I can position the image facing upwards,(not facing user) so that outline of image is visible.
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);
}
}
回答1:
See:
- How to Use Layered Panes
回答2:
As @mre said, the 3D API does what you need.
But to answer closer to your original code, your upwards-facing image would look something like this, right?
. ------
. / \
. / \
. / \
. ------------
Well that is a trapezium which can be calculated using a perspective transform. It doesn't fall into the class of AffineTransforms that you have used. The matrix looks something like this:
r = [ 1 -x/c ] . r0
[ 0 1 ]
Java advanced imaging API has the transform that you need: javax.media.jai.PerspectiveTransform
http://download.oracle.com/docs/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PerspectiveTransform.html
来源:https://stackoverflow.com/questions/7012134/how-to-use-z-axis