Sign in to follow this  

Simple Java Graphics Question

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so almost done my game in Java with my custom graphics. It works great. I basically have two classes, one for creating my Applet, Swing components & running the game. The other class just handles matrix manipulation.

I am coming to find that as I create new animations in my paint method that it is coming extremely BIG. The paint method in my Applet is the only paint method I have...so it is in charge of drawing everything so far. Before I asked, I played around to have my other class be able to handle some drawing of its own. Let me know if this is ok...it works:

public class blockem extends JApplet implements Runnable... {

...private MainBoard board = new MainBoard();

...public void paintFrame(Graphics g) {
......g = (board.test(g));
...}
}

public class MainBoard(){
...
...public Graphics test(Graphics g){
......g.setColor(Color.blue);
......g.drawString("Test.");
......return g;
...}
}

Is there a better method of having objects paint themselves? Also, how the heck do I insert code the correct way? FAQ's doesn't say how :)

Share this post


Link to post
Share on other sites
You don't need to return the Graphics object from the Test method. Since you are passing a reference to the Graphics object into the test method, any call you make on the Graphics object from inside the test method will effect the original Graphics object as well. There is no need to reassign "g" after you pass it into the method.

Share this post


Link to post
Share on other sites
What he said :) The method is operating on the same Graphics object as the first. What's passed is a reference to the same object.

Also, what you're doing is nothing more than breaking up the painting into multiple methods, which is no problem.

You might make these method static if appropriate.

As for whether or not this is a good design method, this is no different than using the Graphics methods in the first place. You're delegating to other methods - the ones offerred by java.awt.Graphics - to operate on the underlying system. It's perfectly normal to write your own methods to take care of stuff.

Generally speaking, whenever you find yourself copying and pasting, it's time to split something out into a method.

Share this post


Link to post
Share on other sites

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this