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.:)
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();
}
}