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 5 of 5

Thread: Help with removing objects

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with removing objects

    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
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help with removing objects

    it should remove the existing 2 and draw 4 but at present it just draws 4
    Where is there logic in the code to make that decision? I don't see any if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with removing objects

    With my limited knowledge using try/catch and: getContentPane.remove(tc);

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    619
    My Mood
    Sleepy
    Thanks
    10
    Thanked 94 Times in 87 Posts

    Default Re: Help with removing objects

    Firts of all you should override paintComponent() when doing custom painting in Swing. A call to super.paintComponent() will repaint the JComponent, i.e. removing old taxis.

    Read this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help with removing objects

    Have you tried calling the remove() method?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 11:53 AM
  2. I need help with removing arrays or hiding them.
    By seaofFire in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2012, 08:09 PM
  3. [SOLVED] I'm having problems with removing arrays
    By seaofFire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2012, 12:18 PM
  4. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM
  5. Removing objects from a hashSet
    By JohnTor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 03:03 PM