Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Canvas setting background Image for games/Other Canvas<Solved>  (Read 9508 times)
0 Members and 1 Guest are viewing this topic.
Offline JayTech

Junior Member


Medals: 1



« Posted 2012-03-24 07:15:46 »

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.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-03-24 07:33:37 »

Using Canvas, you use its BufferStrategy to draw, just like this.

Offline ReBirth
« Reply #2 - Posted 2012-03-24 08:00:26 »

I always draw BG image as usual as other images. Just it has to be first to draw.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JayTech

Junior Member


Medals: 1



« Reply #3 - Posted 2012-03-24 20:44:21 »

@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?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2012-03-24 21:00:00 »

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.

Offline JayTech

Junior Member


Medals: 1



« Reply #5 - Posted 2012-03-24 21:03:50 »

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?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2012-03-24 21:06:00 »

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.

Offline JayTech

Junior Member


Medals: 1



« Reply #7 - Posted 2012-03-24 21:07:51 »

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?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #8 - Posted 2012-03-24 21:34:44 »

By not drawing something....it will not show up Grin

Offline JayTech

Junior Member


Medals: 1



« Reply #9 - Posted 2012-03-24 21:45:13 »

I mean memory wise =D, how is the garbage collection for images, once not drawn all references are gone?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jammas615

Junior Member


Projects: 1



« Reply #10 - Posted 2012-03-24 22:07:28 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
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();
      }
   }
}

Offline JayTech

Junior Member


Medals: 1



« Reply #11 - Posted 2012-03-24 22:22:30 »

@Above that's pretty much what I thought. So each lvl would be a different lvlclasses object right?
Offline jammas615

Junior Member


Projects: 1



« Reply #12 - Posted 2012-03-24 22:23:38 »

That's pretty much it.

Offline JayTech

Junior Member


Medals: 1



« Reply #13 - Posted 2012-03-24 22:39:51 »

Alright cool thanks you guys =).
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #14 - Posted 2012-03-24 23:36:09 »

I mean memory wise =D, how is the garbage collection for images, once not drawn all references are gone?
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!

Offline JayTech

Junior Member


Medals: 1



« Reply #15 - Posted 2012-03-25 00:40:52 »

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
http://www.youtube.com/watch?v=yd39sCPX-zM

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.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #16 - Posted 2012-03-25 01:09:17 »

It's looking good so far. Great job with it Smiley

Offline JayTech

Junior Member


Medals: 1



« Reply #17 - Posted 2012-03-25 01:41:58 »

Thanks =).
Offline JayTech

Junior Member


Medals: 1



« Reply #18 - Posted 2012-03-25 04:35:56 »

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?
Offline ReBirth
« Reply #19 - Posted 2012-03-25 05:28:47 »

I mean memory wise =D, how is the garbage collection for images, once not drawn all references are gone?
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.

Offline JayTech

Junior Member


Medals: 1



« Reply #20 - Posted 2012-03-25 06:03:11 »

Good point.
Offline jammas615

Junior Member


Projects: 1



« Reply #21 - Posted 2012-03-25 08:00:20 »

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?
Well, to be honest, i've never used levels and level switching like that, however i know it works well for game states. But for an average turn-based, 2D rpg i suspect that it will be more than adequate, and also from what i saw in your video. The trick i think is to do what you think best and what you want to do, then if you do run into probems, learn from them and properly fix things up (eg. redesign).

Offline JayTech

Junior Member


Medals: 1



« Reply #22 - Posted 2012-03-25 08:51:23 »

@Above thanks, gotcha =). Also another question for you you guys, I got my canvas working and everything seems fine tri buffering and  my delta time is great but it seems when I go from 60fps to 100 fps animations go a bit too fast, why is this? Why are they moving too fast and not synchronized well, what is the best way to combat this I thought delta time was supposed to do so?
Offline jammas615

Junior Member


Projects: 1



« Reply #23 - Posted 2012-03-25 08:57:36 »

Do you multiply everything by delta time each time through the update process?

Offline JayTech

Junior Member


Medals: 1



« Reply #24 - Posted 2012-03-25 19:15:24 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
 long desiredFPS =100;
   long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
   
   boolean running = true;
   
   public void run(){
     
      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;
     
      while(running){
         beginLoopTime = System.nanoTime();
         
         render();
         
         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
         
         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;
           
           if(deltaLoop > desiredDeltaLoop){
               //Do nothing. We are already late.
          }else{
               try{
                   Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
               }catch(InterruptedException e){
                   //Do nothing
              }
           }
      }
   }

1  
2  
3  
4  
5  
protected void update(int deltaTime){
      //b3.setDelta(deltaTime);
     b3.update(deltaTime);
   System.out.println(deltaTime);
   }

Then I take delta pass it through update which is then passed through set delta method to my board class  b3.update(delta time) that is then passed through for other methods dealing with time such as animations and movement. Or is that incorrect? The other methods in the board class such as playerSprite.setTime(takes in delta from the board class) and then looks over that time and checks if seconds go by or minutes to do game updates based on the overall delta time of the main games time.

Example of MonsterSprites methods dealing with delta time.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void setTick(int delta) {//Time is money..
     tick += delta;
      mainTime += delta;
      aniTime += delta;
      aniTime2+=delta;
      // System.out.println(tick);
     
      if (tick >= 500) {
         mainSpriteTick++;
          //  fx1tick++;
        //counterTick2++;
        // System.out.println(mainSpriteTick);
        tick = 0;

      }
}


When I set it to 100fps as opposed to60fps  at 100 fps things seem to move slightly faster  than they should like 3 frames tick for monster movement looks like 5-6 at 100fps+.

Isn't this correct? What am I doing wrong??? It appears that my game Logic is faster than it should or should I not even worry about 100fps as long as 60fps stays the same? I was before just using timer every 14ms to update an action preformed method to do update logic and repaint(), found this loop on the forums and it seems much better although, can someone please explain how to sync game logic properly?
Thanks
Offline jammas615

Junior Member


Projects: 1



« Reply #25 - Posted 2012-03-25 21:49:57 »

I would say that 60fps is an ample frame rate for any game. Most monitors have a refresh rate of 60Hz anyway and using more can just be overkill i think.

Offline JayTech

Junior Member


Medals: 1



« Reply #26 - Posted 2012-03-25 21:57:33 »

Is my logic correct?  Such as accumulating delta time and updating stuff w.e time I want like 500ms? And where should I be multiplying delta?
For example In my setTick Method in my monster Sprite class for my main sprite movement it updates every 500ms.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void setTick(int delta) {//
     tick += delta;
      mainTime += delta;
      aniTime += delta;
      aniTime2+=delta;
      // System.out.println(tick);
     
      if (tick >= 500) {
         mainSpriteTick++;
          //  fx1tick++;
        //counterTick2++;
        // System.out.println(mainSpriteTick);
        tick = 0;

      }
Offline jammas615

Junior Member


Projects: 1



« Reply #27 - Posted 2012-03-25 22:05:01 »

The way i've done it is multiplying movement by delta.
eg.
1  
xPos += (xVel * delta);

Offline JayTech

Junior Member


Medals: 1



« Reply #28 - Posted 2012-03-25 22:10:28 »

So for xVelocity I want my sprite to move 3pixels but with a delta time of 14ms I'd be moving at 42pixels per second?

How can I make sure I just move x pixles perSecond? This is in regards of a simple RPG. Btw I should be using delta time for keeping track of ALL time right? Or am I confused with the purpose of delta time?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #29 - Posted 2012-03-25 22:39:06 »

xVel would be how many pixels per second you want.
delta would be the number of milliseconds or nanoseconds passed divided by 1000 (for milliseconds) or 1e9 (for nanoseconds).

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (23 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!