Results 1 to 3 of 3

Thread: Quick question

  1. #1
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default Quick question

    can anybody help me figure out
    a) why only the imageicon is being drawn from the paintComponent method and not the dots?
    b)why the JButtons and JLabels and everything in the constructor wont show up?

    im guessing both problems have obvious solutions but its stumping me

    Java Code:
    public class Tour extends JPanel
    {
       private final int SIZE = 10;  // radius of each dot
       ImageIcon cityIcon = new ImageIcon("city.gif");
       ArrayList<Point> cityList = new ArrayList();
       
       public Tour()
       {
          ArrayList<Point> cityList = cityListReader();
          addMouseListener ( new ClickListener() );
          setBackground (Color.WHITE);
          setPreferredSize (new Dimension(600, 580));
          
          JButton algo1Button = new JButton("Simple Greedy Algorithm");
          algo1Button.addActionListener(new ListenerGeneral());
          JButton algo2Button = new JButton("User-Directed Greedy Algorithm");
          algo2Button.addActionListener(new ListenerGeneral());   
          JButton algo3Button = new JButton("User-Directed Algorithm");
          algo3Button.addActionListener(new ListenerGeneral());   
          JButton algo4Button = new JButton("Simple Branch-and-Bound Algorithm");
          algo4Button.addActionListener(new ListenerGeneral());   
          JButton algo5Button = new JButton("Complex Branch-and-Bound Algorithm");
          algo5Button.addActionListener(new ListenerGeneral());
          
          JLabel timerLabel = new JLabel("Time Limit: ");
          JTextField timer = new JTextField(10);
          timer.addActionListener(new ListenerGeneral());
          
    
          JPanel screen = new JPanel();
          JPanel buttons = new JPanel();
          JPanel mapScreen = new JPanel();
          
          buttons.add(timer);
          buttons.add(algo1Button);
          buttons.add(algo2Button);
          buttons.add(algo3Button);
          buttons.add(algo4Button);
          buttons.add(algo5Button);
          buttons.add(timerLabel);
          buttons.add(timer);
          
          screen.add(buttons);
          screen.add(mapScreen);
          
          repaint();
          
          
       }
       
       private ArrayList<Point> cityListReader()
       {
           
           
           Vector initial_xCoord = new Vector();
           initial_xCoord.addElement(new Double(78));
           initial_xCoord.addElement(new Double(399));
           initial_xCoord.addElement(new Double(207));
           initial_xCoord.addElement(new Double(557));
           initial_xCoord.addElement(new Double(280));
           initial_xCoord.addElement(new Double(156));
           initial_xCoord.addElement(new Double(285));
           
           Vector initial_yCoord = new Vector();
           initial_yCoord.addElement(new Double(326));
           initial_yCoord.addElement(new Double(194));
           initial_yCoord.addElement(new Double(306));
           initial_yCoord.addElement(new Double(132));
           initial_yCoord.addElement(new Double(143));
           initial_yCoord.addElement(new Double(257));
           initial_yCoord.addElement(new Double(365));
           
           
           for (int i = 0; i < initial_xCoord.size(); i++)
           {
               Point newCity = new Point();
               newCity.setLocation((double)((Double)initial_xCoord.elementAt(i)), 
                                                                        (double)((Double)initial_yCoord.elementAt(i)));
               cityList.add(newCity);
           }
           
           return cityList;
       }
           
    
       public void paintComponent (Graphics page)
       {
          super.paintComponent(page);
          
          page.setColor (Color.WHITE);   
          for (Point spot : cityList)
          {
              page.fillOval ((int)spot.getX(), (int)spot.getY(), SIZE, SIZE);
              cityIcon.paintIcon (this, page, (int)spot.getX() + 10, (int)spot.getY() + 10);
          }
       }
    
    
       private class ClickListener implements MouseListener
       {
    
          public void mousePressed (MouseEvent event)
          {
             cityList.add(event.getPoint());
             repaint();
          }
    
          public void mouseClicked (MouseEvent event) {}
          public void mouseReleased (MouseEvent event) {}
          public void mouseEntered (MouseEvent event) {}
          public void mouseExited (MouseEvent event) {}
       }
       private class ListenerGeneral implements ActionListener
            {  
                    // CHECKS IF TEXTBOX IS EMPTY
                    private boolean checkText(JTextField timer, ActionEvent f)
                    {
                            Object sourceOfEvent = f.getSource();
                            String textbox = ((JTextField) sourceOfEvent) .getText();
                            
                            return (textbox.isEmpty());
                    }
                     
                    // BUTTON LISTENERS
                    public void actionPerformed(ActionEvent e)
                    {
                            String buttonString = e.getActionCommand( );
    
                            if (buttonString.equals("Simple Greedy Algorithm"))
                                System.out.println ("Simple Greedy Algorithm");
                            if (buttonString.equals("User-Directed Greedy Algorithm"))
                                System.out.println ("User-Directed Greedy Algorithm");
                            if (buttonString.equals("User-Directed Algorithm"))
                                System.out.println ("User-Directed Algorithm");
                            if (buttonString.equals("Simple Branch-and-Bound Algorithm"))
                                System.out.println ("Simple Branch-and-Bound Algorithm");
                            if (buttonString.equals("Complex Branch-and-Bound Algorithm"))
                                System.out.println ("Complex Branch-and-Bound Algorithm");
                            
                            
                    }
            }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're adding buttons and mapScreen JPanels to another JPanel, screen, but nothing apparently gets added to the class's JPanel (this).

    e.g.,
    Java Code:
        //JPanel screen = new JPanel();
        JPanel buttons = new JPanel();
        JPanel mapScreen = new JPanel();
    
        buttons.add(timer);
        //.... etc ...
    
        //screen.add(buttons);
        //screen.add(mapScreen);
    
        //repaint();
        
        add(buttons);
        add(mapScreen);
    Last edited by Fubarable; 12-09-2009 at 03:29 AM.

  3. #3
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

Similar Threads

  1. quick question
    By vouslavous in forum Java Applets
    Replies: 4
    Last Post: 04-24-2009, 09:35 PM
  2. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 05:13 AM
  3. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 09:53 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 09:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 03:26 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
  •