1

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

[here i just want to draw a line for circle center to its circumference.

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));
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89

1 Answers1

2

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.

    enter image description here

  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.

enter image description here enter image description here enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Thank you for helping me ..But I don't get Why did you declare these variables. double lineX = 0; double lineY = 0; double roundedX = 0; double roundedY = 0; – Ashu Ansari Feb 01 '18 at 18:33
  • Those were some tests that I was doing, you can safely remove them I forgot to do so... (I already did the edit in the program above as well @AshuAnsari) – Frakcool Feb 01 '18 at 18:46
  • You're welcome. I'm glad to help ^^ keep it up C: BTW I found another error in your program, let me mention it into my answer (See point #4) – Frakcool Feb 01 '18 at 18:57
  • tell me please . – Ashu Ansari Feb 02 '18 at 14:12
  • @AshuAnsari to tell you what? I already added point #4 to my post which is the other error I saw... – Frakcool Feb 02 '18 at 14:13
  • can you help to add JPanel to JFrame so that I could override paintComponent() method – Ashu Ansari Feb 02 '18 at 14:27
  • Because I have override paintComponets() method but now it is not drawing circle and lines. – Ashu Ansari Feb 02 '18 at 14:31
  • Frakcool please help me . An Error is occurring means When I run the Program . it does not show line at correct angle . but when I run again then it draws line at correct angle. please help me What should I do to resolve it. – Ashu Ansari Feb 02 '18 at 14:43
  • Again it's `paintComponent` not `paintComponentS`. Since this question was about drawing the radius and it is already solved I invite you to ask a new question with a proper [mcve] so we can help you. I'm in my way to the office, if I see your question when I arrive I'll try to help – Frakcool Feb 02 '18 at 14:45