Drawing game elements on the screen

Hey folks,

I have a question about performance of drawing elements to the screen and maybe some suggestion at the current scheme I do use to draw the components in my game screen.

I will explain from the beginning what I’m doing and if I’m doing something(or everything wrong) indicate me what is wrong.

Components Loading

  • I got my game graphics stored in png images
  • I’m using MediaTracker to see when some image is fully loaded
  • While I’m loading graphics, I analyze the image content and convert color(255,0,255) to transparency and store them to Hashtable <String, BufferedImage>(I got different Hashtables, depending on what image category is)
    At the moment I got in memory: creature graphics, character graphics, menu backgrounds, etc

Map Creation

  • Map is created from a dat file which contains array of tile objects
  • Map is only created after character does login
  • At the moment after I create the map it’s 3200x3200px (is it too big??)

Drawing on screen

  • All of my components are drawn in personized JLabel(class which extends JLabel)
  • When map loaded JLabel is created and in paint method of JLabel map image is drawn
  • When characters are loaded, Hashtable <String, JLabel> is filled with characters and kept in memory for repaints
  • When map objects are loaded JLabel is created and put in another Hashtable <String, JLabel>
  • I got update function which calculates and changes the offsets and positions of elements
  • After update is done I’m creating temporary render array were I put the JLabels in the order of drawing them on screen
  • Repaint is done

Thanks is advance

I think the biggest problem there is using JLabels to old your graphics .
You should load them directly in a BufferedImage and draw those images in a Graphics2D (of a canvas, for example) .

I know this has been advised over and over, but there it goes again : Kev Glass’ Space Invaders 2d Tutorial :
http://www.cokeandcode.com/spaceinvaderstutorial

If you read only the first chapter thoroughly you’ll be able to understand how to make your game run smooth as butter on Java2D .
Bottomline : forget the JLabels (and most of swing components) if you want to see your game looking like a game .

(Oh and I forgot to mention when I played your game : it took an awful lot to download, and i’m pretty sure it’s because you’re loading external images from an URL. Don’t do that, pack them with your game or a jar, it’s way faster)

Ok, I see, then I’ll need to do the implementation I had before :smiley:
But there is another question… I still need to track the image positions in case of detection of MouseClicks right?

Thanks, going to check this right after finish working :stuck_out_tongue:

I was already adviced to do that, didn’t got time to test this.