1. Not finding help here? Sign up for a free 30min 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!

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");
          setSize(250,250);
          setResizable(false);
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
       
      }
      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

    jedishrfu

    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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Why does this method execute? JAVA
  1. Java object method (Replies: 23)

  2. Java Method and Subclass (Replies: 11)

Loading...