import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; abstract class Drawable { public abstract void draw(DrawableRaster r); } class line extends Drawable { float x0, y0, x1, y1; int color; public line(float x0, float y0, float x1, float y1, int color) { this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1; this.color = color; } public void draw(DrawableRaster r) { r.line(x0, y0, x1, y1, color); } } class ellipse extends Drawable { float x0, y0; float rx, ry; int color; public ellipse(float x0, float y0, float rx, float ry, int color) { this.x0 = x0; this.y0 = y0; this.rx = rx; this.ry = ry; this.color = color; } public void draw(DrawableRaster r) { r.ellipse(x0, y0, rx, ry, color); } } class fill extends Drawable { int x, y; int color; public fill(int x, int y, int color) { this.x = x; this.y = y; this.color = color; } public void draw(DrawableRaster r) { r.fill(x, y, color); } } public class Project2 extends Applet { final static int LISTSIZE = 100; DrawableRaster raster; Image screen; Drawable displayList[]; int elements; public void init( ) { raster = new DrawableRaster(size().width, size().height); raster.fill(getBackground()); screen = raster.toImage(this); displayList = new Drawable[100]; elements = 0; String filename = getParameter("datafile"); showStatus("Reading "+filename); InputStream is = null; try { is = new URL(getDocumentBase(), filename).openStream(); ReadInput(is); is.close(); } catch (IOException e) { showStatus("Error reading "+filename); } } private double getNumber(StreamTokenizer st) throws IOException { if (st.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException(st.toString()); return st.nval; } private void growDisplayList() { Drawable newList[] = new Drawable[2*displayList.length]; System.arraycopy(displayList, 0, newList, 0, displayList.length); displayList = newList; } public void ReadInput(InputStream is) throws IOException { StreamTokenizer st = new StreamTokenizer(is); st.eolIsSignificant(true); st.commentChar('#'); scan: while (true) { switch (st.nextToken()) { default: break scan; case StreamTokenizer.TT_EOL: break; case StreamTokenizer.TT_WORD: if ("l".equals(st.sval)) { float x0 = (float) getNumber(st); float y0 = (float) getNumber(st); float x1 = (float) getNumber(st); float y1 = (float) getNumber(st); int color = (int) getNumber(st); if (elements == displayList.length) growDisplayList(); displayList[elements++] = new line(x0, y0, x1, y1, color); } else if ("e".equals(st.sval)) { float x0 = (float) getNumber(st); float y0 = (float) getNumber(st); float x1 = (float) getNumber(st); float y1 = (float) getNumber(st); int color = (int) getNumber(st); if (elements == displayList.length) growDisplayList(); displayList[elements++] = new ellipse(x0, y0, x1, y1, color); } else if ("f".equals(st.sval)) { int x = (int) getNumber(st); int y = (int) getNumber(st); int color = (int) getNumber(st); if (elements == displayList.length) growDisplayList(); displayList[elements++] = new fill(x, y, color); } } } is.close(); if (st.ttype != StreamTokenizer.TT_EOF) throw new IOException(st.toString()); } public void paint(Graphics g) { g.drawImage(screen, 0, 0, this); } public void update(Graphics g) { paint(g); } public boolean mouseDown(Event e, int x, int y) { if (e.metaDown()) { raster.fill(getBackground()); } else { showStatus("Drawing "+elements+" elements ..."); raster.fill(getBackground()); long time = System.currentTimeMillis(); for (int i = 0; i < elements; i++) displayList[i].draw(raster); time = System.currentTimeMillis() - time; showStatus("Time = "+time+" ms"); } screen = raster.toImage(this); repaint(); return true; } }