We just started with GUI Programming in Java using AWT only. My task is to draw an ellipse and display it together with a label. Somehow I can't figure out how to display them at the same time. As soon as I add
add(label);
to my program it only displays the label. Thats my code so far...
import java.awt.*;
public class Ellipse extends Frame{
public static void main (String args[]) {
new Ellipse("Ellipse");
}
public void paint(Graphics g){
Graphics shape = g.create();
shape.setColor(Color.black);
shape.fillRect(100,80,100,40);
shape.setColor(Color.red);
shape.fillOval(100,80,100,40);
}
Ellipse(String s){
super(s);
setLocation(40,40);
setSize(300,300);
setBackground(Color.white);
Font serif = new Font("Serif", 1, 10);
setFont(serif);
Label label = new Label("Ellipse 1",1);
add(label);
setVisible(true);
}
}
The actual task is to draw an ellipse, fill the background with black and put a label below. Besides my problem, is there a possibility to fill the background of the oval with color other than drawing a seperate rectangle first?