Results 1 to 4 of 4
Thread: Duplicate Input Boxes
- 11-09-2009, 07:25 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 5
- Rep Power
- 0
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))); g2.draw(ellipse); g2.fill(ellipse); } } }
- 11-09-2009, 07:52 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-09-2009, 06:11 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 5
- Rep Power
- 0
Thank you! That got rid of that problem.
- 11-09-2009, 06:13 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
Dragging Boxes
By anilanar in forum New To JavaReplies: 8Last Post: 09-05-2009, 09:23 PM -
[SOLVED] Combo Boxes Automation
By dbashby in forum New To JavaReplies: 3Last Post: 03-27-2009, 12:39 AM -
Retrieve values of Text boxes using LIST
By Kayal in forum Web FrameworksReplies: 2Last Post: 03-20-2009, 11:00 AM -
Built-in User Popup message boxes ?
By BobZ in forum AWT / SwingReplies: 2Last Post: 02-06-2009, 06:23 PM -
Help! Need to randomly assign boxes
By newb101 in forum New To JavaReplies: 1Last Post: 09-16-2008, 10:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks