如何使用z轴?

发布于 2024-11-29 00:50:59 字数 3160 浏览 0 评论 0原文

我有一个代码,它导入大脑图像,我想知道如何使用第三轴,即 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-12-06 00:50:59

正如 @mre 所说,3D API 可以满足您的需要。

但为了更接近你的原始代码,你的朝上的图像看起来像这样,对吧?

.             ------
.            /      \
.           /        \
.          /          \
.          ------------

这是一个梯形,可以使用透视变换来计算。它不属于您使用过的 AffineTransforms 类。矩阵看起来像这样:

r  =   [ 1   -x/c ] . r0
       [ 0     1  ] 

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文