I still have a little bit of a flicker.
I've heard it could quite possibly be something with the refresh rate.
There is never really too much of a flicker, just usually 1 square, but can sometimes be several squares.
Here's the code.
/**
* @(#)MapPanel2.java
*
*
* @author
* @version 1.00 2012/8/11
*/
package client.panel;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import client.AnimalForest;
import common.Function;
import common.Movable;
import common.Player;
import common.item.Item;
import common.npc.NPC;
import common.tile.Tile;
public class MapPanel extends Canvas {
private BufferStrategy buffer;
private Graphics2D g;
private long lastLoopTime;
public AnimalForest game;
public Image[][] critters;
private Image attacking;
private Image[] rain;
// private String lume;
private ArrayList<Lume> lumes;
private ArrayList<Missile> missiles;
private String names;
public int mouseX,mouseY;
private boolean nightvision = false;
public int pixel_x=0,
pixel_y=0,
frame,
walkFrame,
dist=7,
startX, startY, endX, endY,
weatherFrame,
FPS=0;
MapPanelMouseListener mpml;
public void addMissile(String icon,int x,int y,Movable targ) {
Missile m = new Missile(icon,x,y,targ);
missiles.add(m);
}
public MapPanel(AnimalForest g) {
this.game=g;
lumes = new ArrayList<Lume>();
missiles = new ArrayList<Missile>();
setFocusable(false);
loadImages();
setIgnoreRepaint(true);
mpml = new MapPanelMouseListener(this);
this.addMouseListener(mpml);
this.addMouseMotionListener(mpml);
}
public void initGL() {
}
public Image loadImage(String ref) {
try {
URL url = this.getClass().getClassLoader().getResource(ref);
BufferedImage sourceImage = ImageIO.read(url);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
// draw our source image into the accelerated image
image.getGraphics().drawImage(sourceImage,0,0,null);
return image;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
public boolean mouseOn(int drawx,int drawy) {
if(mpml==null)return false;
int mx = mpml.mx;
int my = mpml.my;
if(mx>=drawx&&mx<=drawx+32) {
if (my>=drawy&&my<=drawy+32) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public Image convertImage(BufferedImage sourceImage) {
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
// draw our source image into the accelerated image
image.getGraphics().drawImage(sourceImage,0,0,null);
return image;
}
public void loadImages() {
BufferedImage[][] critters2=new BufferedImage[12][3];
critters2[0]=Function.divideImage(game.Function.loadImageLocal("images/critters/badger.PNG",null),32,32);
critters2[1]=Function.divideImage(game.Function.loadImageLocal("images/critters/beaver.PNG",null),32,32);
critters2[2]=Function.divideImage(game.Function.loadImageLocal("images/critters/ferret.PNG",null),32,32);
critters2[3]=Function.divideImage(game.Function.loadImageLocal("images/critters/fox.PNG",null),32,32);
critters2[4]=Function.divideImage(game.Function.loadImageLocal("images/critters/hedgehog.PNG",null),32,32);
critters2[5]=Function.divideImage(game.Function.loadImageLocal("images/critters/mouse.PNG",null),32,32);
critters2[6]=Function.divideImage(game.Function.loadImageLocal("images/critters/rabbit.PNG",null),32,32);
critters2[7]=Function.divideImage(game.Function.loadImageLocal("images/critters/rat.PNG",null),32,32);
critters2[8]=Function.divideImage(game.Function.loadImageLocal("images/critters/shrew.PNG",null),32,32);
critters2[9]=Function.divideImage(game.Function.loadImageLocal("images/critters/skunk.PNG",null),32,32);
critters2[10]=Function.divideImage(game.Function.loadImageLocal("images/critters/squirrel.PNG",null),32,32);
critters2[11]=Function.divideImage(game.Function.loadImageLocal("images/critters/weasel.PNG",null),32,32);
attacking=game.Function.loadImageLocal("images/GUI/attacking.PNG",new Color(255,0,255));
rain=Function.divideImage(game.Function.loadImageLocal("images/rain.PNG",new Color(255,0,255)),32,32);
BufferedImage[] tiles2=Function.divideImage(game.Function.loadImageLocal("images/tiles.PNG",new Color(255,0,255)),32,32);
critters=game.imageset.convertToAccelerated(critters2);
}
public void initGraphics() {
createBufferStrategy(2);
buffer=getBufferStrategy();
g = (Graphics2D)buffer.getDrawGraphics();
mainLoop();
}
public void paintTiles(int dist) {
int startX=game.src.x-(dist-1);
int startY=game.src.y-(dist-2);
if(startX<0)startX=0;
if(startY<1)startY=1;
int endX=game.src.x+dist;
int endY=game.src.y+dist;
if(endX>game.map.getWidth())endX=game.map.getWidth();
if(endY>game.map.getHeight())endY=game.map.getHeight();
for(int x=startX;x<endX;x++) {
for(int y=startY-1;y<endY;y++) {
if(game.map.tiles[x][y]!=null) {
paintTile(game.map.tiles[x][y]);
}
}
}
}
public void paintTile(Movable t) {
if(t==null)return;
int drawX=t.getDrawX(game.src)+pixel_x;
int drawY=t.getDrawY(game.src)+pixel_y;
g.drawImage(game.imageset.getImg(t),drawX,drawY,null);
if(game.rain&&game.src.z!=0&&game.src.lastburrow.length()<=2)g.drawImage(rain[weatherFrame],drawX,drawY,null);
if(t.desc!=null&&t.desc.startsWith("burrow")&&mouseX==t.x&&mouseY==t.y) {
// g.drawImage(set[t.icon],drawX+pixel_x,drawY+pixel_y,null);
// g.drawImage(imgSet[t.icon+1],drawX+pixel_x,drawY+pixel_y,null);
g.setColor(Color.red);
// names+=t.name+"=red;";
// g.drawString(t.name,drawX-(t.name.length()/2)*3,drawY-10);
}
}
public void paintGround(boolean npconly, int dist) {
int drawNames=0;
for(Movable m : game.map.ground) {
if(m.inRange(game.src,dist)) {
int drawX=m.getDrawX(game.src)+pixel_x;
int drawY=m.getDrawY(game.src)+pixel_y;
if(m instanceof NPC) {
NPC p = (NPC)m;
g.setColor(Color.white);
g.drawImage(critters[Function.race2id(p.race)][0],drawX,drawY,32,32,null);
if(mouseX==m.x&&mouseY==m.y&&npconly) {
names+=m.name+"=yellow;";
// g.drawString(p.name,drawX-(p.name.length()/2)*3,drawY-10);
}
}if(m instanceof Item&&!npconly) {
g.drawImage(game.imageset.getImg(m),drawX,drawY,32,32,null);
Item item=(Item)m;
if(!npconly&&item.enchanted.equals("Lit"))lumes.add(new Lume(m.x,m.y,1));
if(!npconly&&item.enchanted.equals("Lit3"))lumes.add(new Lume(m.x,m.y,3));
if(item.name.equals("Glowstone"))lumes.add(new Lume(m.x,m.y,Integer.parseInt(item.enchanted)));
if(mouseX==m.x&&mouseY==m.y) {
g.setColor(Color.white);
names+=m.name+"=white;";
// g.drawString(m.name,drawX-(m.name.length()/2)*3,drawY-10+(drawNames*13));
// drawNames++;
}
}if(m instanceof Tile) {
Tile tile = (Tile)m;
if(!npconly||tile.layer>=2) {
g.setColor(Color.red);
g.drawImage(game.imageset.getImg(tile),drawX,drawY,32,32,null);
if(tile.layer==0&&mouseX==m.x&&mouseY==m.y) {
names+=m.name+"=white;";
// g.drawString(m.name,drawX-(m.name.length()/2)*3,drawY-10);
}
}
}
} else if(m.inRange(game.src,7)) {
if(m.isItem()) {
Item item=(Item)m;
if(!npconly&&item.enchanted.equals("Lit"))lumes.add(new Lume(m.x,m.y,1));
if(!npconly&&item.enchanted.equals("Lit3"))lumes.add(new Lume(m.x,m.y,3));
if(item.name.equals("Glowstone"))lumes.add(new Lume(m.x,m.y,Integer.parseInt(item.enchanted)));
}
}
}
}
public void paintSelf() {
if(!game.src.hasTweak("Hiding")) {
// BufferedImage img = critters[game.src.icon][game.src.currentFrame];
int frame=game.src.getFrame();
if(pixel_x!=0||pixel_y!=0) {
frame+=walkFrame;
}
Image img = critters[game.src.icon_state][frame];
if(img.getWidth(null)>32||img.getHeight(null)>32) {
g.drawImage(img,160-(img.getWidth(null)-32)/2,160-(img.getHeight(null)-32)/2,null);
} else g.drawImage(img,160,160,null);
}
}
public void paintMissiles() {
for(Missile m : missiles) {
int drawX=0;
int drawY=11;
drawX+=5+(m.x-game.src.x);
drawY-=(6+(m.y-game.src.y));
drawX*=32;drawY*=32;
drawX+=pixel_x;drawY+=pixel_y;
drawX+=m.pixel_x;drawY+=m.pixel_y;
g.drawImage(game.imageset.getImg(m.icon,m.dir,m.currentFrame),drawX,drawY,null);
}
}
public void paintPlayers(int dist) {
// g.setColor(Color.white);
for(Player p : game.getActivePlayers()) {
if(game.src.inRange(p,7)||game.src.inRange(p,7)&&game.src.isLumed()) {
if(p.hasTweak("Invisibility"))continue;
if(p.hasTweak("Hiding")&&!game.src.hasTweak("True Sight"))continue;
int drawX=p.getDrawX(game.src);
int drawY=p.getDrawY(game.src);
drawX+=pixel_x+p.pixel_x;
drawY+=pixel_y+p.pixel_y;
int frame=p.getFrame();
if(p.pixel_x!=0||p.pixel_y!=0) {
frame+=walkFrame;
}
g.drawImage(critters[p.icon_state][frame],drawX,drawY,null);
if(game.server.target==p) {
g.drawImage(attacking,drawX+8,drawY-16,null);
String thename=p.initialname+" the "+Function.id2race(p.icon_state);
names+=thename+"=red="+drawX+"="+drawY+";";
// g.drawString(thename,drawX-(thename.length()/2)*3,drawY-10);
} else if(mouseOn(drawX,drawY)||game.kl.keydown==KeyEvent.VK_CONTROL){
String thename=p.initialname+" the "+Function.id2race(p.icon_state);
names+=thename+"=red="+drawX+"="+drawY+";";
// g.drawString(thename,drawX-(thename.length()/2)*3,drawY-10);
}
}
}
}
public void paintNames() {
if(names==null||names.equals(""))return;
int amount=0;
for(String info : names.split(";")) {
String[] split = info.split("=");
int drawX=0;
int drawY=11;
drawX+=5+(mouseX-game.src.x);
drawY-=(6+(mouseY-game.src.y));
drawX*=32;drawY*=32;
drawX+=pixel_x;drawY+=pixel_y;
if(split[1].equals("white")) {
g.setColor(Color.white);
} else if(split[1].equals("yellow")) {
g.setColor(Color.yellow);
} else if(split[1].equals("red")) {
g.setColor(Color.green);
}
if(split.length>2) {
try {
g.drawString(split[0],(Integer.parseInt(split[2]))-(split[0].length()/2)*3,(Integer.parseInt(split[3]))-10-(amount*11));
} catch(Exception e) {
g.drawString(split[0],(drawX)-(split[0].length()/2)*3,(drawY)-10-(amount*11));
amount++;
}
} else {
g.drawString(split[0],(drawX)-(split[0].length()/2)*3,(drawY)-10-(amount*11));
amount++;
}
}
names="";
}
public int getDrawX(Player p) {
int drawX=0;
drawX+=5+(p.x-game.src.x);
drawX*=32;
drawX+=pixel_x;
drawX+=p.pixel_x;
return drawX;
}
public int getDrawY(Player p) {
int drawY=11;
drawY-=(6+(p.y-game.src.y));
drawY*=32;
drawY+=pixel_y;
drawY+=p.pixel_y;
return drawY;
}
public void nightTile() {
try {
if(game.src.isLumed()) {
lumes.add(new Lume(game.src.x,game.src.y,game.src.getLume()));
}
for(Player p:game.getActivePlayers()) {
if(game.src.inRange(p,10)&&p.isLumed()) {
lumes.add(new Lume(p.x,p.y,p.getLume()));
}
}
int[][] lumed = new int[game.map.getWidth()][game.map.getHeight()];
for(Lume lume : lumes) {
int theX=lume.x;
int theY=lume.y;
int dist=lume.power;
for(int x=Math.max(0, theX-dist);x<Math.min(game.map.getWidth(), (theX+(dist+1)));x++) {
for(int y=Math.max(0, theY-dist);y<Math.min(game.map.getHeight(),theY+(dist+1));y++) {
int theDist=game.src.getDist(x,y);
if(theDist>2) {
Tile t = game.map.tiles[x][y];
if(t==null)continue;
int darkness=t.getDist(theX,theY)+game.src.getDist(x,y)-(dist/2);
if(darkness>=6)darkness=5;
try {
if(lumed[x][y]==0||lumed[x][y]>darkness) {
lumed[x][y]=darkness;
}
} catch(Exception e) {
}
} else {
lumed[x][y]=-1;
}
}
}
}
for(int x=Math.max(0, game.src.x-7);x<Math.min(game.map.getWidth(), (game.src.x+(7)));x++) {
for(int y=Math.max(0, game.src.y-7);y<Math.min(game.map.getHeight(),game.src.y+(7));y++) {
if(lumed[x][y]==0)continue;
Tile t = game.map.tiles[x][y];
if(t==null)continue;
paintTile(t);
int drawX=t.getDrawX(game.src)+pixel_x;
int drawY=t.getDrawY(game.src)+pixel_y;
for(Movable M : game.map.getGrounds(x,y)) {
paintTile(M);
}
if(game.map.getWidth()<60)continue;
if(lumed[x][y]>0) {
g.drawImage(game.darkness[lumed[x][y]],drawX,drawY,32,32,null);
}
}}
} catch(Exception e){e.printStackTrace();}
lumes.removeAll(lumes);
}
public void paintShadows(int z, int dist2) {
int disty=6;
int startX=game.src.x-(disty-1);
int startY=game.src.y-(disty-1);
if(startX<0)startX=0;
if(startY<1)startY=1;
int endX=game.src.x+disty;
int endY=game.src.y+disty;
int xx=0,yy=0;
if(endX>game.map.getWidth())endX=game.map.getWidth()-1;
if(endY>game.map.getHeight())endY=game.map.getHeight()-1;
g.setColor(Color.black);
for(int x2=startX;x2<endX;x2++) {
for(int y=startY-1;y<endY;y++) {
Tile t = game.map.tiles[x2][y];
if(t!=null&&t.opacity>=1) {
if(t.opacity==1&&this.nightvision&&t.getDist(game.src)==1) {
continue;
}
if(t.x==game.src.x&&t.y==game.src.y)continue;
int drawX=0;
int drawY=11;
drawX+=5+(x2-game.src.x);drawX*=32;
drawY-=(6+(y-game.src.y));drawY*=32;
drawX+=pixel_x;
drawY+=pixel_y;
int xdif=(game.src.x-t.x);
int ydif=(game.src.y-t.y);
for(int x=1;x<=7;x++) {
xx=x;yy=x;
if(ydif>0)yy*=-1;
if(xdif>0)xx*=-1;
g.fillRect(drawX+(xx*32),drawY-(yy*32),32,32);
}
if(ydif==0) {
int posxdif=xdif;
if(posxdif<=-1)posxdif*=-1;
for(int x=1;x<=7-posxdif;x++) {
xx=x;yy=x;
if(xdif<0) {
g.fillRect(drawX+(xx*32),drawY,32,32);
}else {
g.fillRect(drawX-(xx*32),drawY,32,32);
}
if(xdif>0) {
g.fillRect(drawX-(xx*32),drawY-(yy*32),32,32+(xx*32) );
g.fillRect(drawX-(xx*32),drawY+(yy-1*32)+32,32,32+(xx*32) );
}else {
g.fillRect(drawX+(xx*32),drawY-(yy*32),32,32+(xx*32) );
g.fillRect(drawX+(xx*32),drawY+(yy-1*32)+32,32,32+(xx*32) );
}
}
}if(xdif==0) {
int posydif=ydif;
if(posydif<=-1)posydif*=-1;
for(int x=1;x<=8-posydif;x++) {
xx=x;yy=x;
if(ydif<0) {
g.fillRect(drawX,drawY-(xx*32),32,32);
g.fillRect(drawX-32,drawY-(xx*32),32,32);
}else {
g.fillRect(drawX,drawY+(xx*32),32,32);
g.fillRect(drawX-32,drawY+(xx*32),32,32);
}
if(ydif>0) {
g.fillRect(drawX,drawY+(xx*32),32+(xx*32),32);
}else {
g.fillRect(drawX,drawY-(xx*32),32+(xx*32),32);
}
if(ydif>0) {
g.fillRect(drawX-(xx*32),drawY+(xx*32),32+(xx*32),32);
}else {
g.fillRect(drawX-(xx*32),drawY-(xx*32),32+(xx*32),32);
}
}
}
}
}}
}
public void paintWeather() {
}
int count=0,count2=0;
int pixel_amount=4;
public void animationTick() {
if(game.src!=null) {
count++;
count2++;
startX=game.src.x-5;
startY=game.src.y-5;
if(startX<0)startX=0;
if(startY<1)startY=1;
endX=game.src.x+6;
endY=game.src.y+6;
if(endX>game.map.getWidth())endX=game.map.getWidth();
if(endY>game.map.getHeight())endY=game.map.getHeight();
for(Missile m:this.missiles) {
Movable t = m.target;
if(m.x!=t.x&&m.pixel_x==0) {
if(m.x<t.x) {
m.x++;
m.pixel_x=-32;
m.dir=2;
} else {
m.x--;
m.pixel_x=32;
m.dir=3;
}
}
if(m.y!=t.y&&m.pixel_y==0) {
if(m.y<t.y) {
m.y++;
m.pixel_y=32;
if(m.dir==2) {
m.dir=4;
} else if(m.dir==3) {
m.dir=5;
} else {
m.dir=1;
}
} else {
m.y--;
m.pixel_y=-32;
if(m.dir==2) {
m.dir=6;
} else if(m.dir==3) {
m.dir=7;
} else {
m.dir=0;
}
}
}
m.pixelTick(m.speed);
if(m.x==t.x&&m.y==t.y&&m.pixel_x==0&&m.pixel_y==0) {
missiles.remove(m);
break;
}
if(count2!=4)break;
int frames=game.imageset.countFrames(m.icon);
if(frames>1) {
m.currentFrame++;
if(m.currentFrame==frames) {
m.currentFrame=0;
}
// t.icon_state++;
}
}
if(count2==10) {
if(game.src.hasTrait("Night Vision")) {
this.nightvision = true;
}
if(game.map.getWidth()<60||game.daynight.equals("Night")&&game.src.z!=0) {
dist=3;
} else {
dist=7;
}
count=0;
weatherFrame++;
walkFrame++;
if(weatherFrame==3)weatherFrame=0;
if(walkFrame==4)walkFrame=0;
if(game.src.hasTweak("Running")) {
pixel_amount=4;
} else {
pixel_amount=3;
}
count2=0;
for(int x=startX;x<endX;x++) {
for(int y=startY-1;y<endY;y++) {
Tile t = game.map.tiles[x][y];
int frames=game.imageset.countFrames(t);
if(t!=null&&frames>1) {
t.currentFrame++;
if(t.currentFrame==frames) {
t.currentFrame=0;
}
t.icon_state++;
}
}}
for(Movable t:game.map.ground) {
int frames=game.imageset.countFrames(t);
if(frames>1) {
t.currentFrame++;
if(t.currentFrame==frames) {
t.currentFrame=0;
}
// t.icon_state++;
}
}
for(Item t:game.src.items) {
int frames=game.imageset.countFrames(t);
if(frames>1) {
t.currentFrame++;
if(t.currentFrame==frames) {
t.currentFrame=0;
}
// t.icon_state++;
}
}
game.stats.inventory.updateAnimation();
game.stats.equipment.updateAnimation();
/* if(game.daynight.equals("Night")&&game.src.z!=0) {
dist=3;
if(game.src.lefthand!=null&&game.src.lefthand.enchanted.equals("Lit"))dist=4;
if(game.src.righthand!=null&&game.src.righthand.enchanted.equals("Lit"))dist=4;
} else {
dist=7;
}*/
}
game.src.pixel_x=pixel_x;
game.src.pixel_y=pixel_y;
game.src.pixelTick(pixel_amount);
pixel_x=game.src.pixel_x;
pixel_y=game.src.pixel_y;
for(Player p:game.getPlayers() ) {
if(p.hasTweak("Running")) {
p.pixelTick(3);
} else {
p.pixelTick(2);
}
}
game.kl.move();
}
}
public void mainLoop() {
Thread t = new Thread(new Runnable() {
public void run() {
int count=0;
while(true) {
try {
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
//
g = (Graphics2D) buffer.getDrawGraphics();
animationTick();
g.setColor(Color.black);
g.fillRect(0,0,352,352);
if(game==null||game.src==null) {
return;
}
if(game.map.getWidth()==1&&game.map.getHeight()==1) {
g.setColor(Color.red);
g.drawString("Loading map...",10,25);
} else if(game.src.conscious()) {
paintTiles(dist);
paintGround(false,dist);
if(dist<7) {
dist=3;//make it so you can only see 3 tiles away
if(game.src.z!=0)nightTile();//paints far away tiles that are lumed up by an object (torch/glowstone)
paintSelf();
paintPlayers(dist);
} else {
paintSelf();
paintPlayers(dist);
}
paintGround(true,dist);
paintShadows(0,dist);
if(game.src.z!=0&&!game.src.lastburrow.equals(""))paintWeather();
if(game.src.hasTweak("True Sight")) {//a spell
paintPlayers(dist);
paintGround(true,dist);
}
paintMissiles();
paintNames();
} else {
if(game.map.getWidth()>game.src.x&&game.map.getHeight()>game.src.y) {
paintTile(game.map.tiles[game.src.x][game.src.y]);
}
for(Movable M:game.map.getGrounds(game.src.x, game.src.y)) {
if(M instanceof Item) {
g.drawImage(game.imageset.getImg(M),160,160,32,32,null);
}
}
paintSelf();
}
//
g.dispose();
buffer.show();
Toolkit.getDefaultToolkit().sync();
Thread.sleep(10);//100 FPS!
} catch(Exception e) {}
}
}
});
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
}
class Lume {
int x,y,power;
public Lume(int X, int Y, int P) {
x=X;
y=Y;
power=P;
}
}