#How does this work

11 messages · Page 1 of 1 (latest)

grim coyote
#

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Shoot_better extends Canvas implements Runnable{
private boolean running = false ;
private Thread thread ;
private synchronized void start() { // starts the game loop
if(running) {
return ; }
running = true ;
thread = new Thread(this) ;
thread.start();
}
private synchronized void stop() {
if(!running)
return;
running = false ;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
public void run() {
while(running) {
System.out.println("RUNNING") ; //GAME LOOP
}
}

public static void main(String[] args) {
  JFrame frame = new JFrame() ; 
  Shoot_better obj = new Shoot_better() ; 
  obj.setPreferredSize(new Dimension(200,200));
  obj.setMaximumSize(new Dimension(200,200));
  obj.setMinimumSize(new Dimension(200,200));
  
  frame.setResizable(false);
  frame.pack();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  frame.add(obj)  ;
  obj.start(); 
}

}

stray creekBOT
#

This post has been reserved for your question.

Hey @grim coyote! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

grim coyote
#

learning this from a guy on youtube

#

So basically when the code comes to the last line i.e obj.start() ;

#

it calls funtion start

#

function start then starts a thraed

#

how tf does this thread call the run() methhod

hard narwhal
grim coyote
#

yooo thanks that explains it

stray creekBOT
# grim coyote yooo thanks that explains it

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.