Games & Gui

Ok here it goes.

I’m making a simple 2D tile game, would it be smart to goto slick instead of java2d as i’m doing now?
I only paint images, all 2D.
atm i’m using swing Actively rendering and it flickers as you can see here(move your mouse to the left and right edge)
http://www.kwbbz.be/Downloads/Java/versions/test/test.jnlp
the tile image itself also flickers(while it doesn’t get moved around)
I don’t know how to fix it, I need a hud to display stuff and I only know Swing
When I won’t use Swing is everything drawen at a fixed pixel position?

Swing doesn’t flicker when I don’t move stuff around(except for the Tile Image)

    
  public void moveOverTile(Tile t) {
    Point leftTop = mapPanel.getLeftTop();
    Dimension mapPanelDim = mapPanel.getSize();
    if (t == null) {
      return;
    }

    // Is the current Tile position on the left side of the mapPanel(viewArea)
    boolean move = leftTop.x + (t.getCol() * map.getTileSize()) <= mapPanel.getWidth() / 2;

    // check for change
    if (leftSide != move) {
      leftSide = move;

      if (leftSide) {
        tileInfo.setAbsolutePosition(mapPanelDim, TileInfo.Position.LOWER_RIGHT);
      } else {
        tileInfo.setAbsolutePosition(mapPanelDim, TileInfo.Position.LOWER_LEFT);
      }
    }
}

TileInfo:

  public void setAbsolutePosition(Dimension dim, Position pos) {
    Point currentLocation = getLocation();
    int x = 0, y = 0;
    switch (pos) {
      case LOWER_LEFT:
        x = LEFT_MARGIN;
        y = dim.height - HEIGHT - BUTTOM_MARGIN;
        break;
      case LOWER_RIGHT:
        x = dim.width - WIDTH - LEFT_MARGIN;
        y = dim.height - HEIGHT - BUTTOM_MARGIN;
        break;
    }

    // If one coordinate changed we need to change it's position!
    if (x != currentLocation.x || y != currentLocation.y) {
      setLocation(x, y);
    } else {
      System.out.println("stop calling this function, i already changed...");
    }
  }

I don’t see the System.out message so that’s good :stuck_out_tongue:
before i was setting the location for every update, but that wasen’t the cause of the problem… I still get Flickering.

When setLocation is called i can see a rectangle from old to new position being drawen in the background color(grey)
the rectangle has the same height as TileInfo.getHeight and the width is the same as TileInfo.getWidth()
what can be the cause of this rectangle?
I think it’s some kind of dirty rectangle but that’s supposed to be blocked?

I disabled automatic repaintings as in http://www.java-gaming.org/forums/index.php?topic=15140.15
so how does that rectangle get’s there anyway?

I solved it ;D

First remove the panel
update the position
add the panel again

Though this isn’t what you’re asking about, here is some feedback.

The usual thing to do is to override the paintComponent method of the panel and draw whatever part of the map is visible. Instead of changing the position of the panel, just store variables indicating which tile is currently at the top-left and draw whatever tiles are currently visible.

Additionally, it would be better if you couldn’t scroll past the edge of the map, though that’s a separate issue.

I didn’t play the game; I just looked at it and scrolled around.

Noo, you misunderstand me :smiley:
The ‘InfoPanels’ that show tile & unit information that’s are the ones that flicker badly when moved using setPosition()
I do scroll using a leftTop Point.

Agreed need to fix the out of bound scrolling(having problems with it)

and draw whatever tiles are currently visible.
I just draw every Tile in the map, aren’t drawing operations outside the panel bounds ignored?

No. Drawing still occurs if you call a draw routine. Even if the surface is outside of the visible area.