my program works fine but i dont know why when i debug the program the shapes are coming out without me pressing the buttons

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithButtons extends Applet implements ActionListener
{
	Button button1, button2;
	int r, g , b;
	Color color;
	boolean drawLine = false;

	public void init()
	{
		String parmStringRED =  getParameter("red");
		 r = Integer.parseInt(parmStringRED);
		String parmStringGREEN =  getParameter("green");
		 g = Integer.parseInt(parmStringGREEN);
		String parmStringBLUE =  getParameter("blue");
		 b = Integer.parseInt(parmStringBLUE);
	 	color = new Color (r,g,b);
		button1 = new Button("Button 1");
		add(button1);
		button1.addActionListener(this);

		button2 = new Button("Button 2");
		add(button2);
		button2.addActionListener(this);
		button1.setForeground(color);
		button2.setForeground(color);

		button1.setBackground(Color.yellow);
		button2.setBackground(Color.yellow);

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button1)
			drawLine = true;
		else
	        drawLine = false;
	        repaint();
	}
	public void paint(Graphics g)
	{
		super.paint(g);   // for the background
		if(drawLine)
			g.drawLine(0, 0, 100, 100);
		else
			g.fillOval(20, 10, 15, 30);
	}
}

(You can just call on colour: new Colour(255,0,255). I think it is easier using RGB then what you are trying now.)

You initialize drawLine as false. Every time the Applet redraws, and the drawLine variable is still false, the oval will paint. Everytime you do something with the applet, repaint() is called from within, not only when you call repaint() yourself.

and change

Applet

to

JApplet

and

Button

to

JButton


and

public void paint(Graphics g)

to

public void paintComponent(Graphics g)

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.