0
 /*
<applet code =game height = 400 width =400 >
</applet> 
*/
import java.awt.* ; 
import java.awt.event.* ; 
import java.applet.* ; 

public class game extends Applet {

      public void paint(Graphics g){
           System.out.println("done");
      }
}

This is my code. I haven't used the repaint function in program but still the output is:

done
done

That is, 'done' is printed twice.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Perhaps you should have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) to better understand how painting actually works in AWT and Swing. – MadProgrammer Jan 06 '18 at 10:37
  • And then you should have a look at [Oracle reveals Java Applet API deprecation plan](https://www.theregister.co.uk/2016/08/24/oracle_reveals_java_applet_api_deprecation_plan/), [Why applets in JDK 9 are deprecated?](https://stackoverflow.com/questions/45535112/why-applets-in-jdk-9-are-deprecated), [JDK 9 and the Java Plugin](https://java.com/en/download/faq/jdk9_plugin.xml), [The clock is ticking: The Java browser plugin will be deprecated soon](https://jaxenter.com/clock-ticking-java-browser-plugin-will-deprecated-soon-131546.html) and move away from using applets – MadProgrammer Jan 06 '18 at 10:39
  • 1
    To be clear, this is not an issue or at least one you should be considered about, it's expected behaviour. Applets are dead, even if you're not using Java 1.9, most browsers now actively disable/block the applet plugin – MadProgrammer Jan 06 '18 at 10:40
  • Don't use applets, they're outdated. – Alex Cuadrón Jan 06 '18 at 10:40
  • 1
    This isn't a bug. You don't really get to control when Swing calls `paint` - it can call it as many or as few times as necessary, to make sure everything is drawn correctly. – Dawood ibn Kareem Jan 06 '18 at 10:41

1 Answers1

1

First of all, the paintComponent(Graphics g){...} function is called lots and lots of times (when you create the JPanel, when you resize it...) and you don't really know when it's being called. It's not a good practice to write code which isn't intended to draw stuff in that function (unless you're debugging that part of the code). It could cause your app to be very laggy.

Instead, try to write that piece of code in other method and call it at the end of the JPanel constructor(or introduce it directly), that way you'll know when the constructor has ended building up the JPanel. (If that's your purpose).

As a sidenote: check out this swing tutorial, it's going to help you clearly understand how swing works.

Select as answer if it'd helped you. :D

Alex Cuadrón
  • 638
  • 12
  • 19
  • 1) `JFrame` does not use the `paintComponent(..)` method. 2) The OP is using an applet in any case, however it it is not a Swing `JApplet` but an AWT based `Applet`. 3) OTOH the basic thrust of your answer is correct in that paint methods are *"called lots and lots of times .. and you don't really know when it's being called"*. Plus 1. – Andrew Thompson Jan 06 '18 at 13:22
  • 1
    @AndrewThompson thanks for the JFrame correction. I've never used JFrames directly for drawing stuff. Instead I always add a JPanel. (I thinks that the best way to paint or add components in Java) correct me if I'm wrong. – Alex Cuadrón Jan 06 '18 at 13:27
  • *"..correct me if I'm wrong."* No need for correction - you are correct. The fact that we all tend to add a `JPanel` for custom painting in Swing (and used to add a `Panel` for AWT) to the top-level window makes many unaware that the top level windows only ever have a `paint(Graphics)` method. OP: For painting in Swing, use a **`JPanel`**. For **any** component class that extends `JComponent`, override the **`paintComponent(Graphics)`** method. – Andrew Thompson Jan 06 '18 at 13:53