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

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

thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.