如何使用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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅:
See:
正如 @mre 所说,3D API 可以满足您的需要。
但为了更接近你的原始代码,你的朝上的图像看起来像这样,对吧?
这是一个梯形,可以使用透视变换来计算。它不属于您使用过的 AffineTransforms 类。矩阵看起来像这样:
Java 高级成像 API 具有您需要的转换:
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
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:
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