0

I'm trying to make online game. The server sends data of surrounding "chunks" to client, which then saves the chunks and when drawing it checks the saved data of those chunks and draws them. So the data that is drawn is saved on Array on the client. Even if the data is saved on that array, if I connect to server using ip, the graphics seem to be flickering but when connectin as a localhost, the graphics are just fine. I've also tried to use double buffering (when running with localhost it works just fine without double buffer too) but when using ip it still flickers (even if I use double buffering (not too sure if it's working though..)) So what I'm asking is:

1) Why it flickers when using ip instead of localhost? (it can't be because of too much traffic over socket)

2) How could I do better double buffering (I've used multiple guides from youtube and internet like this) None of guides I've used so far haven't helped with flickering though..

Drawing method:

public class Area implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -7079895937774492131L;
private Tile[][] tiles;
private Object[][] objects;
private int x, y;
private static Chunk[] chunks;

//public static Area currentArea;
private static Area[][] areasave = new Area[512][512];

public Area(Tile[][] tiles, Object[][] objects, int x, int y){
    this.tiles = tiles;
    this.objects = objects;
    this.x = x;
    this.y = y;
}

public Area(Tile[][] tiles, Object[][] objects){
    this.tiles = tiles;
    this.objects = objects;
}

public static Tile[][] getTiles(){
    return tiles;
}

public static Object[][] getObjects(){
    return objects;
}

public static boolean addArea(Area area){
    areasave[area.x][area.y] = area;
    return true;
}

public static void Draw(Graphics g) {
    if(chunks != null){
        for(int i = 0; i < chunks.length; i++){
            if(areasave[chunks[i].getX()][chunks[i].getY()] != null){
                Tile[][] tiles = getTiles(areasave[chunks[i].getX()][chunks[i].getY()]);
                Object[][] objects = getObjects(areasave[chunks[i].getX()][chunks[i].getY()]);
                g.setColor(new Color(255, 255, 255));
                if(tiles != null){
                    for(int y = 0; y < tiles.length; y++){
                        for(int x = 0; x < tiles[0].length; x++){
                            if(tiles[x][y] != null){
                                if(new File(Frame.dpath +"TileImages/" +Tile.getID(tiles[x][y]) +".png").exists()){ //if we have tile for that ID
                                    g.drawImage(tileImages[Integer.parseInt(Tile.getID(tiles[x][y]))], ((Integer.parseInt(Tile.getX(tiles[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Player.y-Integer.parseInt(Tile.getY(tiles[x][y])))+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
                                }
                                else{
                                    g.drawImage(tileImages[0], ((Integer.parseInt(Tile.getX(tiles[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Integer.parseInt(Tile.getY(tiles[x][y]))-Player.y)+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
                                }
                            }   
                        }
                    }
                }
                if(objects != null){
                    for(int y = 0; y < objects.length; y++){
                        for(int x = 0; x < objects[0].length; x++){
                            if(objects[x][y] != null){
                                if(new File(Frame.dpath +"ObjectImages/" +Object.getID(objects[x][y]) +".png").exists()){ //if we have object for that ID
                                    g.drawImage(objectImages[Integer.parseInt(Object.getID(objects[x][y]))], ((Integer.parseInt(Object.getX(objects[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Player.y-Integer.parseInt(Object.getY(objects[x][y])))+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
                                    //System.out.println(Integer.parseInt(Object.getID(objects[x][y])) +", " +Object.getX(objects[x][y]) +"-" +Player.x +"," +((Player.y-Integer.parseInt(Object.getY(objects[x][y])))+GameLayout.tilesHeight/2)*(Screen.Height/GameLayout.tilesHeight));
                                }
                                else{
                                    g.drawImage(objectImages[0], ((Integer.parseInt(Object.getX(objects[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Integer.parseInt(Object.getY(objects[x][y]))-Player.y)+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
                                }
                            }   
                        }
                    }
                }
            }
        }
    }
}

the Draw method above did flicker even if I tried to use bufferStrategy (I'm not too sure if I did it all right though..) I'm more than happy to give more code/describe more etc. if needed

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3738243
  • 159
  • 2
  • 13
  • yes, the problem should be because getting information from remote IP takes lot more times than getting from local IP. Double buffering should help if you use it well. (drawing on offscreen and then sync offscreen to on) – gabor.harsanyi Feb 19 '15 at 11:35
  • @harcos Yes, well I tried that and well I don't know.. it shouldn't be because of that really, because the data is saved on the client? so it makes me wonder why it's slower even if the data is saved on client in both cases? and pretty much nothing is sent over the socket (just 2 int values) true! maybe the reason is because of those 2 int values! It starts drawing and when it gets those int values, the drawing changes in mid air so to say.. so it's like the model that it's drawing changes before it's fully drawn! That might be, must try out.. :D Thank you – user3738243 Feb 19 '15 at 11:45
  • you helped yourself but welcome :) – gabor.harsanyi Feb 19 '15 at 12:13

0 Answers0