import java.awt.*; import java.awt.image.*; public class Tah04 extends Canvas{ int vx, vy, ux, uy, ax, ay, laius, korgus; int op=1; Image pilt, sahver; String lause=" "; Graphics piltg, sahverg; Tah04(int w, int h){ // resize(w, h); this.reshape(0, 0, w, h); laius=w; korgus=h; setBackground(Color.white); } public boolean mouseDown(Event e, int x, int y){ ux=x; uy=y; ax=x; ay=y; sahverg.drawImage(pilt, 0, 0, this); return super.mouseDown(e, x, y); } public boolean mouseDrag(Event e, int x, int y){ vx=ux; vy=uy; ux=x; uy=y; if(op==1){ piltg.drawLine(vx, vy, ux, uy);} if(op==2) {piltg.drawImage(sahver, 0, 0 , null); piltg.drawLine(ax, ay, ux, uy);} if(op==4){siseruutjuur();} repaint(); return super.mouseDrag(e, x, y); } public boolean keyDown(Event e, int key){ if(op==3){ if((char)e.key=='\177'){if(lause.length()>0) {lause=lause.substring(0, lause.length()-1);}} else{ lause=lause+String.valueOf((char)e.key);} piltg.drawImage(sahver, 0, 0, null); piltg.drawString(lause, ax, ay); repaint(); } return super.keyDown(e, key); } public void siseruutjuur(){ int dx, dy; dx=ux-ax; dy=uy-ay; piltg.drawImage(sahver, 0, 0, null); piltg.drawLine(ax, (int)(ay+dy*0.5),(int)(1+ax+0.05*dx), uy ); piltg.drawLine((int)(1+ax+0.05*dx), uy,(int)(3+ax+0.1*dx), ay ); piltg.drawLine((int)(3+ax+0.1*dx), ay ,(int)(ux-0.05*dx-1), ay ); piltg.drawLine((int)(ux-0.05*dx-1), ay, ux, (int)(ay+0.3*dy) ); } public void seaVarv(Color co){ piltg.setColor(co);} public void tagasi(){ piltg.drawImage(sahver, 0, 0, null); repaint();} public void joon(){ op=2;} public void sodi(){op=1;} public void kirjuta(){op=3; lause=" ";} public void ruutjuur(){op=4;} public void update(Graphics g){ // g.clipRect(Math.min(vx, ux)-1, Math.min(vy, uy)-1, Math.abs(vx-ux)+100, Math.abs(vy-uy)+100); // g.clipRect(0, 0, 50, 30); paint(g); } public void paint(Graphics g){ if (pilt==null){ pilt=createImage(size().width, size().height); piltg=pilt.getGraphics(); piltg.setColor(Color.black); sahver=createImage(size().width, size().height); sahverg=sahver.getGraphics(); } g.drawImage(pilt, 0, 0, null); } /* public void handleSinglePixel(int x, int y, int pixel, TextArea area) { int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; area.appendText(red+"\n"); // Deal with the pixel as necessary... }*/ public String pildistring() { int[] pixels = new int[laius*korgus]; char c[] = new char[laius*korgus]; int ik=0; String s=""; PixelGrabber pg = new PixelGrabber(pilt, 0, 0, laius, korgus, pixels, 0, laius); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return "x"; } if ((pg.status() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return "x"; } for (int j = 0; j < korgus; j++) { for (int i = 0; i < laius; i++) { // look at the pixel // int red = (pixel >> 16) & 0xff; if (((pixels[j * laius + i] >> 16) & 0xff)==0){c[ik++]='m';} else if (((pixels[j * laius + i] >> 16) & 0xff)==255){c[ik++]='v';} else {c[ik++]='k';} } } s=new String(c); return s; } public void stringipilt(String s){ Image abi; int[] pixels = new int[laius*korgus]; char c[]=new char[laius*korgus]; int i, p; c=s.toCharArray(); p=s.length(); for(i=0; i