Results 1 to 7 of 7
  1. #1
    Jakers is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Repaint on a JFRAME

    Hey, im pretty new to programming, I want my frame to repaint the taxis each time the user enters a number, as it stands it is overlapping, I have used repaint but no such luck, if anyone could help I would be very appriciative thanks, here is the code
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class TaxiFrame extends JFrame implements ActionListener {
    
        private JTextField calc;
        private String additionString = "taxinum";
        TextField text = new TextField(20);
      //  TaxiCanvas jt;
    
        public TaxiFrame() {
    
    
            setSize(600, 400);
            setLocation(100, 100);
            getContentPane().setBackground(Color.CYAN);
            //  getContentPane().add("Center", new TaxiCanvas(9));
            setVisible(true);
    
    
            JPanel sp = new JPanel();
            sp.add(new JLabel("Number of Taxis: ", JLabel.LEFT));
    
            calc = new JTextField(5);
            calc.addActionListener(this);
            sp.add(calc);
            getContentPane().add("South", sp);
            sp.add(new JLabel("(type an integer and press enter)", JLabel.RIGHT));
        }
    
        public static void main(String args[]) {
            new TaxiFrame();
        }
    
        public void actionPerformed(ActionEvent e) {
            try {
    
              if (e.getSource() == calc) {
                    int taxinum = Integer.parseInt(calc.getText());
                   // jt = new TaxiCanvas(calc);
                    getContentPane().add("Center", new TaxiCanvas(taxinum));
                 getContentPane().
    
    
                   setVisible(true);
    
                } else {}
    
    
    
            } catch (NumberFormatException nfe) {
                add(new JLabel("?", JLabel.RIGHT));
            } finally {
                if (e.getSource() == calc) {
                    calc.requestFocus();
    
    
                } else {
                    calc.requestFocus();
                    calc.selectAll();
                }
            }
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Repaint on a JFRAME

    What exactly do you mean when you say "it is overlapping"? And are you adding components to a container that's already visible? If so, you have to revalidate it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Jakers is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Repaint on a JFRAME

    If a user enter 2, 2 taxis will show, if the user then enters 4, 6 taxis will show on screen, etc

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Repaint on a JFRAME

    Well, if you want help, we'll have to see an SSCCE (that's not your whole program) that demonstrates exactly what's going on in the code- for example, we have no idea what your TaxiCanvas class is doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Jakers is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Repaint on a JFRAME

    Here is the taxi canvas class, the taxi class is just to build the taxi, im pretty sure this doesnt need to be changed
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class TaxiCanvas extends JComponent {
    
        private Taxi[] taxis;
    
        public TaxiCanvas(int numTaxis) {
            super();
            makeTaxis(numTaxis);
    
        }
    
        private void makeTaxis(int numTaxis) {
            taxis = new Taxi[numTaxis];
    
            for (int i = 0; i < taxis.length; i++) {
                taxis[i] = new Taxi(i + 1);
            System.out.println(i);
            }
        }
    
        public void paint(Graphics g) {
            for (int i = 0; i < taxis.length; i++) {
                int x = randomInt(0, getWidth() - 150);
                int y = randomInt(0, getHeight() - 100);
                taxis[i].display(g, x, y);
            }
            g.drawString(taxis.length + " taxis", getWidth() / 2 - 30, 40);
        }
    
        private int randomInt(int min, int max) {
            return (int) (Math.random() * (max - min + 1) + min);
        }
    }

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Repaint on a JFRAME

    as it stands it is overlapping, I have used repaint
    1) Custom painting should be done in the paintComponent() method, not the paint() method.

    2) The first statement should be super.paintComponent(...) so that the background gets refreshed before you redo the painting.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Repaint on a JFRAME

    Moving to AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  2. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  3. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  4. Repaint the entire JFrame (GroupLayout)
    By Willi in forum AWT / Swing
    Replies: 13
    Last Post: 12-19-2009, 10:11 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •