my dog learned polymorphism*
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
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
Author

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!
cc11rocks


“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
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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.
Thanks,
cc11rocks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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.
 
wood burning stoves
 
subject: Want confirmation that I am doing everything right