斜面Java(带角度的三角形) [英] Inclined plane Java (Triangle with angle)

查看:256
本文介绍了斜面Java(带角度的三角形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据一定角度创建一个倾斜的平面,上面有一个方块,然后使用物理定律查看该方块是否滑动.现在,我用Path2D.Double创建了我的块,但是我找不到改变块角度的方法.从应用程序框架中的微调器中选择角度.我创建了2个类,一个类是Frame,另一个是Panel,在其中绘制我的块.这是我的路径代码:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);


    Path2D.Double bloc = new Path2D.Double();

    bloc.moveTo(10,0);

    bloc.lineTo(getWidth(), getHeight());
    bloc.lineTo(10, getHeight());
    bloc.closePath();


    g2d.setColor(Color.black);
    g2d.fill(bloc);

}

我知道我只需要做一遍

 g2d.rotate(Math.toRadians(theSpinner Value));

但是对于Path2D,我不知道如何. 有什么建议吗?谢谢!

解决方案

警告词,我的尝试无可救药,但我相信这与您正在寻找的东西类似.

基本上,这假设我们知道了高度和角度,然后使用tan(angle) = opposite leg/adjacent leg从中计算出长度(相邻的腿),用通俗易懂的话说width = height * tan(angle)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final TestPane tp = new TestPane();
                final JSlider slider = new JSlider(0, 90, 0);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        tp.setAngle(slider.getValue());
                    }
                });
                slider.setValue(45);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int angle;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int width = getWidth();
            int height = getHeight();

            Path2D triangle = new Path2D.Double();
            triangle.moveTo(10, 10);
            triangle.lineTo(10, height - 20);

            double b = angle * Math.tan(Math.toRadians(angle));
            triangle.lineTo(10 + b, height - 20);
            triangle.closePath();

            System.out.println("....");
            System.out.println(angle);
            System.out.println(b);

            g2d.setColor(Color.BLACK);
            g2d.draw(triangle);

            g2d.dispose();
        }

        protected void setAngle(int value) {
            angle = value;
            repaint();
        }

    }

}

现在,如果要计算高度(相反),则需要使用height = width / tan(angle)……但是您必须修改绘图代码;)

更改高度而不是宽度

        double b = width / Math.tan(Math.toRadians(angle));

        Path2D triangle = new Path2D.Double();
        triangle.moveTo(10, height - 20);
        triangle.lineTo(10, height - 20 - b);

        triangle.lineTo(width - 20, height - 20);
        triangle.closePath();

i need to create an inclined plane according to a certain angle with a bloc on it and then use the physics laws to see if the bloc slides or not. For now, i created my bloc with a Path2D.Double, but i can't find a way to change the angle of the bloc. The angle is chosen from a spinner in the frame in the application.I created 2 class, one is the Frame, and the other is the Panel, where i draw my bloc. Here is the code of my path:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);


    Path2D.Double bloc = new Path2D.Double();

    bloc.moveTo(10,0);

    bloc.lineTo(getWidth(), getHeight());
    bloc.lineTo(10, getHeight());
    bloc.closePath();


    g2d.setColor(Color.black);
    g2d.fill(bloc);

}

I know that for a line, i just have to do

 g2d.rotate(Math.toRadians(theSpinner Value));

But for a Path2D, i don'T know how. Any advice? Thank you !

解决方案

Word of warning, my trig is hopeless, but I believe this is (something) along the lines of what you are looking for...

Basically, this assumes we know the height and the angle, from which it then calculates the length (adjacent leg) using tan(angle) = opposite leg/adjacent leg, in plainer speak width = height * tan(angle)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final TestPane tp = new TestPane();
                final JSlider slider = new JSlider(0, 90, 0);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        tp.setAngle(slider.getValue());
                    }
                });
                slider.setValue(45);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int angle;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int width = getWidth();
            int height = getHeight();

            Path2D triangle = new Path2D.Double();
            triangle.moveTo(10, 10);
            triangle.lineTo(10, height - 20);

            double b = angle * Math.tan(Math.toRadians(angle));
            triangle.lineTo(10 + b, height - 20);
            triangle.closePath();

            System.out.println("....");
            System.out.println(angle);
            System.out.println(b);

            g2d.setColor(Color.BLACK);
            g2d.draw(triangle);

            g2d.dispose();
        }

        protected void setAngle(int value) {
            angle = value;
            repaint();
        }

    }

}

Now, if you want to calculate the height (opposite), you'd need to use height = width / tan(angle) instead...but you'd have to modify the drawing code ;)

To change the height instead of the width

        double b = width / Math.tan(Math.toRadians(angle));

        Path2D triangle = new Path2D.Double();
        triangle.moveTo(10, height - 20);
        triangle.lineTo(10, height - 20 - b);

        triangle.lineTo(width - 20, height - 20);
        triangle.closePath();

这篇关于斜面Java(带角度的三角形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆