import java.awt.*; import java.awt.image.*; import java.net.*; public class Palette extends Panel { Image offimage, colors; Graphics offgraphics; Dimension offdimension; Scratchpad sp; public Palette(Scratchpad sp, String palettefilename){ this.sp = sp; repaint(); try{ Toolkit tk; tk = Toolkit.getDefaultToolkit(); colors = tk.getImage(new URL(palettefilename)); } catch (Exception e){ System.out.println(e); } } public void paint(Graphics g) { Dimension d = size(); if ((offgraphics == null) || (offdimension.width != d.width) || (offdimension.height != d.height)) { offimage = createImage(d.width, d.height); offgraphics = offimage.getGraphics(); offdimension = d; System.out.println("initialized Palette"); } offgraphics.drawImage(colors, 0, 0, d.width, d.height, this); g.drawImage(offimage, 0, 0, this); } public void update(Graphics g){ paint(g); } public boolean mouseUp(Event e, int x, int y) { // System.out.println("Mouse Up: "+x+" "+y); if ((0 < x ) && (x < offdimension.width) && (0 < y ) && (y < offdimension.height)) { int [] pixelbuffer = new int[1]; PixelGrabber pg = new PixelGrabber(offimage, x, y, 1, 1, pixelbuffer, 0, 1); try{ pg.grabPixels(); } catch (Exception p){ System.out.println(p); } sp.setColor(pixelbuffer[0]); } return true; } }