So for Canvas is there a way to Paintcomponent like in JPanel? What is the best way to set a background Image? All help is appreciated.
I always draw BG image as usual as other images. Just it has to be first to draw.
@Ra4king yea I understand that part, but for a background Image in canvas how do I set a static background Image like paint component does in Jpanel or do I just draw it first? I don’t want to have to keep re painting it right?
2nd question, to clear a canvas you do clear rect right or something else?
ReBirth has already answered that question: just draw the image first. Don’t worry about performance since it is negligible for a single image.
And yes you use clearRect or fillRect to clear anything. The Graphics2D operations are the same for everything. However, it would be unnecessary to clear the screen if you are already drawing a background image.
Thanks man you’re always there when I need ya and rebirth too! Hey another question, what should I do for canvas when I want to load another lvl, like clean up a screen just clear rect? Or what is the proper way to clean up to prepair for another zone?
There is nothing to “clean up”. When you draw, it stays on the screen only for that one frame until you redraw. So essentially you are redrawing everything 60 times a second. When loading another level, you are probably going to be using the same or different background image, which will clear the screen anew for different stuff to draw.
So my game is an RPG and there will be maybe you will be at an Inn or maybe a dungeon. So you’re saying by just loading those classes and each Npc entity etc once I just not draw them they will be gone visually an In memory?
By not drawing something…it will not show up ;D
I mean memory wise =D, how is the garbage collection for images, once not drawn all references are gone?
If you wanted to implement levels you could use objects for each level and then have an object for “currentLevel”. Your game could just control the drawing from the current level and then when you change level, just change the currentLevel to a new one and initialize it. Its kind of like having game states (eg. in-game, menu, etc.) but each state is a different level.
Heres a very simplistic idea of what i mean:
public class Game {
private Level lvlOne;
private Level lvlTwo;
private Level currLvl;
//Change your level here and initialize it.
public void changeLvl(int lvl) {
switch (lvl) {
case 1: currLvl = lvlOne;
currLvl.init();
break;
case 2: currLvl = LvlTwo;
currLvl.init();
break;
}
}
//Update the current level.
public void update() {
currLvl.update();
}
//Render the current level.
public void render() {
currLvl.render();
}
//The main loop just keeps looping through and updating then rendering the current level
public void mainLoop() {
while (running) {
update();
render();
}
}
}
@Above that’s pretty much what I thought. So each lvl would be a different lvlclasses object right?
That’s pretty much it.
Alright cool thanks you guys =).
You are confusing drawing with objects. When you draw anything on the screen, the pixels are copied from the image to the screen. The Image object still exists whether you draw it or not. It’s not until you remove all references to that Image, by setting the reference to null, when the garbage collector retrieves the memory.
And you shouldn’t be worrying about performance and memory this early. Just write a game already!
Hm, yea that is true. I guess I was confusing the two XD. But I just wanted to make sure I was cleaning up the graphics properly in Canvas, as where Jpanel you could do clear etc.
Here’s a preview of my games engine so far
Been finishing the graphics API am almost done, all graphics are place holders till I get a new artist…Need to incorperate the battle engine in soon.
Game preview is in JPanel atm switching to canvas as we speek.
Let me know whatcha think =D.
It’s looking good so far. Great job with it
Thanks =).
Btw, jammas615 about your message about lvl states. I mean for an average turn base 2d rpg like maybe Fireemblem etc would you consider those lvls? I mean what is the usual standard to switch between lvls/zones? Are lvl/states an efficent way of doing so?
Think about this. You want to have field of grass. You load one smallest unit (tile) of grass and draw it repeatly. It shows that an image object can be used multiple times. Screen has its own amount of memory use. Even a black window uses memory. What you should consider is object that is created by you.