Improving perfomance with gif drawing.

Hi,

I wrote a litte puzzle game (a remake of the classic Atomix).
Screenshot (127 kb) :

http://kh.weltregierung.de/trin/uploads/jatomix.png (slow)
http://domfree.de/trin/uploads/jAtomix.png (copy&paste link)

The problem is, that on startup the drawing of the background image, which is a 682x451x64 gif with 76.7 kb, is horrible slow. It takes about 10 seconds until the image appears.

It would be nice, if someone could give me some advice, how to speed up the drawing process.

This is a sample of the code I use.


import java.awt.*; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.image.*; 


public class test { 
    
    Image bg; 
    Screen screen; 

    public test() { 
  
        Frame frame = new Frame("test"); 
        frame.addWindowListener(new MyWindowAdapter()); 
        
        screen    = new Screen(); 
        frame.add(screen); 

        frame.setSize(675, 455); 
        frame.setResizable(false); 
        frame.setVisible(true); 
        
        bg = Toolkit.getDefaultToolkit().getImage("gifs/backgr.gif"); 
    } 
    
    public class Screen extends Canvas { 

        // doublebuffering 

        Image dbImage; 
        Graphics dbg; 

        public void update(Graphics g) { 
  
            // initialize buffer 

            if (dbImage == null) { 
                dbImage = createImage(this.getSize().width, this.getSize().height); 
                dbg = dbImage.getGraphics(); 
            } 

            // clear screen in background 
        
            dbg.setColor(getBackground()); 
            dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); 

            // draw elements in background 
            
            dbg.setColor(getForeground()); 
            paint(dbg); 

            // draw image on the screen 
            
            g.drawImage(dbImage, 0, 0, this); 
        } 

        public void paint(Graphics g) { 

            // draw background 

            g.drawImage(bg, 0, 0, this); 
        } 
    } 
    
    public static void main(String args[]) { 
       test jA = new test(); 
    } 
    
    class MyWindowAdapter extends WindowAdapter { 
        
        public void windowClosing(WindowEvent event) { 
            System.exit(0); 
        } 
    } 
} 

much thx :slight_smile:

OMG it’s trin :slight_smile:

Hey Richard (that was your name wasn’t it?) :wink:

You have to use a MediaTracker in order to be sure that the loading is done before you start drawing.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/MediaTracker.html

Everything you add will get cached (that’s nice this way) you should remember that if you load tons of huge images (let’s say 30mb of jpgs) and run out of memory. [But that won’t happen - usually ;)]

thx, that mediatracker thingy helped :smiley:

no need to load tons of images, just the background and some tiles graphics.

with this problem solved, I will be done with my game in a day or two. hooray :slight_smile: