Images loaded , make the game/program really really slow

Hello all.My exams are over and I can once again continue learning programming :smiley:

Well I got back to my messing around code , after having done the 2D asteroids tutorial form cokeandcode and from a spanish site , and now I am trying to make a simple ( the more simple the better) platform game.So messing around wth code I discovered that when I add an image … really simple image the game goes really slow … I mean with that image not loaded the player’s movement is ok and smooth but when I paint that image … it goes really slow and the fps drop A LOT.
Maybe the way I do it is wrong ?

public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.blue);
        g.fillRect(0,0,getWidth(),getHeight());
        
        ground = spriteCache.getSprite("ground.jpg");
        
        player.paint(g);
        //g.drawImage(ground , 0 , 690 , this);  <---- with that one ..... the game goes slow as hell .. why is that ? :S
        
         paintfps(g);
         
        strategy.show();
    }

I am 100% sure that the answer is really really simple … but I am a real noob :frowning:

How are you loading the image within ā€œspriteCache.getSprite(String)ā€ ?

Like that :

private  BufferedImage loadImage(String filename)
        {
           url = this.getClass().getClassLoader().getResource(filename);
            try
            {
                
                return ImageIO.read(url);
            }
             catch(Exception e)
             {
                 e.printStackTrace();
                 System.exit(1);
                 return null;
             }
        }
    
    
    public BufferedImage getSprite(String name)
    {
        BufferedImage img = (BufferedImage)sprites.get(name);
        if(img == null)
        {
            img = loadImage("images/"+name);
            sprites.put(name , img);
        }
        return img;
    }

does the image get loaded at all?

your not initiating x streams? where x=your fps.

what kind of picture is it? transparent? how big is it?.. if paintWorld() is renderer I don’t see the problem.
Be carefull to make picture right and to show it right… once I drew picture wrong, trying to paint it beyond screen or it was corrupter (width and height in viewer didn’t match what java got with loaded image) or something, it painted in the middle and everything slowed down with picture flickering.

it is just a big brown rectangle … maube it does go beyond the screen’s limits … maybe it’s that ?

Hmmmm it had a width of 1024 … , so did my application … I lowered its width to 1020 , removed the comments … and it worked.It no longer slows down the whole thing.But I would ike to understand the reason , so if anyone can explain it to me I would be grateful

Lol , another edit/question !

I wanted to find a nice sky picture … I found one for testing only , but it was small.So somewhere I found this :

But now it starts to go slow as hell again … why ?

public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        sky = spriteCache.getSprite("background.jpg");
        g.setPaint(new TexturePaint(sky,new Rectangle(0,0, sky.getWidth(), sky.getHeight())));
        g.fillRect(0,0,getWidth(),getHeight());
        ground = spriteCache.getSprite("ground.png");
        
        player.paint(g);
        g.drawImage(ground , 0 , 690 , this);  
        
         paintfps(g);
         
        strategy.show();
    }

Are you using 1.5?

If not, that way of loading an image will not result in the image being elligable for hardware acceleration.

Yes I am using jdk1.5.0

yeah I would also like to know why pics acts like that when you drow them off screen (why that part isn’t simply discarded). About texture thing… don’t have a clue, worked only with pics :-\

Edit: Again, don’t just give up when it dosen’t work and post here… try to solve it yourself. First time it was drown beyond screen… maybe it’s this time also? Try first drowing on half of screen… try, you’ll never learn if others do it for you.

Yes ofcourse you are right , but I would like to use every means available to me to ask questions and from the answers learn.Of course I am still messing around with code and experimenting while expecting a reply here :slight_smile:

I know that replying to myself is not nice , but I would like someone to at least plz answer the question me and kova presented.So I bump this thread so the gurus in here help plz :-[

My guess is that you have more then one bit of alpah in the image.

That will defeat the hardware rendering unless you set some special flags on windows…

what is an alpah? Or even if u made a spellinmg mistake alpha?

Yup its a spellinmg mistake.

Alpha is transparency as I remember it. It works but it’s tricky with Java, book Killer Game Programming has good text about images and how to accelerate them.
Why do you even use TexturePaint? Why not just use picture with drawImage()? Here’s what JavaDoc has to say about TexturePaint:

[quote=ā€œJavaDocā€]The TexturePaint class provides a way to fill a Shape with a texture that is specified as a BufferedImage. The size of the BufferedImage object should be small because the BufferedImage data is copied by the TexturePaint object.
[/quote]
So maybe if you put your image in like 100x100, repeating, it would be better then one large image… and that is if image can be split into smaller repeating ones (I hope you know what I mean with ā€œrepeatingā€).
Another thing… don’t know if it has any effect on performace but in your paint() you calculate and copy image data every time. You shuold do that one time, in constructor, like:


constuctor or something:
...
sky = spriteCache.getSprite("background.jpg");
sky_texture = new TexturePaint(sky,new Rectangle(0,0, sky.getWidth(), sky.getHeight()));
ground = spriteCache.getSprite("ground.png");
...
public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setPaint(sky_texture);
        g.fillRect(0,0,getWidth(),getHeight());
       
        player.paint(g);
        g.drawImage(ground , 0 , 690 , this); 
       
        paintfps(g);
         
        strategy.show();
    }

Also at the end, you might want to put g.dispose(); … it releases system resources that g was using (you recreate it every time so it dosen’t matter).
Try something of this, good luck.

Thanks a lot m8 , I will try to use all that you people adviced me to in here.I guess it all comes down to trying and trying and trying on my own :smiley: