Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Thread-related methods - public or private?  (Read 310 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS

Junior Member


Medals: 1



« Posted 2013-07-01 19:23:11 »

These questions have been driving me mental so here goes. For clarification, this program works perfectly, I'd just like to know if I can write it in a different, possibly better way.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
public class Game extends Canvas implements Runnable {
   
   private static final long serialVersionUID = 1L;
   private static final int width = 400;
   private static final int height = 400;
   private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
   private boolean running = false;
   
   public static void main (String[] args) {
     
      Game game = new Game();
      game.setPreferredSize(new Dimension(width,height));
      game.setFocusable(true);
      game.addKeyListener(new Input());
     
      JFrame frame = new JFrame("Toast");
      frame.add(game);
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
     
      game.start();
     
   }
   
   
   public void start () {
      running = true;
      new Thread(this).start();
   }
   
   public void stop () {
      running = false;
   }
   
   public void run () {
     
      double before = System.currentTimeMillis();
      long now;
      double unprocessed = 0;
     
      while (running) {
         
         now = System.currentTimeMillis();
         unprocessed += (now-before)*60/1000;
         before = now;
         
         while (unprocessed >= 0) {
            update();
            unprocessed --;
         }
         
         render();
         
      }
     
   }
   
   public void update () {
      Player.update();
   }
   
   public void render () {
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(3);
         return;
      }
      Graphics g = bs.getDrawGraphics();
      g.drawImage(image,0,0,getWidth(),getHeight(),null);
      g.setColor(Color.WHITE);
      g.fillRect(Math.round(Player.x),Math.round(Player.y),Player.w,Player.h);
      g.dispose();
      bs.show();
   }
   
}

The code has been shortened to make it more read-able.

Question 1: I can remove the method stop with no noticeable change. Is the method ever called and if not - why bother?

Question 2: Changing the method start to private doesn't affect my program. However, I receive an error message when doing the same to method run. Why does run have to be public?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Input implements KeyListener {
   
   static boolean left = false;
   static boolean right = false;
   static boolean up = false;
   static boolean down = false;
   
   public void keyPressed (KeyEvent e) {
      int keyCode = e.getKeyCode();
      if (keyCode == KeyEvent.VK_LEFT) {
         left = true;
      }
      if (keyCode == KeyEvent.VK_RIGHT) {
         right = true;
      }
      if (keyCode == KeyEvent.VK_UP) {
         up = true;
      }
      if (keyCode == KeyEvent.VK_DOWN) {
         down = true;
      }
   }
   
   public void keyReleased (KeyEvent e) {
      int keyCode = e.getKeyCode();
      if (keyCode == KeyEvent.VK_LEFT) {
         left = false;
      }
      if (keyCode == KeyEvent.VK_RIGHT) {
         right = false;
      }
      if (keyCode == KeyEvent.VK_UP) {
         up = false;
      }
      if (keyCode == KeyEvent.VK_DOWN) {
         down = false;
      }
   }
   
   public void keyTyped(KeyEvent e) {
     
   }
   
}

This is less important and less about this specific class but if you're feeling generous.
Question 3: The static variables in my input-handling class are used, but never changed, in other classes. This brings to my total noob question. Can't public variables be accessed by all classes? Why does a static type do the job for me?

I'd gladly appreciate some help!
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-07-01 20:08:08 »

It's very clear that you didn't write this code, or you simply copied it from another source.

1. In the code given, stop is never called.

2. start() is only used by the class that it's defined in, so it can be private with no consequence.
    run() needs to be accessed by the thread class, therefore it needs public access.

3. Public classes can be accessed by any class

"public static final" is a way of creating global constants that any class can access. Though, because they are private, making them static doesn't do anything at all. At the same time, they are only used in this class, so that makes no difference.

Edit: Reading your 3rd question again, you mention your input handling class, which isn't given, so my above answer probably doesn't apply.
Offline VaPS

Junior Member


Medals: 1



« Reply #2 - Posted 2013-07-01 20:48:30 »

It's very clear that you didn't write this code, or you simply copied it from another source.
I've watched and read five or so tutorials and they have written in this exact way. I'm sure there are other ways but from my limited experience I've seen this countless times.

Quote
3. Public classes can be accessed by any class

"public static final" is a way of creating global constants that any class can access. Though, because they are private, making them static doesn't do anything at all. At the same time, they are only used in this class, so that makes no difference.

Edit: Reading your 3rd question again, you mention your input handling class, which isn't given, so my above answer probably doesn't apply.
So there is no such thing as public variables? I still don't understand which type to use. I added Input.java in my original post for reference.

Thanks for the help
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troncoso

JGO Coder


Medals: 20



« Reply #3 - Posted 2013-07-01 20:55:51 »

Sorry, that was a typo. Yes, public fields are possible and can be accessed from any class.

1  
2  
static boolean left = false;
static boolean right = false;


Because these static variables have no access modifier (public, private, protected) they use the default access, which is package. So, any class in the same package can access these values. The only thing static does is allow them to be accessed without creating an object of the class, for example:

1  
boolean isLeft = Input.left;


And for reference, the 4 access modifiers for fields and methods:

public - Any class can access them
private - only the class it is contained in can access it
protected - only subclasses can access it
package - Any class within the same package can access it
Offline VaPS

Junior Member


Medals: 1



« Reply #4 - Posted 2013-07-01 20:57:47 »

That cleared up things for me, thanks a lot!
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #5 - Posted 2013-07-01 22:25:11 »

1  
2  
3  
4  
5  
6  
7  
public class SomeClass implements Runnable {

   @Override
   public void run() {
   }

}

run() can't be anything else than public, because you inherit it from Runnable. In the above code, you see @Override, this indicates that it is inherited and you override it.
If you change public to private or protected, then save your project in Eclipse, it will show you an error and it will tell you why you can't do that.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (20 views)
2014-04-15 18:08:23

BurntPizza (16 views)
2014-04-15 03:46:01

UprightPath (29 views)
2014-04-14 17:39:50

UprightPath (14 views)
2014-04-14 17:35:47

Porlus (30 views)
2014-04-14 15:48:38

tom_mai78101 (55 views)
2014-04-10 04:04:31

BurntPizza (112 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!