斜面Java(角度三角形)

时间:2014-12-10 05:25:13

标签: java jframe jpanel paintcomponent graphics2d

我需要根据某个角度创建一个倾斜的平面,然后使用物理定律来查看该团块是否滑动。现在,我用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,我不知道如何。 有什么建议?谢谢!

1 个答案:

答案 0 :(得分:2)

警告,我的三角是没有希望的,但我相信这是(某事)你正在寻找的东西......

基本上,这假设我们知道高度和角度,然后使用tan(angle) = opposite leg/adjacent leg计算长度(相邻的腿),更简洁地说width = height * tan(angle)

Trig

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();