I'm creating a program for a programming course which creates an approximation of a circle using line segments. The user inputs the parametric interval and the program will draw the circle using the given interval. the point is to see which intervals create a good approximation. The user enters the interval using the JOptionPane input box and then the program creates a frame and a panel on which to draw, using JFrame and JPanel. However, once the user inputs the interval and clicks ok, for some reason a portion of the box imprints itself on the JFrame which is showing the circle. I think this may have something to do with the fact that I set the frame as visible before the interval is entered, but I do not know how to do it any other way. here is my code and a picture of the problem:
import java.util.*; import java.lang.*; import javax.swing.*; import java.awt.*; public class curveApprox { public static void main(String[] args) { JFrame frame = new JFrame("Circle Approximator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myPanel panel = new myPanel(); frame.add(panel); frame.setSize(500,500); frame.setVisible(true); } } class myPanel extends JPanel { public void paintComponent(Graphics g) { Scanner scanner = new Scanner(System.in); String input = JOptionPane.showInputDialog("Choose the interval t in radians"); double interval = Double.parseDouble(input); g.drawLine(250,0,250,500); //x-axis g.drawLine(0,250,500,250); //y-axis for (double t = 0;t<=2*Math.PI;) { double x0 = t; double y0 = t; double x1 = t+interval; double y1 = t+interval; g.drawLine((int)(200*Math.cos(x0)+(250)),(int)(200*Math.sin(y0)+250),(int)(200*Math.cos(x1)+250),(int)(200*Math.sin(y1)+250)); t += interval; } } }
problem.jpg