Why does this method execute? JAVA

  • Context: Comp Sci 
  • Thread starter Thread starter TheMathNoob
  • Start date Start date
  • Tags Tags
    Java Method
Click For Summary
SUMMARY

The discussion centers on the execution of the paint method in a Java Swing application, specifically in the MyFirstGame class. When an instance of MyFirstGame is created, the Java runtime initiates both a main thread and a Swing thread, allowing the paint method to execute automatically without an explicit call from the main method. This design enables the GUI to remain responsive while performing background computations, such as rendering complex graphics like a Julia set fractal.

PREREQUISITES
  • Understanding of Java Swing framework
  • Familiarity with multithreading concepts in Java
  • Knowledge of the Java Graphics class
  • Basic experience with Java programming
NEXT STEPS
  • Study Java Swing threading model and event dispatch thread (EDT)
  • Learn about the repaint() method and its role in GUI updates
  • Explore advanced painting techniques in Java, including double buffering
  • Investigate performance optimization for Java Swing applications
USEFUL FOR

Java developers, particularly those working with GUI applications, game developers, and anyone interested in understanding Java's painting mechanism and multithreading behavior.

TheMathNoob
Messages
189
Reaction score
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
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K