import java.awt.*; import java.awt.image.*; public class Tah06 extends Canvas{ int vx, vy, ux, uy, ax, ay, px, py, laius, korgus; int op=1; Image pilt, sahver, puhver; Image summa, integraal; String lause=" "; Graphics piltg, sahverg, puhverg; Color maluvarv=Color.black; boolean margitud=false, pilt_olemas=false; Tah06(int w, int h){ // resize(w, h); this.reshape(0, 0, w, h); laius=w; korgus=h; setBackground(Color.white); } public boolean imageUpdate( Image image, int f, int x0, int y0, int xe, int ye){ if ((f & ImageObserver.ALLBITS) != 0){pilt_olemas=true; System.out.println("pilt juures"); } System.out.println("Pildivaatleja"); return true;} public boolean mouseUp(Event e, int x, int y){ if(op==13){px=px+x-ax; py=py+y-ay;} return true; } public boolean mouseDown(Event e, int x, int y){ ux=x; uy=y; ax=x; ay=y; if(op==13); else if(op!=10)sahverg.drawImage(pilt, 0, 0, this); else piltg.drawImage(sahver, 0, 0, this); if(op==1){piltg.drawLine(ax, ay, ux, uy); repaint();} if(op==5){piltg.fillRect(ux-6, uy-6, 12, 12); repaint();} 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<30){ 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();} if(op==5){piltg.fillRect(ux-6, uy-6, 12, 12);} if(op==6){hv6(e, op, x, y);} if((op>9)&&(op<20))hv10(e, op, x, y); else if(op>19)hv20(e, op, x, y); } else if(op<50) hv3040(e, op, x, y); else hv50(e, op, x, y); repaint(); //System.out.println(op); return super.mouseDrag(e, x, y); } void hv6(Event e, int opkood, int x, int y){ int x1=vx, x2=x, y1=vy, y2=y, vahe, abi, rida, veerg, dv, xv, yv; //if(x1>x2){abi=x1; x1=x2; x2=abi;} //if(y1>y2){abi=y1; y1=y2; y2=abi;} xv=x2-x1; yv=y2-y1; try{ if(Math.abs(x2-x1)>Math.abs(y2-y1)){if(xv>0)dv=1; else dv=-1; for(veerg=x1; (dv>0)? veerg<=x2 : veerg>=x2 ; veerg+=dv) { piltg.fillRect(veerg-3, y1+(int)((float)(veerg-x1)*((float)yv/(float)xv)-3), 6, 6); } } else {if(yv>0)dv=1; else dv=-1; for(rida=y1; (dv>0)? rida<=y2 : rida>=y2 ; rida+=dv) { piltg.fillRect( x1+(int)((float)(rida-y1)*((float)xv/(float)yv)-3),rida-3, 6, 6); } // System.out.println("rida="+rida); } }catch(Exception ex){System.out.println(ex.toString());} } void hv10(Event e, int opkood, int x, int y){ int x1=ax, x2=x, y1=ay, y2=y, abi; piltg.drawImage(sahver, 0, 0, null); if(x1>x2){abi=x1; x1=x2; x2=abi;} if(y1>y2){abi=y1; y1=y2; y2=abi;} if(opkood==10){piltg.drawRect(x1, y1, x2-x1, y2-y1); margitud=true;} if(opkood==13){piltg.drawImage(puhver, px+x-ax, py+y-ay, null);} } void hv20(Event e, int opkood, int x, int y){ double pikkus=Math.sqrt(Math.pow(x-ax, 2)+Math.pow(y-ay, 2)); double siinus=(y-ay)/pikkus; double koosinus=(x-ax)/pikkus; piltg.drawImage(sahver, 0, 0, null); if(opkood==20)piltg.drawLine(ax, ay, x, y); if(opkood==21){ for(double k=5; kMath.abs(y-ay)) if(x-ax>0)x2=ax+Math.abs(y-ay); else x2=ax-Math.abs(y-ay); else if(y-ay>0)y2=ay+Math.abs(x-ax); else y2=ay-Math.abs(x-ax);} if(x1>x2){abi=x1; x1=x2; x2=abi;} if(y1>y2){abi=y1; y1=y2; y2=abi;} if(opkood==30)piltg.drawRect(x1, y1, x2-x1, y2-y1); if(opkood==40)piltg.fillRect(x1, y1, x2-x1, y2-y1); if(opkood==31)piltg.drawOval(x1, y1, x2-x1, y2-y1); if(opkood==41)piltg.fillOval(x1, y1, x2-x1, y2-y1); if(opkood==32)piltg.drawRoundRect(x1, y1, x2-x1, y2-y1, (x2-x1)/4, (y2-y1)/2); if(opkood==42)piltg.fillRoundRect(x1, y1, x2-x1, y2-y1, (x2-x1)/4, (y2-y1)/2); } void hv50(Event e, int opkood, int x, int y){ int dx, dy; dx=Math.abs(ux-ax); dy=Math.abs(uy-ay); //dx=0; dy=55; ImageObserver ob; int x1=ax, x2=x, y1=ay, y2=y, abi; if(x1>x2){abi=x1; x1=x2; x2=abi;} if(y1>y2){abi=y1; y1=y2; y2=abi;} piltg.drawImage(sahver, 0, 0, null); if(opkood==50){ 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) ); } if(opkood==51){pilt_olemas=false; piltg.drawImage(integraal, x, y, 15, 20 , this); } if(opkood==52){pilt_olemas=false;piltg.drawImage(summa, x, y, 15, 15, this);} } 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); maluvarv=co;} public void tagasi(){ piltg.drawImage(sahver, 0, 0, null); repaint();} public void seaopkood(int kood){ if((kood==11)&margitud)loika(); else if((kood==12)&margitud)koopia();else if(op!=10)sahverg.drawImage(pilt, 0, 0, this); else {piltg.drawImage(sahver, 0, 0, this);} if((kood==10)&(piltg.getColor()!=Color.blue)){maluvarv=piltg.getColor(); piltg.setColor(Color.blue);} if(kood!=10){piltg.setColor(maluvarv); margitud=false;} if(kood==13)kleebi(); op=kood; if(op==3)lause=" "; System.out.println(maluvarv.toString()+" "+piltg.getColor().toString()); } public void tuhjaks(){ Color co; sahverg.drawImage(pilt, 0, 0, null); co=piltg.getColor(); piltg.setColor(Color.white); piltg.fillRect(0, 0, laius, korgus); piltg.setColor(co); repaint(); } public void panepilt(Image im){ sahverg.drawImage(pilt, 0, 0, null); piltg.drawImage(im, 0, 0, null); repaint(); } public void koopia(){ int x1=ax, x2=ux, y1=ay, y2=uy, abi; if(x1>x2){abi=x1; x1=x2; x2=abi;} if(y1>y2){abi=y1; y1=y2; y2=abi;} piltg.drawImage(sahver, 0, 0, this); puhver=createImage(x2-x1, y2-y1); puhverg=puhver.getGraphics(); puhverg.drawImage(sahver, -x1, -y1, null); } public void loika(){ int x1=ax, x2=ux, y1=ay, y2=uy, abi; if(x1>x2){abi=x1; x1=x2; x2=abi;} if(y1>y2){abi=y1; y1=y2; y2=abi;} piltg.drawImage(sahver, 0, 0, this); puhver=createImage(x2-x1, y2-y1); puhverg=puhver.getGraphics(); puhverg.drawImage(sahver, -x1, -y1, null); Color co=piltg.getColor(); piltg.setColor(Color.white); piltg.fillRect(x1, y1, x2-x1, y2-y1); piltg.setColor(co); repaint(); } public void kleebi(){ if(puhver!=null) piltg.drawImage(puhver, 0, 0, null); px=0; py=0; repaint(); } 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 synchronized void joonista_string(String s, int x, int y){ Color co=piltg.getColor(); piltg.setColor(Color.black); sahverg.drawImage(pilt, 0, 0, null); piltg.drawString(s, x, y); piltg.setColor(co); repaint(); } 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 String kokku(String s){ char tahed1[]; char tahed2[]=new char[laius*korgus]; char c; int i, koht1, koht2, arv; String abi; tahed1=s.toCharArray(); koht1=0; koht2=0; while(koht1s.length()-1)break; } abi=String.valueOf(arv); for (int ik=0; ik> 16) & 0xff; if (((pixels[j * laius + i] >> 16) & 0xff)==0){c[ik++]='m';} else if ((((pixels[j * laius + i] >> 16) & 0xff)==255)&& (((pixels[j * laius + i] >> 8) & 0xff)==255)){c[ik++]='v';} else if ((((pixels[j * laius + i] >> 16) & 0xff)==255)&& (((pixels[j * laius + i] >> 8) & 0xff)==0)){c[ik++]='p';} else {c[ik++]='k';} } } s=new String(c); return kokku(s); } public String pildistring(Image pilt) { 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)&& (((pixels[j * laius + i] >> 8) & 0xff)==255)){c[ik++]='v';} else if ((((pixels[j * laius + i] >> 16) & 0xff)==255)&& (((pixels[j * laius + i] >> 8) & 0xff)==0)){c[ik++]='p';} else {c[ik++]='k';} } } s=new String(c); return kokku(s); } public void stringipilt(String s){ Image abi; int[] pixels = new int[laius*korgus]; char c[]=new char[laius*korgus]; int i, p; s=lahti(s); c=s.toCharArray(); p=s.length(); for(i=0; i