Why does this method execute? JAVA

  • Comp Sci
  • Thread starter TheMathNoob
  • Start date
  • Tags
    Java Method
In summary, the conversation discusses the execution of the paint function in a Java program and the use of two threads, a main thread and a swing thread, to make the program GUI responsive to the user while the main thread performs the desired operations.
  • #1
TheMathNoob
189
4

Homework Statement


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();
    }
  
}

Homework Equations

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:
Physics news on Phys.org
  • #2
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
 
Last edited by a moderator:

Related to Why does this method execute? JAVA

1. Why do I need to use the "public static void main" method in a Java program?

The "public static void main" method is the entry point for a Java program. It is required in order for the program to run and allows the program to start executing code.

2. How does the "public static void main" method execute?

The "public static void main" method executes by calling a series of instructions in the order they were written in the program. These instructions are known as statements and are executed one at a time.

3. Why is the "public static void main" method declared as "public" and "static"?

The "public" keyword allows the "main" method to be accessible to other classes and programs. The "static" keyword allows the "main" method to be called without having to create an instance of the class it belongs to.

4. What happens if I don't include the "public static void main" method in my Java program?

If the "public static void main" method is not included in a Java program, the program will not have an entry point and therefore will not be able to run. The compiler will display an error message indicating that the "main" method is missing.

5. Can I have multiple "public static void main" methods in a single Java program?

No, a Java program can only have one "public static void main" method. This is because the "main" method is the entry point for the program and having multiple entry points would cause confusion and errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top