problem regarding buttons and graphics

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 36
Reputation: esy928 is an unknown quantity at this point 
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

problem regarding buttons and graphics

 
0
  #1
Jul 30th, 2008
Good Day!

I'm working on my homework for the past 5 hrs now, i really need some help. I need to create a program that will generate a random point on the Panel. the problem is i cant seem to make the point appear on my panel. any help is greatly appreciated.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class HomeworkCrish3 extends JFrame {
  5.  
  6. private int x = 0,
  7. y = 0;
  8.  
  9. private JButton random = new JButton("Random"),
  10. exit = new JButton("Exit");
  11.  
  12. public HomeworkCrish3()
  13. {
  14. exitButtonHandler exitbHandler;
  15.  
  16.  
  17. JPanel panel = new JPanel();
  18. Container pane = new Container(); // for the buttons
  19.  
  20. pane.setLayout(new GridLayout(1,2));
  21. pane.add(random);
  22. pane.add(exit);
  23.  
  24. add(panel);
  25. add(pane,BorderLayout.SOUTH);
  26.  
  27. random.addActionListener(
  28. new ActionListener()
  29. {
  30. public void actionPerformed(ActionEvent e)
  31. {
  32. x = (int) ( Math.random() * getWidth() );
  33. y = (int) ( Math.random() * getHeight() );
  34. System.out.println(x+" "+y);
  35. }
  36. }
  37. );
  38.  
  39. exitbHandler = new exitButtonHandler();
  40. exit.addActionListener(exitbHandler);
  41.  
  42. setSize(500,500);
  43. setVisible(true);
  44. setDefaultCloseOperation(EXIT_ON_CLOSE);
  45. }
  46.  
  47. public void paintComponent(Graphics g )
  48. {
  49. g.setColor(Color.red);
  50. g.drawOval(x,y,1,1);
  51. g.drawString(x + "," + y ,(x-5),(y+5));
  52. }
  53.  
  54.  
  55. private class exitButtonHandler implements ActionListener
  56. {
  57. public void actionPerformed(ActionEvent e)
  58. {
  59. System.exit(0);
  60. }
  61. }
  62.  
  63.  
  64. public static void main (String [] args)
  65. {
  66. HomeworkCrish3 frame = new HomeworkCrish3();
  67.  
  68. }
  69.  
  70. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: problem regarding buttons and graphics

 
0
  #2
Jul 30th, 2008
for some reason your paintCompant() isn't working for your JFrame, remove Component form the method declaration, and you need to call repaint() from you actionPerformed() method
Last edited by sciwizeh; Jul 30th, 2008 at 12:03 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 36
Reputation: esy928 is an unknown quantity at this point 
Solved Threads: 0
esy928 esy928 is offline Offline
Light Poster

Re: problem regarding buttons and graphics

 
0
  #3
Jul 31st, 2008
thanks!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 468 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC