1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Why does this method execute? JAVA

  1. Sep 16, 2015 #1
    1. The problem statement, all variables and given/known data
    Code (Java):

    package myfirstgame;
    import javax.swing.JFrame;
    import java.awt.Graphics;
    * @author danif

    public class MyFirstGame extends JFrame {

      public MyFirstGame()
          setTitle("the game");
      public void paint(Graphics g)
          g.draw3DRect(WIDTH, WIDTH, WIDTH, WIDTH, rootPaneCheckingEnabled);
        public static void main(String[] args) {
            MyFirstGame game = new MyFirstGame();
    2. Relevant equations

    3. The attempt at a solution

    It is kind of weird to me how paint executes without calling the function in the main. As soon as the object is created, paint is called. But paint is not static, so it is weird.
    Last edited by a moderator: Sep 16, 2015
  2. jcsd
  3. Sep 16, 2015 #2


    Staff: Mentor

    When this kind of java program is run, the java runtime starts two threads a main thread and a swing thread. You're seeing the swing thread in operation. The idea is that your actual work is done in the main thread while at the same time the program GUI stays responsive to the enduser.

    As an example, if you wrote a program to display a julia set fractal, then the enduser can interact with the program (like press the exit icon) while the program is computing the fractal points and periodically calling the repaint() method to have the swing thread update the screen display.

    http://www.oracle.com/technetwork/java/painting-140037.html [Broken]
    Last edited by a moderator: May 7, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted