绘制一条线 - 最大点[英] Drawing a Line - Maximum Point

问题描述

我画了一条基于滑块的角度.

我试图使线路的结尾y坐标为一定数量(例如300),即使它是角度的.

关于如何执行此操作的任何想法?这是到目前为止我系列上的工作:

double angle = intAngle;
angle = angle * Math.PI / 180;
double length = 300;

graphics.setColor(Color.RED);

double startX = 300;
double startY = 100;
double endX = startX + length * Math.cos(angle);
double endY = startY + length * Math.sin(angle);
double end2X;
double end2Y;   
double dblAngle;
double angle2;
int intAngle2;
double start2X = endX;
double start2Y = endY;
intAngle2 = 180 - intAngle;
angle2 = intAngle2;
angle2 = (angle2 * Math.PI / 180);
end2X = (start2X - length * Math.cos(angle2));
end2Y = (start2Y - length * Math.sin(angle2));

int intEndX = (int)endX;
int intEndY = (int)endY;

if(blnButton == true){

  graphics.draw(new Line2D.Double(startX, startY, endX, endY));    

  graphics.draw(new Line2D.Double(start2X, start2Y, end2X, end2Y));       

}

推荐答案

可能有一种更简单的方法,但基本上,您可以根据角度和角度的角度(角度-360)

在圆上计算两个点

,半径为150的圆圈,这将为您提供300行,例如

 example

红线是从圆的中心到指向给定天使表示的圆的线.蓝色是逆的.每行都是150个像素线,含义在一起,它们的长度为300像素.

这个示例分别绘制,但实际上,它们可以被绘制为单行

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.Ellipse2D;
import java.awt.geom.Line2D;
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();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            DrawPane drawPane = new DrawPane();
            add(drawPane);

            JSlider slider = new JSlider(0, 100);
            add(slider, BorderLayout.SOUTH);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    drawPane.setAngleInDegrees(360d * (slider.getValue() / 100d));
                }
            });
            slider.setValue(0);
        }

    }

    public class DrawPane extends JPanel {

        private double angle;

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

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

            // Radius of the circle
            double r = 150;
            // Degrees to radians...
            double radians = Math.toRadians(angle);

            // The end point on the circle...
            int endX = (int) Math.round(r * Math.cos(radians));
            int endY = (int) Math.round(r * Math.sin(radians));

                        // The start point on the circle, 360 degress from the
            // start angle
            radians = Math.toRadians(angle - 360);
            int startX = (int) Math.round(r * Math.cos(radians));
            int startY = (int) Math.round(r * Math.sin(radians));

            // Offset for the ellipse (center of the screen)
            double x = (getWidth() / 2d) - r;
            double y = (getWidth() / 2d) - r;

            g2d.setColor(Color.LIGHT_GRAY);
            g2d.draw(new Ellipse2D.Double(x, y, r * 2, r * 2));
            // Center of the circle...
            x = (getWidth() / 2d);
            y = (getWidth() / 2d);

            // One single line
            //g2d.setColor(Color.BLACK);
            //g2d.draw(new Line2D.Double(x - startX, y - startY, x + endX, y + endY));

            g2d.setColor(Color.RED);
            g2d.draw(new Line2D.Double(x, y, x - startX, y - startY));
            g2d.setColor(Color.BLUE);
            g2d.draw(new Line2D.Double(x, y, x + endX, y + endY));

            g2d.dispose();
        }

        public void setAngleInDegrees(double value) {
            if (angle != value) {
                angle = Math.min(Math.max(value, 0), 360);
                repaint();
            }
        }

    }

}

或类似的东西...

本文地址:https://www.itbaoku.cn/post/978669.html