/* * impression - * This is a paint program that implements an Impressionist * painting technique based on image sampling. * * Paul Haeberli - 1995 * paul@sgi.com * * This painting technique is covered by * U.S patent Number 5,182,548 * */ import java.awt.*; import java.lang.*; import java.net.*; import java.awt.image.*; import java.util.Random; import java.io.PrintStream; class MemoryImage implements ImageConsumer { boolean imgready; int imgxsize, imgysize; int imgpixels[][]; MemoryImage(Image picture) { int ticks; picture.getSource().startProduction(this); ticks = 5*60*1000; /* 5 minutes */ while(ticks>0) { try { Thread.currentThread().sleep(100); } catch (Exception e) { ; } if(this.imgready) break; ticks -= 100; } } public void setProperties(java.util.Hashtable dummy) { } public void setColorModel(ColorModel dummy) { } public void setHints(int dummy) { } public void imageComplete(int dummy) { imgready = true; } public void setDimensions(int x, int y) { imgxsize = x; imgysize = y; imgpixels = new int[y][x]; } public void setPixels(int x1, int y1, int w, int h, ColorModel model, byte pixels[], int off, int scansize) { int pix, x, y, x2, y2, sx, sy; x2 = x1+w; y2 = y1+h; sy = off; for(y=y1; y=memimg.imgxsize) x=memimg.imgxsize-1; if(y<0) y = 0; if(y>=memimg.imgysize) y=memimg.imgysize-1; return new Color(memimg.imgpixels[y][x]); } private void drawbrush(Graphics g, int lx, int ly, int cx, int cy, int nsteps, boolean demo) { int i, x, y, rx, ry, px, py, del; int m, nmarks; for(i=0; i0) { setbrushsize(brushsize-1); showbrushsize(); } } private void showtext(String word) { canvasG.setColor(text); canvasG.drawString(word,(wxsize-fm.stringWidth(word))/2, wysize/3); } private void clearscreen() { canvasG.setColor(canvas); canvasG.fillRect(0,0,dxsize,dysize); switch(paintmode) { case 1: showtext("Loading image . . . "); break; case 2: showtext("Paint Here to Reveal. . ."); break; } } public void update(Graphics g) { paint(g); } public boolean mouseDown(Event evt, int x, int y) { if(x>0 && x0 && y0 && xcysize+1 && y0 && x<57) { clearscreen(); updatescreen(myG); } else if(x>80 && x<161) smallerbrush(); else if(x>181 && x<259) biggerbrush(); else if(x>261 && x<291) nextbrush(); } return true; } public boolean mouseUp(Event evt, int x, int y) { dopaint = false; return true; } public boolean mouseDrag(Event evt, int x, int y) { if(dopaint) { if(lok) { if((lx == x) && (ly == y)) return true; drawbrush(canvasG,lx,ly,x,y,nstrokes[brushtype],false); updatescreen(myG); } else { lok = true; } lx = x; ly = y; } return true; } public void updatescreen(Graphics g) { g.drawImage(canvasimage,1,1,this); } private void updateall(Graphics g) { g.setColor(border); g.drawRect(0,0,cxsize-1,cysize-1); g.setColor(Color.white); g.fillRect(0,cysize,wxsize,controlheight); updatescreen(g); g.drawImage(menu,0,cysize+4,this); } private Color paramcolor(String name, Color defcolor) { String pval; Integer i; if((pval=getParameter(name)) != null) { i = Integer.valueOf(pval,16); return new Color(i.intValue()); } else { return defcolor; } } public void paint(Graphics g) { String sourcename; if(paintmode == 1) { border = paramcolor("bordercolor", new Color(190,190,190)); canvas = paramcolor("canvascolor", new Color(231,215,199)); text = paramcolor("textcolor", new Color(20,20,20)); sourcename = getParameter("source"); clearscreen(); menu = getImage(getCodeBase(),"menu.gif"); updateall(g); if(sourcename.startsWith("http://")) { try { picture = getImage(new URL(sourcename)); } catch (Exception e) { System.out.println("impression: bad URL spec"); } } else { picture = getImage(getDocumentBase(),sourcename); } memimg = new MemoryImage(picture); if(memimg.imgpixels == null) System.out.println("impression: error on image read!"); paintmode = 2; clearscreen(); updatescreen(g); } else updateall(g); showbrushsize(); showbrushtype(); } }