Tile/LargeImage speed?

I’m using one large map image, (think 4000x4000) and am creating a scrolling background. Should I just cut the appropriate segment for then screen out each frame and display that, or should I cut the image up into an array of tiles upon loading of the map, and display only the appropriate tiles?

I have actually done both of these, a friend assuring me a tiled system is better, but I found that the tiles slow the program way too much. I’m getting 34 fps, when drawing 15x7 (105) tiles at 128x128 pixels. Now typing this that sounds like a hell of a lot of tiles. What sort of road should I go down here? (I’m drawing onto a JPanel, contained in a JFrame, each tile is a BufferedImage)

((I found some really good threads in search, but they where old and the links (some really useful) where broken, I have no problems with the whole scrolling/fixed camera concept))

First of all, you should take a look at active rendering on a Canvas instead of passive rendering on a JPanel

I will do this now and see how it helps. Thanks

you should not need tiles for 4000*4000 image size

[quote]…a friend assuring me a tiled system is better…
[/quote]
I realy depend on what you’re trying to do, use tiles if you want to use multiple times the same tile, to reduce memory for example or build level based on a collection of tiles, if you just want to scroll a 4000*4000 image this is not requiered

Think my stupid buffered images are getting blitted onto screen again, I’ve obviously cocked something up. Yeah all tiles are different, so I guess you’re right.

I’m following some tutorials, and this confused me.


        constructor()
        {
        JFrame container = new JFrame("name");
        JPanel panel = (JPanel) container.getContentPane();
        setSize(800,600);
        panel.add(this);
        setIgnoreRepaint(true);
        container.pack();

This is the constructor method for a class which extends Canvas. The JFrame and JPanel where not established before this constructor… How can they work? I was under the impression they would no longer exist outside the method.

Also whats going on with the Panel? After googling I still dont understand the getContentPane() method. I’ve seen people using ‘getContentPane().add(something);’ - from all the books I’ve read, they just added things without that extra method. And finally, why is this tutorial putting a Canvas into a JPanel into a JFrame… I find this all very confusing.

One of the problems this is causing me now is that I can no longer use getSize() to find the width or height. It always returns the initial values, and the JFrame or Panel can’t be found outside this constructor.

If anyone could clear this up this up for me I’d be really thankful

First, this is all covered in “The Java Tutorial” (http://download.oracle.com/javase/tutorial/) under Creating Graphical User Interfaces.

Second, you do not add components directly to a JFrame. It is built of sub containers, one of which is the content pane. The content pane is where you add your components. So if you want to draw to the entire space inside your JFrame you get the content pane and then set the layout to BorderLayout then add Canvas to the center. You can then either getContentPane().getSize() or Canvas getSize() to find out what your drawing area is.

Alternatively you can make your drawing area the size you want with the code below.


public class DrawingPanel extends JPanel {
  private Dimension size;

  public DrawingPanel(int width, int height) {
    size = new Dimension(width, height);
  }

  public Dimension getPreferredSize() {
    return size;
  }

  public static void main(String args[]) {
    JFrame f = new JFrame("My Game");
    f.setContentPane(new DrawingPanel(700, 700));
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

Since you are only drawing to the panel you could subclass any component like JComponent or Canvas to use as a drawing area.

frame.add() is a convenience method to frame.getContentPane().add().
You can call canvas.getSize() and it will return the same values as frame.getContentPane().getSize().

Aaah! I’m with you now. Thank you both. The book I had followed dedicated many chapters to Swing, I thought I understood, but had never seen mention of a content pane. I’ve got my Window working beautifully now. I see now the Panel is the container, and also removes the problem of the JFrame’s insets.

Active rendering on a canvas with volatile images, at full screen window size, 1200fps :D. Finally. Thanks everyone who helped.