Hi, I just started learning about java applets recently and was wondering why the shapes won't appear on screen when I press a button.

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

public class Buttons extends Applet implements ActionListener {
    public void init() {
        button1 = new Button("Button 1");
        add(button1);
        button1.addActionListener(this);

        button2 = new Button("Button 2");
        add(button2);
        button2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1){
            button1.setForeground(Color.RED);
            drwLine = true;
            }

        else if(e.getSource() == button2){
            button2.setForeground(Color.BLUE);
            drwOval = true;
            }
    }

    public void paint(Graphics g){
    if(drwLine){
        g.drawLine(50,50, 70,80);
    }

    if(drwOval){
        g.drawOval(90,90, 30,30);
    }

    }

    Button button1, button2;
    boolean drwLine = false;
    boolean drwOval = false;
}

Is the paint() method called? Add a println to see.
What color is the shape that is drawn? Try calling setColor()

Also try calling repaint()

Thank you. Adding repaint() worked.

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.