1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| 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 { GraphicsConfiguration gc = this.getGraphicsConfiguration();
int valCode = volatileImg.validate(gc); if(valCode==VolatileImage.IMAGE_INCOMPATIBLE){ createBackBuffer(); }
Graphics offscreenGraphics = volatileImg.getGraphics(); offscreenGraphics.clearRect(0,0,600,600); 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){ }else if(getX(npc[i])+10 > 600 || getY(npc[i])+10 > 600){ }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){ int thisx = thing.getPos()[0]; thisx = thisx - player.getPos()[0]; thisx = 300+thisx; return thisx; } public int getY(Objects thing){ int thisy = thing.getPos()[1]; thisy = thisy - player.getPos()[1]; thisy = 300+thisy; return thisy; } } |