Results 1 to 3 of 3
Thread: Quick question
- 12-09-2009, 02:15 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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"); } } }
-
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 02:29 AM.
- 12-09-2009, 03:01 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
quick question
By vouslavous in forum Java AppletsReplies: 4Last Post: 04-24-2009, 08:35 PM -
Hello everyone! quick question.
By irishhokie in forum New To JavaReplies: 5Last Post: 04-03-2009, 04:13 AM -
One last quick question
By jigglywiggly in forum New To JavaReplies: 7Last Post: 01-26-2009, 08:53 AM -
Quick Question
By Graeme in forum New To JavaReplies: 4Last Post: 01-08-2009, 08:01 PM -
Quick Question
By Spenc in forum New To JavaReplies: 3Last Post: 09-22-2008, 02:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks