0

When I run this applet it does not show anything. What is the problem in this code? Did I miss anything? Thanks for your help!

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

public class Pucca extends Applet {

public void init() {
}

public void paint(Graphics g) {

    Color yellow = new Color(255, 255, 51);
    g.setColor(yellow);
    g.fillRect(500,50,400,400);

    Color white = new Color(255,255,255);
    g.setColor(white);
    g.fillOval(600, 100, 125, 125);

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 03 '16 at 05:40
  • I can see the applet now. I just don't click the maaximize button. Thanks for your concern! Hey can you see my other question regarding how to move an applet in jcreator? Can you please help me? Thank you! – Cherry Lugtu Apr 03 '16 at 08:27
  • *"can you see my other question.."* I'll consider that once you've answered my two questions. They weren't rhetorical questions.. – Andrew Thompson Apr 03 '16 at 09:33
  • What's your two questions? – Cherry Lugtu Apr 03 '16 at 12:03
  • The two sentences ending with '?'. If you cannot figure it out, I suggest a different career than programming.. – Andrew Thompson Apr 03 '16 at 12:36

1 Answers1

1

Yes you do. In general, when you override a method in a sub-class, you call the overridden method in the base class (not always, though). So, add the following line at the beginning of your "paint" method:

super.paint(g);

This is important for the proper painting of the applet.

AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
  • Where can I add the super.paint(g);? I add it before the Color statement but nothing happens. – Cherry Lugtu Apr 01 '16 at 07:49
  • Yes, just before the yellow color line. If nothing happens, maybe you have an error in the way you add the applet to the web page or in using the applet viewer. You don't show it in your question. Anyway, the "super.paint(g)" is very important, or else the applet will not be painted correctly. – AhmadWabbi Apr 01 '16 at 07:55
  • I can see the applet now! Thanks! – Cherry Lugtu Apr 01 '16 at 08:10
  • *"`super.paint(g);` This is important for the proper painting of the applet."* It's also important for any custom painting in ***any*** AWT **or** Swing component (though with Swing components, we more often override the `paintComponent(Graphics)` method). And while that may not have been the answer to this problem, it is certainly true, and worth noting. – Andrew Thompson Apr 03 '16 at 09:36
  • 1
    @CherryLugtu If the answer solved your problem then please mark it as the correct answer. All questions on Stack Overflow are supposed to be useful to future visitors too. Please give this question a correct answer. –  Jun 05 '16 at 12:42