Results 1 to 20 of 20
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman

Thread: Need Help Structuring My Classes and Methods for my Java 2D Game

  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Need Help Structuring My Classes and Methods for my Java 2D Game

    I'm getting a NullPointerException error in my game, I know why, but in my RenderUnit class I don't want to merge my preRender() and postRender() methods because I want to do preRender(), do something, draw character, then postRender(). Which first one draws the game field, and the second draws the borders, as the final layer, overlapping anything that is where the borders are.

    How could I make my Strategy and Graphics, literally known in ALL of my RenderUnit class, and if one updates it, another using the same one(s) will have it updated! And not null, as the error explains.


    Java Code:
    package pack.script.game;
    
    public class Main implements Runnable {
    	public static GameData data = new GameData("Java Game", "v1.00", 20, 32);
    	public static RenderUnit render = new RenderUnit(data);
    	
    	public synchronized void start() { data.gameRunning = true; new Thread(this).start(); }
    	public synchronized void stop() { data.gameRunning = false; }
    	
    	public void run() {
    		while (data.gameRunning) {
    			render.preRender();
    			render.postRender();
    		}
    	}
    	
    	public static void main(String[] args) {
    		new Main().start();
    	}
    }
    Java Code:
    package pack.script.game;
    
    import java.awt.Dimension;
    
    public class GameData {
    	public boolean gameRunning = false;
    	
    	public static String gameName;
    	public static String gameVersion;
    	public static String gameTitle;
    	
    	public static int tilemapWidth;
    	public static int tilemapHeight;
    	public static int tilemapScale;
    	
    	public static int screenWidth;
    	public static int screenHeight;
    	public static int borderThickness;
    	
    	public static Dimension screenDimension;
    	
    	public static int[] xGrid;
    	public static int[] yGrid;
    	
    	GameData(String name, String ver, int size, int scale) {
    		gameName = name;
    		gameVersion = ver;
    		tilemapWidth = size;
    		tilemapScale = scale;
    		borderThickness = 5;
    		
    		gameTitle = gameName + " " + gameVersion;
    		tilemapHeight = tilemapWidth / 4 * 3;
    		screenWidth = tilemapWidth * tilemapScale;
    		screenHeight = tilemapHeight * tilemapScale;
    		screenDimension = new Dimension(screenWidth, screenHeight);
    		
    		xGrid = new int[tilemapWidth];
    		yGrid = new int[tilemapHeight];
    		
    		for (int i = 0; i < xGrid.length; i++) { xGrid[i] = 5 + (i * tilemapScale); }
    		for (int i = 0; i < yGrid.length; i++) { yGrid[i] = 5 + (i * tilemapScale); }
    	}
    	
    	public String getName() { return gameName; }
    	public String getVer() { return gameVersion; }
    	public String getTitle() { return gameTitle; }
    	
    	public int getWidth(boolean full) {
    		if (full == true) { return screenWidth; } else { return tilemapWidth; } }
    	public int getHeight(boolean full) {
    		if (full == true) { return screenHeight; } else { return tilemapHeight; } }
    	public int getScale() { return tilemapScale; }
    	
    	public int getBorderThickness() { return borderThickness; }
    	public Dimension getDimension() { return screenDimension; }
    	public int[] getGridX() { return xGrid; }
    	public int[] getGridY() { return yGrid; }
    }
    Java Code:
    package pack.script.game;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    
    public class RenderUnit extends Canvas {
    	private static final long serialVersionUID = 1L;
    	
    	private JFrame frame;
    	private Graphics graphics;
    	private BufferStrategy strategy;
    	private BufferedImage image;
    	private GameData data;
    	
    	public RenderUnit(GameData data) {
    		setMinimumSize(data.getDimension());
    		setMaximumSize(data.getDimension());
    		setPreferredSize(data.getDimension());
    		this.data = data;
    		
    		frame = new JFrame(data.getTitle());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new BorderLayout());
    		frame.add(this, BorderLayout.CENTER);
    		frame.setSize(data.getWidth(true), data.getHeight(true));
    		frame.pack();
    		
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		
    		image = new BufferedImage(data.getWidth(true),
    		data.getHeight(true),BufferedImage.TYPE_INT_RGB);
    	}
    	
    	public void preRender() {
    		strategy = getBufferStrategy();
    		if (strategy == null) { createBufferStrategy(3); }
    		graphics = strategy.getDrawGraphics();
    		graphics.setColor(Color.BLACK);
    		graphics.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    	}
    	
    	public void postRender() {
    		graphics.setColor(Color.BLACK);
    		for (int i = 0; i < data.getBorderThickness(); i++) {
    		rectangle(graphics, 0 + i, 0 + i, getWidth() - (i + 1), getHeight() - (i + 1), false); }
    		graphics.dispose();
    		strategy.show();
    	}
    	
    	public void rectangle(Graphics graphics, int x1, int y1, int x2, int y2, boolean fill) {
    		int x[] = {x1,x2,x2,x1,x1}; int y[] = {y1,y1,y2,y2,y1};
    		if (fill == false) { graphics.drawPolygon(x, y, 5);
    		} else { graphics.fillPolygon(x, y, 5); }
    	}
    }
    Error:

    Java Code:
    Exception in thread "Thread-2" java.lang.NullPointerException
    	at pack.script.game.RenderUnit.preRender(RenderUnit.java:45)
    	at pack.script.game.Main.run(Main.java:12)
    	at java.lang.Thread.run(Unknown Source)
    Last edited by augustas656; 04-10-2014 at 05:19 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Step one: use standard naming conventions. Variables and method start with a lower-case letter. Classes start with an upper-case letter.

    Step two: identify which variable is null on the line that throws the NPE.

    Step three: determing *why* that variable is null. When do you set its value?

    PS- btw, this is custom painting. Using Swing's built-in double buffering is almost definitely the way to go here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by KevinWorkman View Post
    Step one: use standard naming conventions. Variables and method start with a lower-case letter. Classes start with an upper-case letter.

    Step two: identify which variable is null on the line that throws the NPE.

    Step three: determing *why* that variable is null. When do you set its value?

    PS- btw, this is custom painting. Using Swing's built-in double buffering is almost definitely the way to go here.
    I'm having trouble with step 3.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Which variable is null? When do you set its value? It's going to be hard for us to read your code until you do step one.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    The null variable is strategy, and I've known from the start. And I've just edited my post to all having lower-case prime character.
    I've set the variable to a value when I initialised the object, in the constructor.
    Last edited by augustas656; 04-10-2014 at 05:31 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Where are you setting the strategy variable in the RenderUnit class? I don't see that anywhere.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    oh sorry, I meant, I make it nothing at the start, just define it in the class as private as shown, and then in the same void method as I use it. If I initialise the variable in the constructor, and update it in a void method? And i call the void method outside of the class, will the variable still be updated if I use it in another void method that I also call outside the class?

  8. #8
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    sorry, everything's good in the postRender

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Ok, I see what's happening, when I createBufferStrategy, it doesn't change strategy to the new bufferStrategy

  10. #10
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Fixed it! Can I just get advise for improving the layout of my methods and structuring of classes?

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by augustas656 View Post
    oh sorry, I meant, I make it nothing at the start, just define it in the class as private as shown, and then in the same void method as I use it. If I initialise the variable in the constructor, and update it in a void method? And i call the void method outside of the class, will the variable still be updated if I use it in another void method that I also call outside the class?
    I don't really understand what you're asking here. What happened when you tried?

    Is your strategy variable hiding a variable named strategy in the Canvas class? This line makes believe the answer to that question is yes:

    if (strategy == null) { createBufferStrategy(3); }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by augustas656 View Post
    Fixed it! Can I just get advise for improving the layout of my methods and structuring of classes?
    I've given you the best advice I can give you. You need to start smaller, as the types of questions you're asking show that you're mostly just guessing here, which isn't going to work. I know that isn't what you want to hear, but the way you're approaching this is just going to lead to headaches and an unfinished game.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Well I almost fully know everything of basic java (as in, java excluding libraries). But I do realise I am missing out on a variety of things, still it's actually very small, but it really affects how I program. Therefore, it would be a pain in the ass for me to look at every tutorial in the tutorials page you provided me to find the very few things that I am missing out (but they are affecting me in a big way). I'm not guessing everything though, I'm only guessing how I can fix an error, rather than how I can make something. If I'm stuck on how to fix an error, it's very normal and can be efficient to guess what could do the trick, and fix it! :)

    The case is really that I'm getting confused with myself.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Again, saying that you know everything of basic Java shows that you might be overestimating. I've been programming Java for over 10 years, and I still learn new "basic stuff" every day. Getting a handle on how everything fits together is crucial for doing "real" programming, and it's going to require a lot of patience. It's up to you how to proceed, but you asked for advice, and that's the best advice I can give you.
    Norm likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Let's say a group of 100 people who know java would learn the basics, well not all of the basics, because as in reality, and just like you said you will always learn something new, now they all learn basics and go to intermediate, and then perhaps advanced. Generically, avaragelly, they would have a standard for what they mean by I know all the basics of java. Although, speaking critically, again I'll say, yes you can say you always learn something new, and yes this is true and does apply to everything even if it may not seem like it, the same way as an irrelevant statement here nothing is absolute truth, and it applies to everything in reality.

    By basics, I don't mean very simple concepts, but by basics I mean the general standard information you learn from java basics tutorials, and the avarage standard of how people judge the idea of knowing all the basics of java. The tutorials I watched, I'll admit are somewhat messy, but I do know more than 80% approximately of what there is to know, those little bits that I don't know may affect me vastly. Going to the tutorials again would give me (personally) more headaches reading everything trying to find the bits that I don't know than making my game in a slow manner, during which errors occur very often and I have to post many of them on these forums.

    Creating something is less tiring for me (personally) than exploring tutorials from which all the information I'll take in, 80% I'll aprximatelly just have to waste, because I already know it.

    What I just explained, I don't mean to talk in a way telling you that your advise is bad, even though I was the one asking for advise. But as I said, my case is that I'm getting confused with myself rather than me guessing and not knowing a majority of the java basic skills and knowledge. Confusion can lead to me making mistakes, which can give the audience of reading my code the impression that I don't know java basics, and they would tell me that I need to start smaller.

    EDIT: In general, not java BASIC things, but java's BASICS

    But thank you anyway, for your efforts in trying to help me learn from my mistakes and how I could improve my java code, because, that is what I asked in the first place...
    Last edited by augustas656; 04-10-2014 at 07:35 PM.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    One of the things that makes programming fun for some and a nightmare for others: there are new basics to learn every day. I now have to sit down and learn the basics of single page web applications and how to do as much on the client and as little on the server, because that's the new basics of web development. I used to be an expert web developer and now the basics have caught up to me again and I'm running behind.

    No matter, the books arrive tomorrow I hope.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by gimbal2 View Post
    One of the things that makes programming fun for some and a nightmare for others: there are new basics to learn every day. I now have to sit down and learn the basics of single page web applications and how to do as much on the client and as little on the server, because that's the new basics of web development. I used to be an expert web developer and now the basics have caught up to me again and I'm running behind.

    No matter, the books arrive tomorrow I hope.
    Can I just ask? I have mentioned that you always learn something new and this applies not just for programming, and, are you enforcing my point or just repeating me for some reason? The only feature(s) of the concept you've mentioned that I haven't is just that it can be fun for some and a nightmare for others. Although, as I just said, I wasn't talking about that kind of basics. I will repeat myself like the 5th time, I wasn't talking about basic, as in, simple things. I mean basics, as in an avarage amount of java knowledge that IS simple and required to continue from the beginner stage. There's a difference, but it can be quite hard to tell.

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by augustas656 View Post
    I will repeat myself like the 5th time, I wasn't talking about basic, as in, simple things. I mean basics, as in an avarage amount of java knowledge that IS simple and required to continue from the beginner stage.
    And we'll repeat ourselves for the 5th time: you're working off of faulty assumptions. The only distinction between "basic" and "advanced" Java is this:

    Basic Java might be described as the syntax of for loops, if statements, functions, etc.
    "Advanced" Java is the combining of that syntax to actually write programs.

    Any other definition is arbitrary at best. Let's say you know all of "basic" Swing. What about "basic" server side Java? Java is a big beast with lots of heads, so saying you know "the basics of Java" doesn't really get you very far.

    So with the above definition, saying you know "all of basic Java" and are ready to write a custom renderer while avoiding Swing are a bit at odds with one another. The questions you've asked and the arguments you've made show that you aren't really familiar with how most of Java works, including the parts that are required for this kind of project. That's not a personal attack. That's a technical fact, and pointing it out to you is an attempt at helping you. We aren't trying to start pointless arguments, so I'm not sure why you're getting so impatient.

    You might know "all of the basics" because you've seen the syntax, but combining all of that isn't as trivial as you might think. The hard part of programming is not the syntax, but in the process of solving a problem by breaking it down into smaller pieces. It seems you still need a bit of practice with that, which is why we've said that you might want to start on something smaller before diving into a big project like this.

    We aren't saying these things to start an argument. We're saying this because you're making the same mistake that many novice programmers make in thinking that you're ready to take on a big project before you really are. That's just going to lead to headaches and unfinished projects. But it's up to you how you proceed.

    Required reading: Teach Yourself Programming in Ten Years
    Last edited by KevinWorkman; 04-17-2014 at 02:49 PM.
    kneitzel likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Just as a side note and something I've already stated in another basic VS advanced thread: when I read "basic/advanced Java", in my mind I replace that with "basic/advanced software engineering". Because I don't know what advanced Java is, much like I don't know what an advanced hammer or saw is. Its just a tool, with which you can create basic and complex ("advanced") things.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: Need Help Structuring My Classes and Methods for my Java 2D Game

    Quote Originally Posted by KevinWorkman View Post
    And we'll repeat ourselves for the 5th time: you're working off of faulty assumptions. The only distinction between "basic" and "advanced" Java is this:

    Basic Java might be described as the syntax of for loops, if statements, functions, etc.
    "Advanced" Java is the combining of that syntax to actually write programs.

    Any other definition is arbitrary at best. Let's say you know all of "basic" Swing. What about "basic" server side Java? Java is a big beast with lots of heads, so saying you know "the basics of Java" doesn't really get you very far.

    So with the above definition, saying you know "all of basic Java" and are ready to write a custom renderer while avoiding Swing are a bit at odds with one another. The questions you've asked and the arguments you've made show that you aren't really familiar with how most of Java works, including the parts that are required for this kind of project. That's not a personal attack. That's a technical fact, and pointing it out to you is an attempt at helping you. We aren't trying to start pointless arguments, so I'm not sure why you're getting so impatient.

    You might know "all of the basics" because you've seen the syntax, but combining all of that isn't as trivial as you might think. The hard part of programming is not the syntax, but in the process of solving a problem by breaking it down into smaller pieces. It seems you still need a bit of practice with that, which is why we've said that you might want to start on something smaller before diving into a big project like this.

    We aren't saying these things to start an argument. We're saying this because you're making the same mistake that many novice programmers make in thinking that you're ready to take on a big project before you really are. That's just going to lead to headaches and unfinished projects. But it's up to you how you proceed.

    Required reading: Teach Yourself Programming in Ten Years
    Okay, yes, I've looked at other tutorials, and everything is going perfectly smoothly. When I repeated myself the fifth time I already had my mistake fixed, but it's just horrible for me to know that someone misunderstood me so badly.

    Still, again, what you just said, as you said you repeated the fifth time, again, that is not what "I", "me", what I meant. No I don't mean loops, and basic things like java server side. But I'm just so thinking that you just don't get what I'm saying. Even if I said this like many many times, I still think you don't understand what I said. Java Basics meaning not ALL java basic things, Basic Things (as in things that are simple), Java Basics (an avarage set of SIMPLE java knowledge, NOT all of simple java knowledge though).

    There's a big difference in what I mean by Basics rather than Basic. It's like words with whole different meanings, barely related. I do not mean basic things, simple things. By basics I mean a set of simple java knowledge, but that doesn't mean it's all of java knowledge that is simple. Let's say you are learning to ride a skateboard, you learn to ride it and do a jump with it, okay YOU can say you have done Skateboard Basics. But then AGAIN, it's not everything that is basic. You might find thousands of flaws in how the person scoots, and maybe the very wrong and unefficient method of jumping. People all around the world who learn java and communicate with each other may tell each other that they have went past the Java Basics, another may say, but have you learnt this? And then it turns out that you haven't, and it is a simple thing. So, they can say okay, we can't say that Java Basics are all the simple things, because as you said you learn them every day, in which case what we may define Java Experts only know all the basics. It's just like you have an Exam, and you get 38 / 42 marks. That is really good, but by good I don't mean 42.

    I have gone past my problem, and the only reason I'm trying to explain this you is so in the future no other is frustrated in the same way as I am trying to drill it in. However, I might have got you SO wrong, and you understand everything I just said. In which case tell me please what you meant in the fitest way possible, the same way I tried telling you this too.

    Regards
    Augustas

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Replies: 1
    Last Post: 08-04-2011, 09:22 AM
  5. Replies: 3
    Last Post: 10-12-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •