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

查看:190
本文介绍了绘制线 - 最大点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绘制了一条基于滑块的角度线。

I drew a line that is at an angle based on a slider.

我试图让线的Y坐标成一定数量(比方说300) ,即使它是一个角度。

I am trying to make the line's end Y coordinate a certain number (let's say 300), even if it is at an angle.

关于如何做到这一点的任何想法?这是我到目前为止的工作:

Any ideas on how to do this? Here is the work on my line so far:

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)

There's probably a simpler way, but basically, you can calculate two points on a circle based on the angle and the inverse of the angle (angle - 360)

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

With a circle with a radius of 150, this will give you a line of 300, for example

红线是从圆心到指定圆圈的直线天使。蓝色是相反的。每行是150像素的行,意思是一起,它们的长度是300像素。

The red line is the line from the center of the circle to point on the circle represented by the given angel. The blue is the inverse. Each line is 150 pixels line, meaning together, they are 300 pixels in length.

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

This examples draws the separately, but realistically, they could be draw as a single line

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

    }

}

或类似的东西......

or something along those lines...

这篇关于绘制线 - 最大点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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