Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

java - Drawing a Line - Maximum Point

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

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

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

Example

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...