import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.applet.Applet; import com.sun.image.codec.jpeg.*; import java.io.*; import java.awt.Choice; import java.net.URL; public class Graafika extends Applet implements MouseListener, ActionListener { int a=0; int b=0; int a1=0; int b1=0; BufferedImage pilt=new BufferedImage(600, 600, BufferedImage.TYPE_INT_RGB); BufferedImage pilt2=new BufferedImage(600, 600, BufferedImage.TYPE_INT_RGB); BufferedImage pilt3=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); Graphics2D g=pilt.createGraphics(); Choice valik=new Choice(); Choice valik2=new Choice(); Choice valik3=new Choice(); Choice valik4=new Choice(); Button salvesta=new Button("Salvesta"); Button tuhmista=new Button("Tuhmista pilt"); Button varjusta=new Button("Tekita vari"); Button asenda=new Button("Asenda värv"); Button sulge=new Button("Sulge aken"); Button ava=new Button("Ava fail"); TextField fail=new TextField(20); BufferedImage puhver; public Graafika(){ addMouseListener(this); Panel p=new Panel(new GridLayout(1, 2)); Panel pp=new Panel(new GridLayout(1, 2)); p.add(fail); p.add(ava); ava.addActionListener(this); valik.add("Sinine"); valik.add("Punane"); valik.add("Kollane"); p.add(valik); valik2.add("Ring"); valik2.add("Ristkülik"); valik2.add("Joon"); p.add(valik2); valik3.add("Seest tühi"); valik3.add("Seest täis"); p.add(valik3); valik4.add("Lõika"); valik4.add("Kleebi"); p.add(valik4); pp.add(tuhmista); tuhmista.addActionListener(this); pp.add(varjusta); varjusta.addActionListener(this); pp.add(asenda); asenda.addActionListener(this); pp.add(salvesta); salvesta.addActionListener(this); pp.add(sulge); sulge.addActionListener(this); setLayout(new BorderLayout()); add(p, BorderLayout.NORTH); add(pp, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e){ if(e.getSource()==salvesta){ try{ FileOutputStream FO=new FileOutputStream("Pilt.jpg"); JPEGCodec.createJPEGEncoder(FO).encode(pilt); FO.close(); }catch(Exception viga){} repaint(); }else if(e.getSource()==ava){ Image pilt=Toolkit.getDefaultToolkit().getImage(fail.getText()); MediaTracker md=new MediaTracker(this); md.addImage(pilt, 0); try{ md.waitForAll(); }catch(Exception viga){} g.drawImage(pilt, 0, 0, this); repaint(); }else if(e.getSource()==sulge){ try { Robot r=new Robot(); Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); int laius=d.width; int korgus=d.height; Image pilt=r.createScreenCapture(new Rectangle(0, 0, laius, korgus)); int punktid[]=new int[laius*korgus]; PixelGrabber pg=new PixelGrabber(pilt, 0, 0, laius, korgus, punktid, 0, laius); pg.grabPixels(); for(int y=0; y>16 & 0xFF; int roheline=p>>8 & 0xFF; int sinine=p & 0xFF; if(punane==0 && sinine==0 && roheline==0){ p=punktid[y*laius+x+1]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==0 && sinine==0 && roheline==0){ p=punktid[y*laius+x+2]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==192 && sinine==192 && roheline==192){ p=punktid[y*laius+x+3]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==192 && sinine==192 && roheline==192){ p=punktid[y*laius+x+4]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==192 && sinine==192 && roheline==192){ p=punktid[y*laius+x+5]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==192 && sinine==192 && roheline==192){ p=punktid[y*laius+x+6]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==0 && sinine==0 && roheline==0){ p=punktid[y*laius+x+7]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; if(punane==0 && sinine==0 && roheline==0){ p=punktid[y*laius+x+8]; punane=p>>16 & 0xFF; roheline=p>>8 & 0xFF; sinine=p & 0xFF; r.mouseMove(x, y); r.mousePress(InputEvent.BUTTON1_MASK ); r.mouseRelease(InputEvent.BUTTON1_MASK ); System.exit(0); }}}}}}}} } } }catch(Exception viga){} }else if(e.getSource()==tuhmista){ RescaleOp rop = new RescaleOp(0.1f, 200.0f, null); rop.filter(pilt,pilt2); g.drawImage(pilt, 25, 50, this); g.drawImage(pilt2, 125, 150, this); repaint(); }else if(e.getSource()==varjusta){ float andmed[]=new float[]{0.5f, 0, 0, 0, 0, 0, 0, 0, 0, 0.5f}; ConvolveOp co=new ConvolveOp(new Kernel(1, 10, andmed)); ConvolveOp c=new ConvolveOp(new Kernel(10, 1, andmed)); co.filter(pilt, pilt2); c.filter(pilt2, pilt3); g.drawImage(pilt2, 25, 50, this); // g.drawImage(pilt3, 225, 250, this); repaint(); }else if(e.getSource()==asenda){ float andmed[][]=new float[][]{ {1, 0, 0}, {0, 0, 0}, {0, 1, 1} }; BandCombineOp bco=new BandCombineOp( andmed, null ); bco.filter(pilt.getData(), pilt2.getRaster()); g.drawImage(pilt, 25, 50, this); g.drawImage(pilt2, 125, 150, this); repaint(); } } public void paint(Graphics g){ g.drawImage(pilt, 0, 0, this); // g.drawImage(pilt2, 0, 0, this); // g.drawImage(pilt3, 0, 0, this); } public void mousePressed(MouseEvent e){ Color c=Color.green; a= e.getX(); b= e.getY(); a1= e.getX(); b1= e.getY(); String varv=valik.getSelectedItem(); if(varv.equals("Sinine"))c=Color.blue; if(varv.equals("Punane"))c=Color.red; if(varv.equals("Kollane"))c=Color.yellow; g.setColor(c); String seest=valik3.getSelectedItem(); String kuju=valik2.getSelectedItem(); if(kuju.equals("Ring") && seest.equals("Seest tühi"))g.drawOval(e.getX()-10, e.getY()-10, 20, 20); if(kuju.equals("Ring") && seest.equals("Seest täis"))g.fillOval(e.getX()-10, e.getY()-10, 20, 20); if(kuju.equals("Ristkülik") && seest.equals("Seest tühi"))g.drawRect(e.getX()-10, e.getY()-10, 20, 20); if(kuju.equals("Ristkülik") && seest.equals("Seest täis"))g.fillRect(e.getX()-10, e.getY()-10, 20, 20); g.setColor(c); } public void mouseReleased(MouseEvent e){ int laius=Math.abs(a-e.getX()); int korgus=Math.abs(b-e.getY()); String seest=valik3.getSelectedItem(); String kuju=valik2.getSelectedItem(); String tegu=valik4.getSelectedItem(); if(kuju.equals("Joon") && seest.equals("Seest tühi"))g.drawLine(a, b, e.getX(), e.getY()); if(kuju.equals("Joon") && seest.equals("Seest täis"))g.drawLine(a, b, e.getX(), e.getY()); if(tegu.equals("Lõika")){ System.out.println(laius+ " "+ korgus); puhver=new BufferedImage(laius, korgus, BufferedImage.TYPE_INT_RGB); Graphics2D puhverg=puhver.createGraphics(); puhverg.drawImage(pilt, -a, -b, this); } if(tegu.equals("Kleebi")){ if (puhver!=null){ g.drawImage(puhver, a, b, this); repaint(); } System.out.println(laius+ " "+ korgus); g.drawImage(puhver, a1, b1, this); }repaint(); } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] arg){ Frame f=new Frame(); f.add(new Graafika()); f.setSize(600, 600); f.setVisible(true); } }