import java.lang.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.image.*; public class NetrekImageLib { final static int MAXWIDTH = 80; final static int MAXHEIGHT = 80; final static int MAXBITMAP = 800; final int colors[] = { 0xFFFFFF00, 0xFFFFA500, 0xFF00FF00, 0xFF00FFFF, 0xFFC0C0C0, 0xFFFFFFFF }; Component converter; NetrekColors cols; Hashtable classes; double scale; public NetrekImageLib(NetrekColors colors, Component conv) { cols = colors; converter = conv; Initialize(); } void Initialize() { classes = new Hashtable(); } public Color GetRaceColor(int racenum) { if (racenum < 0 || racenum >= cols.Races.length) return new Color(0); return cols.Races[racenum]; } public void AddBitmap(String family, String aclass, int frame, int numframes, int width, int height, byte[] bits) { Hashtable families = (Hashtable) classes.get(aclass); if (families == null) { families = new Hashtable(); classes.put(aclass, families); } Hashtable frames = (Hashtable) families.get(family); if (frames == null) { frames = new Hashtable(); families.put(family, frames); } NetrekImageFrame iframe = (NetrekImageFrame)frames.get(new Integer(frame)); if (iframe == null) { iframe = new NetrekImageFrame(); frames.put(new Integer(frame), iframe); } iframe.width = width; iframe.height = height; iframe.bits = bits; } public void AddImage(String family, String aclass, int racenum, int frame, int numframes, Image image) { Hashtable families = (Hashtable) classes.get(aclass); if (families == null) { families = new Hashtable(); classes.put(aclass, families); } Hashtable frames = (Hashtable) families.get(family); if (frames == null) { frames = new Hashtable(); families.put(family, frames); } NetrekImageFrame iframe = (NetrekImageFrame)frames.get(new Integer(frame)); if (iframe == null) { iframe = new NetrekImageFrame(); frames.put(new Integer(frame), iframe); } iframe.images[racenum] = image; } public Image GetImage(String family, String aclass, int racenum, int frame) { if (racenum < 0 || racenum > 5) { System.err.println("Bad racenum: " + racenum); return null; } Hashtable families = (Hashtable) classes.get(aclass); if (families == null) { return null; } Hashtable frames = (Hashtable) families.get(family); if (frames == null) { return null; } NetrekImageFrame iframe = (NetrekImageFrame)frames.get(new Integer(frame)); if (iframe == null) { return null; } if (racenum < 0 || racenum >= iframe.images.length) return null; if (iframe.images[racenum] == null && iframe.bits != null) iframe.images[racenum] = ConvertImage(iframe.width, iframe.height, iframe.bits, colors[racenum]); return iframe.images[racenum]; } public Image ConvertImage(int width, int height, byte[] bits, int color) { int scanline = (width + 7)/8; int inptr = 0; int outptr = 0; int imageArr[] = new int[width*height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) if (((bits[inptr + j/8] >> j%8) & 1) == 0) imageArr[outptr++] = 0; else imageArr[outptr++] = color; inptr += scanline; } MemoryImageSource src = new MemoryImageSource(width, height, imageArr, 0, width); Image img = converter.createImage(src); return img; } public void LoadLibrary(DataInput data) { byte family[] = new byte[8]; byte aclass[] = new byte[8]; byte bits[]; try { while(true) { try {Thread.sleep(1);} catch (InterruptedException e){} data.readFully(family); String sfamily = new String(family, 0); sfamily = sfamily.substring(0, sfamily.indexOf(0)); data.readFully(aclass); String sclass = new String(aclass, 0); sclass = sclass.substring(0, sclass.indexOf(0)); short race = data.readShort(); int frame = data.readShort(); int numframes = data.readShort(); int width = data.readShort(); int height = data.readShort(); int pad = data.readShort(); int size = data.readInt(); bits = new byte[size]; data.readFully(bits); AddBitmap(sfamily, sclass, frame, numframes, width, height, bits); } } catch (IOException ex) { return; } } } class NetrekColors { final int colors[] = { 0xFFFF00, 0xFFA500, 0x00FF00, 0x00FFFF, 0xC0C0C0, 0xFFFFFF }; public Color Races[]; public Color White; public Color Black; public Color Red; public Color Yellow; public Color Green; public NetrekColors() { Races = new Color[colors.length]; for (int i = 0; i < colors.length; i++) Races[i] = new Color(colors[i]); White = Color.white; Black = Color.black; Red = new Color(0xFFA500); Yellow = Color.yellow; Green = Color.green; } } class NetrekImageFrame { int width, height; byte[] bits; Image[] images; public NetrekImageFrame() { bits = null; images = new Image[6]; for (int i = 0; i < 6; i++) images[i] = null; } public NetrekImageFrame(int width, int height, byte[] bits) { this.width = width; this.height = height; this.bits = bits; images = new Image[6]; for (int i = 0; i < 6; i++) images[i] = null; } }