Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JOptionPane problem

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JOptionPane problem

    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
    Last edited by helloworld922; September 29th, 2011 at 11:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane problem

    Move the call to JOptionPane outside of the paintComponent method.
    The paint method should only do the drawing. What it is to draw should be determined outside of paint and then repaint called which will cause paint method to be called where the drawing can be done.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane problem

    That makes sense but how do I pass interval to paintComponent so that it knows what do draw?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane problem

    Make it a class variable that the paintComponent can see.

  5. The Following User Says Thank You to Norm For This Useful Post:

    sonicjr (September 30th, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane problem

    ok so I think I did what you said and it's still giving me the same problem... here's the code:

    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 Global
    {
    	Scanner scanner = new Scanner(System.in);
    	public static String input = JOptionPane.showInputDialog("Choose the interval t in radians");
    	public static double interval = Double.parseDouble(input);
    }
     
     
    class myPanel extends JPanel
    {
     
    	public void paintComponent(Graphics g)
    	{
    		g.drawLine(250,0,250,500); //x-axis
    		g.drawLine(0,250,500,250); //y-axis
     
    		double interval = Global.interval;
     
    		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;
    		}
    	}
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane problem

    What is the "same" problem? I don't see an error message in your post?
    When you get an error, please copy and paste the full text here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane problem

    Your code is still calling the JOptionPane from code within the paintComponent method.
    Move this line to the class level:
    double interval = Global.interval;

  9. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane problem

    "Move this line to the class level" -I don't know what you mean by this

    and the problem isn't an error message; the code compiles fine but if you look at my original post you will see the picture i posted of the output... that is the problem

  10. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane problem

    Oh wait I got it now, put double interval = Global.interval in the class myPanel before the method definition right? It's working fine now, thanks for the help

Similar Threads

  1. HTML in JOptionPane
    By LordThumper in forum AWT / Java Swing
    Replies: 1
    Last Post: April 27th, 2011, 11:50 AM
  2. JOptionPane problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 7th, 2011, 06:16 PM
  3. Convert to JOptionPane
    By ahzim8 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2011, 09:52 AM
  4. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM

Tags for this Thread