import java.lang.*; import java.io.*; import java.awt.*; class LocalMap extends java.awt.Canvas { NetrekState game_state; Eraser eraser; NetrekImageLib img_lib = null; Image local_img = null; Graphics local_g = null; Dimension gsize; public LocalMap(NetrekState game_state, NetrekImageLib img_lib) { this.game_state = game_state; this.img_lib = img_lib; //eraser = new Eraser(2*MAXPLAYER + MAXPLAYER*MAXTORP + MAXPLAYER*MAXPLASMA //+ MAXPLANET, MAXPLAYER + 2*MAXPLAYER); eraser = null; resize(game_state.state.winside, game_state.state.winside); gsize = size(); } public void SetImageLib(NetrekImageLib lib) { img_lib = lib; } public void SetGameState(NetrekState game_state) { this.game_state = game_state; } public void Refresh() { } public void paint() { Graphics g = getGraphics(); if (g != null) update(g); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (!isVisible()) return; if (!game_state.state.ready) { //g.setColor(getBackground()); g.clearRect(0, 0, gsize.width, gsize.height); return; } if (local_img == null) { local_img = createImage(gsize.width, gsize.height); local_g = local_img.getGraphics(); local_g.setFont(getFont()); } //eraser.Erase(g, bgcolor); local_g.setColor(getBackground()); local_g.fillRect(0, 0, gsize.width, gsize.height); if (img_lib == null) local_g.drawString( "Image library not initialized properly. Please reload.", 50, 50); else synchronized(game_state) { game_state.planets.paintLocal(img_lib, local_g, game_state.state,eraser); game_state.players.paintLocal(img_lib, local_g, game_state.state,eraser); game_state.phasers.paintLocal(img_lib, local_g, game_state.state,eraser); game_state.plasmas.paintLocal(img_lib, local_g, game_state.state,eraser); game_state.torps.paintLocal(img_lib, local_g, game_state.state, eraser); } g.drawImage(local_img, 0, 0, null); } } class GalacticMap extends java.awt.Canvas { final static int DETAIL = 40; NetrekState game_state; Eraser eraser; NetrekImageLib img_lib = null; Dimension gsize; boolean redraw = true; public GalacticMap(NetrekState game_state, NetrekImageLib img_lib) { this.game_state = game_state; this.img_lib = img_lib; eraser = new Eraser(NetrekState.MAXPLAYER + NetrekState.MAXPLANET, 0); resize(game_state.state.winside, game_state.state.winside); gsize = size(); } public void SetImageLib(NetrekImageLib lib) { img_lib = lib; } public void SetGameState(NetrekState game_state) { this.game_state = game_state; invalidate(); } public void Refresh() { invalidate(); } public void paint() { Graphics g = getGraphics(); if (g != null) if (isValid()) IncrementalPaint(g); else { update(g); validate(); } } public void paint(Graphics g) { redraw = true; IncrementalPaint(g); } public void IncrementalPaint(Graphics g) { if (!isVisible()) return; if (!game_state.state.ready || img_lib == null) return; g.setFont(getFont()); synchronized(game_state) { game_state.players.PrepareToErase(eraser, game_state.planets, game_state.state); game_state.planets.PrepareToErase(eraser); eraser.Erase(g, getBackground()); game_state.planets.GalacticRedraw(img_lib, g, game_state.state, redraw); game_state.players.GalacticRedraw(img_lib, g, game_state.state, redraw); } redraw = false; } } class DashBoard extends java.awt.Canvas { final static int BARLEN = 56; final static int BARWDT = 9; NetrekState game_state; Image buf_image; Graphics buf_g; Dimension gsize; int old_typ = -1; int old_flg = -1; int old_spd = -1; int old_cur_spd = -1; int old_shl = -1; int old_dam = -1; int old_arm = -1; int old_cur_arm = -1; int old_wpn = -1; int old_egn = -1; int old_ful = -1; public DashBoard(NetrekState game_state) { this.game_state = game_state; buf_image = null; setForeground(Color.white); setBackground(Color.black); resize(500, 43); gsize = size(); } public void SetGameState(NetrekState game_state) { this.game_state = game_state; } public void Refresh() { invalidate(); } public void paint() { Graphics g = getGraphics(); if (g != null) if (isValid()) IncrementalPaint(g); else { update(g); validate(); } } public void paint(Graphics g) { old_typ = -1; old_flg = -1; old_spd = -1; old_cur_spd = -1; old_shl = -1; old_dam = -1; old_arm = -1; old_cur_arm = -1; old_wpn = -1; old_egn = -1; old_ful = -1; IncrementalPaint(g); } public void IncrementalPaint(Graphics buf_g) { if (!isVisible()) return; Player me = game_state.state.me; if (me == null || !game_state.state.ready) return; buf_g.setFont(getFont()); synchronized(game_state) { Color col; /* if (buf_image == null) { buf_image = createImage(gsize.width, gsize.height); buf_g = buf_image.getGraphics(); buf_g.setFont(getFont()); buf_g.setColor(getBackground()); buf_g.fillRect(0, 0, gsize.width, gsize.height); } */ Ship ship = me.p_ship; boolean all = (old_typ != ship.s_type); old_typ = ship.s_type; int max_spd = (int) ((ship.s_maxspeed + 2) - (ship.s_maxspeed + 1) * ((double) me.p_damage / ship.s_maxdamage)); if (max_spd > ship.s_maxspeed) max_spd = ship.s_maxspeed; if (max_spd < 0) max_spd = 0; if (all || me.p_speed != old_spd || max_spd != old_cur_spd) { old_spd = me.p_speed; old_cur_spd = max_spd; if (old_spd >= ship.s_maxspeed - 2) col = Color.yellow; else col = Color.white; drawBar(buf_g, col, 90, 3, "Sp", me.p_speed, max_spd, ship.s_maxspeed, 3); } if (all || me.p_shield != old_shl) { old_shl = me.p_shield; int value = (100 * me.p_shield) / ship.s_maxshield; if (value < 50) col = Color.red; else if (value < 90) col = Color.yellow; else col = Color.white; drawBar(buf_g, col, 90, 17, "Sh", me.p_shield, ship.s_maxshield, ship.s_maxshield, 3); } if (all || me.p_damage != old_dam) { old_dam = me.p_damage; int value = (100 * me.p_damage) / ship.s_maxdamage; if (value > 50) col = Color.red; else if (value > 10) col = Color.yellow; else col = Color.white; drawBar(buf_g, col, 90, 31, "Da", me.p_damage, ship.s_maxdamage, ship.s_maxdamage, 3); } int armies; if (ship.s_type == Ship.STARBASE) armies = ship.s_maxarmies; else { if (ship.s_type == Ship.ASSAULT) armies = 3; else armies = 2; armies = (int) (me.p_kills * armies); if (armies > ship.s_maxarmies) armies = ship.s_maxarmies; } if (all || me.p_armies != old_arm || armies != old_cur_arm) { old_arm = me.p_armies; old_cur_arm = armies; if (old_arm <= 3) col = Color.white; else if (old_arm <= 5) col = Color.yellow; else col = Color.red; drawBar(buf_g, col, 218, 3, "Ar", me.p_armies, armies, ship.s_maxarmies, 3); } if (all || me.p_wtemp != old_wpn) { old_wpn = me.p_wtemp; int value = (100 * me.p_wtemp) / ship.s_maxwpntemp; if (value > 50) col = Color.red; else if (value > 20) col = Color.yellow; else col = Color.white; drawBar(buf_g,col, 218, 17, "Wt", me.p_wtemp/10, ship.s_maxwpntemp/10, ship.s_maxwpntemp/10, 3); } if (all || me.p_etemp != old_egn) { old_egn = me.p_etemp; int value = (100 * me.p_etemp) / ship.s_maxegntemp; if (value > 75) col = Color.red; else if (value > 25) col = Color.yellow; else col = Color.white; drawBar(buf_g,col, 218, 31, "Et", me.p_etemp/10, ship.s_maxegntemp/10, ship.s_maxegntemp/10, 3); } if (all || me.p_fuel != old_ful) { old_ful = me.p_fuel; int value = (100 * me.p_fuel) / ship.s_maxfuel; if (value < 50) col = Color.red; else if (value < 90) col = Color.yellow; else col = Color.white; drawBar(buf_g, col, 346, 3, "Fu", me.p_fuel, ship.s_maxfuel, ship.s_maxfuel, 5); } //g.drawImage(buf_image, 0, 0, null); } } void IntToChars(char[] text, int val, int digits, int pos) { for (int i = 1; i <= digits; i++, val /= 10) if (val != 0 || i == 1) text[digits+pos-i] = (char) ((val % 10) + (int) '0'); else text[digits+pos-i] = ' '; } public void drawBar(Graphics g, Color col, int x, int y, String name, int value, int tmpmax, int max, int digits) { char caption[] = new char[32]; int ptr = 0; caption[ptr++] = name.charAt(0); caption[ptr++] = name.charAt(1); caption[ptr++] = '['; IntToChars(caption, value, digits, ptr); ptr += digits; caption[ptr++] = '/'; IntToChars(caption, tmpmax, digits, ptr); ptr += digits; caption[ptr++] = ']'; FontMetrics fm = g.getFontMetrics(); int yoff = BARWDT - fm.getMaxDescent() + 1; int spc = fm.getMaxAdvance(); //g.setColor(getBackground()); g.clearRect(x, y-1, spc*ptr + BARLEN, BARWDT+2); g.setColor(getForeground()); g.drawChars(caption, 0, ptr, x, y + yoff); x += spc * ptr; g.drawRect(x, y, BARLEN-1, BARWDT-1); if (tmpmax < 0) tmpmax = 0; if (tmpmax < max) { int xmax = (BARLEN * tmpmax) / max; g.drawLine(x+xmax, y, x+xmax, y+BARWDT-1); g.drawLine(x+xmax, y + BARWDT/2, x+BARLEN-1, y+BARWDT/2); } if (value > max) value = max; int xval = (BARLEN * value) / max; g.setColor(col); g.fillRect(x, y, xval, BARWDT); } }