Results 1 to 4 of 4
  1. #1
    habester is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Duplicate Input Boxes

    Hi everyone,

    I'm using JOptionPane.showInputDialog for a program that gets a number, than draws out that many random ellipses. Everything works fine except that for some reason, there are two input boxes that pop up every time. I can't figure out why. Any ideas?

    Java Code:
    import javax.swing.*;
    import java.awt.geom.*;
    import java.util.Scanner;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    public class P6_16JH extends JApplet {
    	public void paint(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		String response = JOptionPane.showInputDialog(null, "Enter number of circles to draw:");
    		int n = Integer.parseInt(response);
    		for(int i = 0; i<n; i++) {
    			double w = Math.random()*getWidth();
    			double h = Math.random()*getHeight();
    			double x = Math.random()*(getWidth()-w);
    			double y = Math.random()*(getHeight()-h);
    			Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, w, h);
    			g2.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Do not do any kind of real "work" in the paint method of a Component. Don't override paint, at all. Override paintComponent. And, even there, do not do any real "work" there. Ask for the input outside of the method and set an instance variable with the result (look into AtomicInteger just to make sure that the value is consistent) then call repaint() on the component.

  3. #3
    habester is offline Member
    Join Date
    Nov 2009
    Rep Power


    Thank you! That got rid of that problem.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    Custom painting is done by extending paintComponent() of a JPanel or JComponent. The component is then added to the content pane of the applet.

    Never override paint() of a top level container (JApplet, JFrame, JDialog).

Similar Threads

  1. Dragging Boxes
    By anilanar in forum New To Java
    Replies: 8
    Last Post: 09-05-2009, 09:23 PM
  2. [SOLVED] Combo Boxes Automation
    By dbashby in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 01:39 AM
  3. Retrieve values of Text boxes using LIST
    By Kayal in forum Web Frameworks
    Replies: 2
    Last Post: 03-20-2009, 12:00 PM
  4. Built-in User Popup message boxes ?
    By BobZ in forum AWT / Swing
    Replies: 2
    Last Post: 02-06-2009, 07:23 PM
  5. Help! Need to randomly assign boxes
    By newb101 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts