import java.awt.*; import java.net.*; import java.util.Random; public class Scratchpad extends Panel { int lastx, lasty; public Image offimage; Graphics offgraphics; Dimension offdimension; Color c; public int paintmode; Random r = new Random(); int offsetx, offsety; public void paint(Graphics g) { Dimension d = size(); int dim; if (offgraphics == null) { if (d.width > d.height) dim = round(d.height); else dim = round(d.width); Color bg = getBackground(); setBackground(new Color(0x0f0f0f)); offimage = createImage(dim, dim); setBackground(bg); offgraphics = offimage.getGraphics(); offdimension = d; System.out.println("initialized Scratchpad"); offgraphics.setColor(c); } else if ((offdimension.width != d.width) || (offdimension.height != d.height)) { System.out.println("resized graphics"); offdimension = d; if (d.width > d.height) dim = round(d.height); else dim = round(d.width); Image resizedimage = createImage(dim, dim); offgraphics = resizedimage.getGraphics(); offgraphics.drawImage(imageResize(offimage, dim), 0, 0, this); offimage = resizedimage; offgraphics.setColor(c); } offsetx = (d.width-offimage.getWidth(this))/2; offsety = (d.height-offimage.getHeight(this))/2; g.drawImage(offimage, offsetx, offsety, this); // System.out.println(d.width+"x"+d.height); // System.out.println(offimage.getWidth(this)+"x"+offimage.getHeight(this)); } public void update(Graphics g){ paint(g); } public void setColor(int in){ System.out.println("I think the color is: "+in); c = new Color(in); offgraphics.setColor(c); } private void drawpoint(int x, int y) { if (paintmode == 0) offgraphics.drawLine(lastx, lasty, x, y); else drawchars(x,y); repaint(); } private void drawchars(int x, int y) { char c = (char)('a' + (r.nextInt() % 26)); offgraphics.drawString(""+c, x, y); } public boolean mouseDown(Event e, int x, int y) { lastx = x - offsetx; lasty = y - offsety; drawpoint(x - offsetx, y - offsety); return true; } public boolean mouseDrag(Event e, int x, int y) { drawpoint(x - offsetx, y - offsety); lastx = x - offsetx; lasty = y - offsety; return true; } public void loadImage(URL cb, String newImagename) { int dim = offimage.getHeight(this); Color bg = getBackground(); setBackground(new Color(0x0f0f0f)); offgraphics.clearRect(0,0,dim,dim); setBackground(bg); Image i=null; try{ Toolkit tk; tk = Toolkit.getDefaultToolkit(); try{ i = tk.getImage(new URL(newImagename)); System.out.print ("Loading image: "+newImagename+"..."); } catch (Exception e) { System.out.println(e); System.out.print("Trying alternate: "+ cb.toString()+newImagename+ "..."); i = tk.getImage(new URL(cb,newImagename)); } } catch (Exception e){ System.out.println(e); } MediaTracker mt = new MediaTracker(this); mt.addImage(i, 0); try{ mt.waitForID(0); } catch (Exception e){ System.out.println(e); } System.out.println("Done."); i = imageResize(i, dim); offgraphics.drawImage(i, (dim-i.getWidth(this))/2, (dim-i.getHeight(this))/2, this); repaint(); } private int round (int unround) { int i = 1; while ((unround>>=1) > 0) { i <<= 1; } return i; } private Image imageResize(Image i, int dim) { int w = i.getWidth(this); int h = i.getHeight(this); if (w > h) { h = -1; w = dim; } else { w = -1; h = dim; } i = i.getScaledInstance(w,h,i.SCALE_SMOOTH); System.out.println("resized to "+w+" x "+h+" on image "+dim+" x "+dim); MediaTracker mt = new MediaTracker(this); mt.addImage(i, 7); try{ mt.waitForID(7); } catch (Exception e){ System.out.println(e); } return i; } }