<i id='h1hoY'><tr id='h1hoY'><dt id='h1hoY'><q id='h1hoY'><span id='h1hoY'><b id='h1hoY'><form id='h1hoY'><ins id='h1hoY'></ins><ul id='h1hoY'></ul><sub id='h1hoY'></sub></form><legend id='h1hoY'></legend><bdo id='h1hoY'><pre id='h1hoY'><center id='h1hoY'></center></pre></bdo></b><th id='h1hoY'></th></span></q></dt></tr></i><div id='h1hoY'><tfoot id='h1hoY'></tfoot><dl id='h1hoY'><fieldset id='h1hoY'></fieldset></dl></div>

        • <bdo id='h1hoY'></bdo><ul id='h1hoY'></ul>
        <tfoot id='h1hoY'></tfoot>
        <legend id='h1hoY'><style id='h1hoY'><dir id='h1hoY'><q id='h1hoY'></q></dir></style></legend>

        <small id='h1hoY'></small><noframes id='h1hoY'>

      1. 如何在不小于或大于周长的情况下绘制圆的半径

        时间:2023-05-28

        <i id='jnz0l'><tr id='jnz0l'><dt id='jnz0l'><q id='jnz0l'><span id='jnz0l'><b id='jnz0l'><form id='jnz0l'><ins id='jnz0l'></ins><ul id='jnz0l'></ul><sub id='jnz0l'></sub></form><legend id='jnz0l'></legend><bdo id='jnz0l'><pre id='jnz0l'><center id='jnz0l'></center></pre></bdo></b><th id='jnz0l'></th></span></q></dt></tr></i><div id='jnz0l'><tfoot id='jnz0l'></tfoot><dl id='jnz0l'><fieldset id='jnz0l'></fieldset></dl></div>
        <legend id='jnz0l'><style id='jnz0l'><dir id='jnz0l'><q id='jnz0l'></q></dir></style></legend>
      2. <tfoot id='jnz0l'></tfoot>

          <bdo id='jnz0l'></bdo><ul id='jnz0l'></ul>

            <small id='jnz0l'></small><noframes id='jnz0l'>

                <tbody id='jnz0l'></tbody>

                1. 本文介绍了如何在不小于或大于周长的情况下绘制圆的半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个程序,我想在其中画一条从圆心到其参数点的线.但它在圆周外和内圆周上画线.我想根据 XY 角度在圆周上精确地画线.

                  圆中心点:

                  x = 200y = 100半径= 100

                  public SecondActivity(String azim,String ele) {初始化();设置标题(我的窗口");`angle_x = Integer.parseInt(azim);angle_y = Integer.parseInt(ele);x = 200+r*Math.cos(angle_x);y = 100+r*Math.sin(angle_y);}公共无效油漆(图形g){super.paint(g);drawCircle(g,200,100,r);画线(g);}公共无效drawCircle(图形g,int x,int y,int r){g.setColor(Color.BLACK);g.drawOval(x, y, r*2,r*2);}公共无效drawLine(图形g){Graphics2D g2d = (Graphics2D) g;g2d.setColor(Color.BLUE);g2d.draw(new Line2D.Double(300.0d,200.0d,x,y));}

                  解决方案

                  您的代码(和逻辑)中有几个错误:

                  1. 您使用 2 个角度来计算 XY 坐标,因此会得到奇怪的结果.根据这个

                  2. 您使用的角度是度数,但 Math.cos(angle)Math.sin(angle) 要求 angle 以弧度表示,因此,您必须将角度转换为弧度,如 Math.roRadians(angle),如以下问题所示:

                    I am working on a program in which I want to draw a line from circle's center to its parametric point. But it draws line out of circumference and inside circumference. I want to draw line exactly on circumference according to X and Y angles.

                    Circle center points:

                    x = 200
                    y = 100
                    radius= 100
                    

                    public SecondActivity(String azim,String ele) {
                        initialize();
                        setTitle("My WIndow");`
                    
                        angle_x = Integer.parseInt(azim);
                        angle_y = Integer.parseInt(ele);
                    
                        x = 200+r*Math.cos(angle_x);
                        y = 100+r*Math.sin(angle_y);      
                    }
                    
                    public void paint(Graphics g) {
                        super.paint(g);
                    
                        drawCircle(g,200,100,r);
                        drawLine(g);
                    }
                    
                    public void drawCircle(Graphics g,int x,int y,int r) {
                        g.setColor(Color.BLACK);
                        g.drawOval(x, y, r*2,r*2);
                    }
                    public void drawLine(Graphics g) {
                        Graphics2D g2d = (Graphics2D) g;    
                        g2d.setColor(Color.BLUE);
                        g2d.draw(new Line2D.Double(300.0d,200.0d,x,y));
                    }
                    

                    解决方案

                    You have a couple of errors in your code (and logic):

                    1. You're using 2 angles to calculate the X and Y coords, thus, giving you weird results. As per this Wikipedia image the angle theta is the same for bot coords.

                    2. You're using the angle in degrees, but Math.cos(angle) and Math.sin(angle) require that the angle is given in radians, so, you must convert the angles to radians as like Math.roRadians(angle), as shown in this question: How to use Math.cos() & Math.sin()?

                    3. Not really an error but a suggestion from @MadProgrammer in this other answer of mine to use the Shape API instead of pure .drawOval as you did while drawing the line, change drawOval to draw(new Ellipse2D.Double(...)).

                    4. You're overriding paint(Graphics g) method instead of paintComponent(Graphics g) this could cause some issues while painting. Use JPanels and override their paintComponent method and add those JPanels to your JFrame.

                    Having said all of the above, I came to a good example that follows the above advises as well as solving the issue:

                    import java.awt.Color;
                    import java.awt.Dimension;
                    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.SwingUtilities;
                    
                    public class RadiusDrawer {
                        private JFrame frame;
                        private int centerX = 50;
                        private int centerY = 50;
                        private int x = 0;
                        private int y = 0;
                        private int r = 100;
                    
                        public static void main(String[] args) {
                            SwingUtilities.invokeLater(new RadiusDrawer()::createAndShowGui);
                        }
                    
                        private void createAndShowGui() {
                            frame = new JFrame(getClass().getSimpleName());
                    
                            frame.add(new MyCircle());
                            frame.pack();
                            frame.setVisible(true);
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        }
                    
                        @SuppressWarnings("serial")
                        class MyCircle extends JPanel {
                            int cx = 0;
                            int cy = 0;
                            public MyCircle() {
                                int angle = 0;
                    
                                x = (int) (r * Math.cos(Math.toRadians(angle)));
                                y = (int) (r * Math.sin(Math.toRadians(angle)));
                    
                                y *= -1; //We must inverse the Y axis since in Math Y axis starts in the bottom while in Swing it starts at the top, in order to have or angles correctly displayed, we must inverse the axis
                    
                                calculateCenter();
                            }
                    
                            private void calculateCenter() {
                                cx = centerX + r;
                                cy = centerY + r;
                            }
                    
                            @Override
                            protected void paintComponent(Graphics g) {
                                super.paintComponent(g);
                                Graphics2D g2d = (Graphics2D) g;
                    
                                drawCircle(g2d, centerX, centerY, r);
                                drawRadius(g2d);
                            }
                    
                            private void drawCircle(Graphics2D g2d, int x, int y, int r) {
                                g2d.setColor(Color.BLACK);
                                g2d.draw(new Ellipse2D.Double(x, y, r * 2, r * 2));
                            }
                    
                            private void drawRadius(Graphics2D g2d) {
                                g2d.setColor(Color.BLUE);
                                g2d.draw(new Line2D.Double(cx, cy, cx + x, cy + y));
                            }
                    
                            @Override
                            public Dimension getPreferredSize() {
                                return new Dimension(300, 300);
                            }
                        }
                    }
                    

                    Here are some screenshots of the output at 0, 60 and 90 degrees.

                    这篇关于如何在不小于或大于周长的情况下绘制圆的半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查一个点是否在指定的 Rectangle 内 下一篇:动圈与非动圈的JAVA弹性碰撞

                  相关文章

                2. <i id='BIWt1'><tr id='BIWt1'><dt id='BIWt1'><q id='BIWt1'><span id='BIWt1'><b id='BIWt1'><form id='BIWt1'><ins id='BIWt1'></ins><ul id='BIWt1'></ul><sub id='BIWt1'></sub></form><legend id='BIWt1'></legend><bdo id='BIWt1'><pre id='BIWt1'><center id='BIWt1'></center></pre></bdo></b><th id='BIWt1'></th></span></q></dt></tr></i><div id='BIWt1'><tfoot id='BIWt1'></tfoot><dl id='BIWt1'><fieldset id='BIWt1'></fieldset></dl></div>

                        <bdo id='BIWt1'></bdo><ul id='BIWt1'></ul>
                    1. <small id='BIWt1'></small><noframes id='BIWt1'>

                      <legend id='BIWt1'><style id='BIWt1'><dir id='BIWt1'><q id='BIWt1'></q></dir></style></legend>
                      <tfoot id='BIWt1'></tfoot>