this is how i do this issue in the constructor i give all the currents objects in the world(im going to work on it to only give all obvjects in an given area) but then at drawing time ((i use a volatileimage for double buffering thats hardware accelerated) i ignore all objects that are out of the field and draw the rest to the volatile image my player is always in the middle the player has coordinates in the world and everything else is drawn relative to the player (player is drawn at 300,300 on the canvas but in the world hes at 0,0 when an object is in the world is -40,40 its drawn at 260,340 and if hes in the world at 318,48 its not drawn bceause its too far away. i tested it with over a million random object within an area of 500000 pixels(it was drawing like 1000 objects on the screen and it ran pretty smooth)
package lima.Games.GUI;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import lima.Games.objects.*;
import java.awt.image.VolatileImage;
public class DrawArea extends Canvas{
int i = 0,fps = 0;
private Players player;
private NPC[] npc;
private StaticObjects[] sobject;
private VolatileImage volatileImg;
int[] playerpos;
public DrawArea(Players player, NPC[] npc, StaticObjects[] sobject){
this.player = player;
this.npc = npc;
this.sobject = sobject;
setSize(600,600);
}
public void paint(Graphics g){
createBackBuffer();
do {//creating the buffer and draw everything if there is some kind of problem and the image is lost do it again
GraphicsConfiguration gc = this.getGraphicsConfiguration();
int valCode = volatileImg.validate(gc);
if(valCode==VolatileImage.IMAGE_INCOMPATIBLE){
createBackBuffer(); // recreate the hardware accelerated image.
}
Graphics offscreenGraphics = volatileImg.getGraphics();
//offscreenGraphics
offscreenGraphics.clearRect(0,0,600,600); //clear the offscreen image
offscreenGraphics.drawString(fps+" fps", 20, 20);
offscreenGraphics.drawString(player.getPos()[0]+","+player.getPos()[1]+": "+player.getName(),(300)-10,(300)+20);
offscreenGraphics.drawRect((300)-10,(300)-10,20,20, true);
for(int i = 1; i < npc.length; i++){
if(getX(npc[i])-10 < 0 || getY(npc[i])-10 < 0){//if the x or y is to far to the west or north dont draw it
}else if(getX(npc[i])+10 > 600 || getY(npc[i])+10 > 600){//same goes for here but then for east and south
}else{
offscreenGraphics.drawString(npc[i].getPos()[0]+","+npc[i].getPos()[1]+": "+npc[i].getName(),getX(npc[i])-10,getY(npc[i])+20);
offscreenGraphics.drawRect(getX(npc[i])-10,getY(npc[i])-10,20,20,true);
}
}
for(int i = 1; i < sobject.length; i++){
if(getX(sobject[i])-10 < 0 || getY(sobject[i])-10 < 0){
}else if(getX(sobject[i])+10 > 600 || getY(sobject[i])+10 > 600){
}else{
offscreenGraphics.drawString(sobject[i].getPos()[0]+","+sobject[i].getPos()[1]+": "+sobject[i].getName(),getX(sobject[i])-10,getY(sobject[i])+20);
offscreenGraphics.drawRect(getX(sobject[i])-10,getY(sobject[i])-10,20,20,true);
}
}
g.drawImage(volatileImg,0,0,this);
} while(volatileImg.contentsLost());
}
public void Update(Players player, NPC[] npc, StaticObjects[] sobject, int fps){
this.player = player;
this.npc = npc;
this.sobject = sobject;
this.fps = fps;
repaint();
}
public void update(Graphics g)
{
paint(g);
}
private void createBackBuffer() {
GraphicsConfiguration gc = getGraphicsConfiguration();
volatileImg = gc.createCompatibleVolatileImage(getWidth(), getHeight());
}
public int getX(Objects thing){//this retrieves the x coordinates to draw the object relative to the player
int thisx = thing.getPos()[0];
thisx = thisx - player.getPos()[0];
thisx = 300+thisx;
return thisx;
}
public int getY(Objects thing){//sme as getX but then for the y coordinates
int thisy = thing.getPos()[1];
thisy = thisy - player.getPos()[1];
thisy = 300+thisy;
return thisy;
}
}