import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.net.*; public class ImagePalette extends Panel { Image offimage, r, o, y, g, b, v; public Image brush; private boolean shift = false; private final int brushWidth = 30; private final int brushHeight = 30; Graphics offgraphics; Dimension offdimension; public ImagePalette(String r, String o, String y, String g, String b, String v){ try{ Toolkit tk; tk = Toolkit.getDefaultToolkit(); this.r = tk.getImage(new URL(r)); this.o = tk.getImage(new URL(o)); this.y = tk.getImage(new URL(y)); this.g = tk.getImage(new URL(g)); this.b = tk.getImage(new URL(b)); this.v = tk.getImage(new URL(v)); } catch (Exception e){ System.out.println(e); } } public void paint(Graphics grp) { Dimension d = size(); if ((offgraphics == null) || (offdimension.width != d.width) || (offdimension.height != d.height)) { setBackground(Color.black); setForeground(Color.black); brush = createImage(brushWidth, brushHeight); offimage = createImage(d.width, d.height); offgraphics = offimage.getGraphics(); offdimension = d; System.out.println("initialized Palette"); } // setBackground(Color.black); //setForeground(Color.black); offgraphics.setColor(Color.black); offgraphics.fillRect(0,0,d.width/2,d.height/4); offgraphics.setColor(Color.white); offgraphics.fillRect(d.width/2,0,d.width,d.height/4); offgraphics.drawImage(r, 0, d.height/4, d.width/2, d.height/4, this); offgraphics.drawImage(o, d.width/2, d.height/4, d.width/2, d.height/4, this); offgraphics.drawImage(y, 0, d.height/2, d.width/2, d.height/4, this); offgraphics.drawImage(g, d.width/2, d.height/2, d.width/2, d.height/4, this); offgraphics.drawImage(b, 0, 3*d.height/4, d.width/2, d.height/4, this); offgraphics.drawImage(v, d.width/2, 3*d.height/4, d.width/2, d.height/4, this); offgraphics.drawImage(brush, (d.width-brushWidth)/2, (d.height/4 - brushHeight)/2, brushWidth, brushHeight, this); grp.drawImage(offimage, 0, 0, this); } public void update(Graphics grp){ paint(grp); } public boolean keyDown(Event evt, int key) { if (key == 'a') { shift = true; } // System.out.println("I heard a "+key+" down. Looking for "+'a'); return true; } public boolean keyUp(Event evt, int key) { if (key == 'a') { shift = false; } System.out.println("I heard a "+key+" up. Looking for "+'a'); return true; } // public void keyPressed(KeyEvent ke) { //if (ke.getKeyCode() == ke.VK_SHIFT) // shift = true; //System.out.println("I heard a: "+ke.getKeyCode()+" shift is "+ke.VK_SHIFT); //return true; //} public boolean keyReleased(KeyEvent ke) { if (ke.getKeyCode() == ke.VK_SHIFT) shift = false; return true; } public boolean mouseUp(Event ev, int x, int y) { Image mixer; if ((0 < x ) && (x < offdimension.width) && (0 < y ) && (y < offdimension.height)) { int [] newcolorbuffer = new int[brushWidth * brushHeight]; PixelGrabber pg = new PixelGrabber(offimage, x-(brushWidth/2), y-(brushHeight/2), brushWidth, brushHeight, newcolorbuffer, 0, brushWidth); try{ pg.grabPixels(); } catch (Exception p){ System.out.println(p); } if (!shift) { brush = createImage(new MemoryImageSource(brushWidth, brushHeight, newcolorbuffer, 0, brushWidth)); } else { int [] currentcolorbuffer = new int[brushWidth * brushHeight]; int [] mixbuffer = new int[brushWidth * brushHeight]; PixelGrabber brushpg = new PixelGrabber(brush, 0, 0, brushWidth, brushHeight, currentcolorbuffer, 0, brushWidth); try{ brushpg.grabPixels(); } catch (Exception e) { System.out.println(e); } for (int i=0; i < brushWidth * brushHeight; i++) { // int [] p = new int[3]; //for(int j=0; j < 3; j++) { //p[j] = (((newcolorbuffer[i])>>(8*j)) & 0xff) + // (((currentcolorbuffer[i])>>(8*j)) & 0xff) / // 2; //} //mixbuffer[i] = 0xff000000 | (p[2]<<16) | (p[1]<<8) | p[0]; newcolorbuffer[i] = newcolorbuffer[i] & 0x00ffffff; newcolorbuffer[i] = newcolorbuffer[i] | 0x7f000000; } Image newbrush = createImage(brushWidth, brushHeight); Graphics newgrp = newbrush.getGraphics(); newgrp.drawImage(brush, 0, 0, this); newgrp.drawImage(createImage(new MemoryImageSource(brushWidth, brushHeight, newcolorbuffer, 0, brushWidth)), 0, 0, this); brush = newbrush; //brush = createImage(new MemoryImageSource(brushWidth, // brushHeight, // mixbuffer, // 0, // brushWidth)); } } repaint(); return true; } }