Java 当从另一个ip获取数据时,客户端图形会闪烁,但使用localhost时效果良好
我正在尝试制作网络游戏。服务器向客户机发送周围“块”的数据,然后客户机保存这些块,并在绘制时检查这些块的保存数据并绘制它们。因此,绘制的数据保存在客户端的数组中。即使数据保存在该阵列上,如果我使用ip连接到服务器,图形似乎会闪烁,但当连接为本地主机时,图形就很好了。我也尝试过使用双缓冲(当使用localhost运行时,没有双缓冲也可以正常工作),但是当使用ip时,它仍然会闪烁(即使我使用双缓冲(不太确定它是否工作…),所以我想问的是: 1) 为什么在使用ip而不是本地主机时会闪烁?(这不可能是因为套接字上的流量太大) 2) 我如何才能更好地进行双缓冲(我使用了来自youtube和类似互联网的多个指南) 不过,到目前为止,我使用的指南中没有一个对闪烁没有帮助 绘图方法:Java 当从另一个ip获取数据时,客户端图形会闪烁,但使用localhost时效果良好,java,graphics,awt,flicker,doublebuffered,Java,Graphics,Awt,Flicker,Doublebuffered,我正在尝试制作网络游戏。服务器向客户机发送周围“块”的数据,然后客户机保存这些块,并在绘制时检查这些块的保存数据并绘制它们。因此,绘制的数据保存在客户端的数组中。即使数据保存在该阵列上,如果我使用ip连接到服务器,图形似乎会闪烁,但当连接为本地主机时,图形就很好了。我也尝试过使用双缓冲(当使用localhost运行时,没有双缓冲也可以正常工作),但是当使用ip时,它仍然会闪烁(即使我使用双缓冲(不太确定它是否工作…),所以我想问的是: 1) 为什么在使用ip而不是本地主机时会闪烁?(这不可能是因
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);
}
}
}
}
}
}
}
}
}
公共类区域实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=-7079895937774492131L;
私人瓷砖[][]瓷砖;
私有对象[][]对象;
私有整数x,y;
私有静态块[]块;
//公共静态区;
私有静态区域[][]区域保存=新区域[512][512];
公共区域(平铺[][]平铺,对象[][]对象,整数x,整数y){
this.tiles=tiles;
this.objects=对象;
这个.x=x;
这个。y=y;
}
公共区域(瓷砖[][]瓷砖,对象[][]对象){
this.tiles=tiles;
this.objects=对象;
}
公共静态互动程序[][]getTiles(){
返回瓷砖;
}
公共静态对象[][]getObjects(){
归还物品;
}
公共静态布尔添加区域(区域){
areasave[area.x][area.y]=面积;
返回true;
}
公共静态空白绘制(图g){
if(块!=null){
for(int i=0;i
即使我尝试使用bufferStrategy,上面的Draw方法也会闪烁(但我不太确定我是否做得很好..)我非常乐意提供更多代码/描述更多内容等。如果需要是的,问题应该是因为从远程IP获取信息比从本地IP获取信息需要更多时间。如果使用得当,双缓冲应该会有所帮助。(在屏幕外绘制,然后在屏幕外同步到打开)@harcos是的,我试过了,但我不知道。这真的不应该是因为这个,因为数据保存在客户机上?所以我想知道为什么即使数据以两种方式保存在客户机上,速度也会变慢