Meaningless Drivel is fun!
The moose likes Other JSE/JEE APIs and the fly likes Want confirmation that I am doing everything right Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Want confirmation that I am doing everything right" Watch "Want confirmation that I am doing everything right" New topic

Want confirmation that I am doing everything right

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I am making a open-world 2D game. There is a character (first person) who can walk around. When you move forward, items get bigger (like real life). When you move backward, items get smaller (like real life). It has the appearance of 3D because the object get bigger or smaller according to where you are and it is first person. I would like comments and suggestions on two things:
Ideas for the game itself - things you can do, buildings, characters, background, etc
My code, which is posted below. I am just starting and positioning everything takes copious amounts of time, which is the reason I only have one object moving.
NOTE: The thing sticking out to the left on the bottom is the arm, hand, and 2 fingers. I will add the other 8 fingers, the other hand, and the other arm later, when I have time. This is just a test/template for the game. I also have ideas for the background, as I am just setting up everything as I go.

Thank you very much!

“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

Three things I noticed right away:
1) your MouseListener methods do nothing. Why do you extend MouseListener if you do nothing with it?
2) likewise for the ActionListener method.
3) you create some Point objects in your paintComponent method only to use their X and Y values. You can skip these objects and put their X and Y values directly in the int[]s.

How To Ask Questions How To Answer Questions
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Thank you very much. I am not doing anything with the MouseListener and the ActionListener because I am just starting to make this. I will add support for these when I have time and get in more depth with the game itself. I will take your suggestion with the points though.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19751

john price wrote:Thank you very much. I am not doing anything with the MouseListener and the ActionListener because I am just starting to make this.

Oh, good. Because you asked if you were doing everything right, and writing one giant class which implements everything in sight doesn't qualify. But fortunately you still have time to change your existing errors.

(Why is it not right to have your JPanel implement ActionListener? Because when you do that, you say "This JPanel object IS AN ActionListener". But the purpose of JPanel is to display things on the screen, so the "Single Responsibility" rule says that it shouldn't be doing anything other than displaying things on the screen. Including that it shouldn't be responding to button-clicks and the like.)

Unfortunately most of the tutorials on the web still use this antiquated technique, probably because they were all written in the last century when people were less careful about object-oriented design. But nowadays it's considered better to use separate classes to implement all of those listeners. Typically anonymous inner classes are used, but there are cases where top-level classes can be used.

But now that I look at your code, you are using a ButtonListener class already to listen to JButton presses. So does your panel implement ActionListener for some other reason? Get rid of that and create another class for that other reason, then.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4094

I will repeat what I've told you in other threads:
- Separate your game logic from your graphical interface. Use a simple class to keep track of where all the stuff is. The only thing the JPanel should do is draw the stuff, nothing more.
- Don't let your panel implement any listeners. Make separate listener classes, or use anonymous classes.
- Instead of MouseListener, you can use MouseAdapter. Instead of KeyListener, you can use KeyAdapter.
- Make your fields private. Really. Do this. I mean it.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Want confirmation that I am doing everything right
It's not a secret anymore!