Hello. I'm new to Java and these forums and wondered if anybody could assist with this. The aim of my code is to get the user to input a number of taxis they would like to draw and this number is drawn. That works, but if the user then enters a new number it draws that amount too but is supposed to remove the old amount. For example, user enters 2 and 2 taxis are drawn - if they then enter 4 it should remove the existing 2 and draw 4 but at present it just draws 4 in addition to the existing 2. Please see code below:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TaxiJFrame extends JFrame implements ActionListener { private TaxiCanvas tc; JTextField numTaxiField = new JTextField(8); JPanel jPanel = new JPanel(); JLabel enterText = new JLabel(); JLabel numTaxis = new JLabel(); public TaxiJFrame(){ super("Taxi Express"); setSize(800, 600); setLocation(100, 100); getContentPane().setBackground(Color.CYAN); setDefaultCloseOperation(EXIT_ON_CLOSE); jPanel.setBackground(Color.YELLOW); jPanel.add(numTaxis); numTaxis.setText("Number of taxis"); jPanel.add(numTaxiField); numTaxiField.addActionListener(this); enterText.setText("Type a number and press [Enter]"); jPanel.add(enterText); getContentPane().add("South", jPanel); setVisible(true); } public void actionPerformed(ActionEvent pressEnter){ try { int inputNum = Integer.parseInt(numTaxiField.getText()); getContentPane().add("Center", new TaxiCanvas(inputNum)); setVisible(true); } // end try catch (NumberFormatException nfe){ int inputNum = Integer.parseInt(numTaxiField.getText()); getContentPane().remove(tc); getContentPane().add("Center", new TaxiCanvas(inputNum)); setVisible(true); } } public static void main(String[] args) { new TaxiJFrame(); } }import java.awt.Graphics; import javax.swing.JComponent; import java.util.ArrayList; public class TaxiCanvas extends JComponent{ private ArrayList<Taxi> taxis = new ArrayList<Taxi>(); public TaxiCanvas(int numTaxi){ for (int i=1; i<=numTaxi; i++){ taxis.add(new Taxi(i)); } } public void paint(Graphics g){ final int TAXI_WIDTH = 80; for (Taxi b: taxis){ int x = randomInt(5, getWidth()/100*TAXI_WIDTH); int y = randomInt(5, getHeight()/100*TAXI_WIDTH); b.paint(g, x, y); } } private int randomInt(int min, int max){ return (int)(Math.random() * (max - min + 1) + min); } }import java.awt.Color; import java.awt.Graphics; public class Taxi { private int num; public Taxi(int num){ this.num = num; } public void paint(Graphics g, int x, int y){ final int TAXI_WIDTH = 80; final int UNIT = TAXI_WIDTH/12; g.setColor(Color.YELLOW); g.fillRect(x + 3*UNIT, y, 5*UNIT ,3*UNIT); //Top of the body g.setColor(Color.YELLOW); g.fillRect(x, y + 3*UNIT, 12*UNIT, 4*UNIT); //Main part of the body g.setColor(Color.WHITE); g.fillRect(x + 4*UNIT, y + 1*UNIT , 3*UNIT, 2* UNIT); //Window g.setColor(Color.BLACK); //Colour of wheels g.fillOval(x + 1*UNIT, y + 5*UNIT, 3*UNIT, 3*UNIT); //Left wheel g.fillOval(x + 8*UNIT, y + 5*UNIT, 3*UNIT, 3*UNIT); //Right wheel g.drawString(""+ num, x + 5*UNIT, y + 6* UNIT); //Position of number, offset to be placed inside taxi } }