0

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

Edited by Ezzaral: Changed to forum thread. This is not a code snippet.

2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by NoUserNameHere
1

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

Edited by NormR1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.