| | |
problem regarding buttons and graphics
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 36
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HomeworkCrish3 extends JFrame { private int x = 0, y = 0; private JButton random = new JButton("Random"), exit = new JButton("Exit"); public HomeworkCrish3() { exitButtonHandler exitbHandler; JPanel panel = new JPanel(); Container pane = new Container(); // for the buttons pane.setLayout(new GridLayout(1,2)); pane.add(random); pane.add(exit); add(panel); add(pane,BorderLayout.SOUTH); random.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { x = (int) ( Math.random() * getWidth() ); y = (int) ( Math.random() * getHeight() ); System.out.println(x+" "+y); } } ); exitbHandler = new exitButtonHandler(); exit.addActionListener(exitbHandler); setSize(500,500); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void paintComponent(Graphics g ) { g.setColor(Color.red); g.drawOval(x,y,1,1); g.drawString(x + "," + y ,(x-5),(y+5)); } private class exitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main (String [] args) { HomeworkCrish3 frame = new HomeworkCrish3(); } }
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
"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
![]() |
Similar Threads
- problem with scientific calculator! plz help ! (C++)
- monitor won't display (Monitors, Displays and Video Cards)
- Unusual and annoying graphics problem (Monitors, Displays and Video Cards)
- How can i fix this problem!!! (Java)
- need help to solve this problem (Java)
- Highjack file Ie wont download (Viruses, Spyware and other Nasties)
- Javascript making image disappear? (JavaScript / DHTML / AJAX)
- Need help with navigation buttons (HTML and CSS)
- Internet explorer terminating at will. (Viruses, Spyware and other Nasties)
- Explorer.exe terminting at will (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: Best Way to Check for same Word in string?
- Next Thread: Help on getting 2 lowest values in an array.
Views: 468 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal game givemetehcodez graphics gui helpwithhomework hql html ide image ingres input integer invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows





