Originally Posted by DANGEROUSSCION
Note that you should override a JPanel's or JComponent's paintComponent(...) method, not its paint(...) method.
You've not shown us this class, and so it is hard to know what it might be doing right or wrong.
TaxiCanvas() which creates the amount of taxis to draw using a for loop.
Creates how? Using what layout manager?
The problem, I think is with the following TaxiFrame().
Are you sure about this? Have you used a debugger or println statements to isolate your error?
I'm trying to get it use the inputNum from the JTextField to repaint() the Taxi(). But I enter an int and press enter and it displays correctly, but if I hit enter again it doubles the already defined number without redrawing, creating 2 instances of the Taxi(), if I clear the JTextField and use a new int, it adds even more instances of Taxi().
How can I clear the user input and then enter a new int, so it redraws the Taxi().
Also, what should I put in the catch (NumberFormatException nfe) section.
I usually display a JOptionPane informing the user that the number entered was incorrect. I also usually only use the number inside of the try block, *after* the number has been attempted to be parsed. This will prevent me from trying to use the number if inaccurate data has been presented.
--- Update ---
Recommendations on first glance of your newly posted code:
- Override the TaxiCanvas's paintComponent(Graphics g) method, not its paint(Graphics g) method.
- The first line of this override should be a call to the super's method: super.paintComponent(g). This will help remove any previously drawn images.
- Don't create your Taxi objects in the paint or paintComponent method but rather in the class's constructor.
- I prefer extending JPanel in this situation to that of JComponent since JPanel will repaint its background for you.