import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.image.*; import java.awt.Choice; import java.io.*; import java.net.URL; import com.sun.image.codec.jpeg.*; public class GT extends Applet implements MouseListener, ActionListener, Runnable{ int pildilaius=300; int pildikorgus=300; BufferedImage pilt=new BufferedImage(pildilaius,pildikorgus, BufferedImage.TYPE_INT_RGB); Graphics2D piltg=pilt.createGraphics(); TextField tf=new TextField(20); TextField tf2=new TextField(20); Choice valik=new Choice(); Choice kuju=new Choice(); TextField tf1=new TextField(10); Button salvesta=new Button("Salvesta"); Button tryki=new Button("Väljasta"); Button too=new Button("Too pilt"); BufferedImage pufer; int suurendus=2; TextField pikkus=new TextField(" 10 "); int ooteaeg=300; //ms boolean veel=true; boolean alustatud=false; int x=40; int a=0; int b=0; int a1=0; int b1=0; public GT(){ valik.add("Sinine"); valik.add("Punane"); valik.add("Kollane"); kuju.add("Ruut"); kuju.add("Täis ring"); kuju.add("Sõõrik"); kuju.add("Joon"); kuju.add("Lõika"); kuju.add("Kleebi"); add(tf1); add(tryki); add(salvesta); add(valik); add(kuju); add(salvesta); add(too); addMouseListener(this); add(pikkus); salvesta.addActionListener(this); tryki.addActionListener(this); too.addActionListener(this); tf1.addActionListener(this); new Thread(this).start(); } int ex(int px){ return px*suurendus; } int ey(int py){ return py*suurendus; } int px(int ex){ return ex/suurendus; } int py(int ey){ return ey/suurendus; } public void joonista(Graphics g){ g.drawOval(x, 90, 20, 20); } public void run(){ while(veel){ Graphics g=getGraphics(); if(g!=null){ g.setXORMode(Color.white); g.setColor(Color.red); joonista(g); } try{ Thread.sleep(ooteaeg); } catch(Exception e){e.printStackTrace();} if(g!=null){ joonista(g); x++; } } } public void actionPerformed(ActionEvent e){ try{ if(e.getSource()==tryki){ String joonistus=tf1.getText(); Image failistpilt=Toolkit.getDefaultToolkit().getImage(joonistus); MediaTracker md=new MediaTracker(this); md.addImage(failistpilt, 0); md.waitForAll(); piltg.drawImage(failistpilt, 0, 0, this); repaint(); } if(e.getSource()==salvesta){ FileOutputStream FO=new FileOutputStream("pilt1.jpeg"); JPEGCodec.createJPEGEncoder(FO).encode(pilt); FO.close(); } if(e.getSource()==too){ Image pildike=Toolkit.getDefaultToolkit().getImage("siil.jpg"); MediaTracker mod=new MediaTracker(this); mod.addImage(pildike, 100); mod.waitForAll(); piltg.drawImage(pildike, 10, 10, this); repaint(); } }catch(Exception viga){viga.printStackTrace();} } public void paint(Graphics g){ g.drawImage(pilt,0,0, pildilaius*suurendus, pildikorgus*suurendus, this); //g.drawImage(pildike, 30, 30, this); if(!alustatud){ new Thread(this).start(); alustatud=true; } } public void mousePressed(MouseEvent e){ int nr=Integer.parseInt(pikkus.getText().trim()); a=px(e.getX()); b=py(e.getY()); a1=e.getX(); b1=e.getY(); Color c=Color.green; String varv=valik.getSelectedItem(); if(varv.equals("Punane"))c=Color.red; if(varv.equals("Sinine"))c=Color.blue; if(varv.equals("Kollane"))c=Color.yellow; piltg.setColor(c); piltg.setColor(c); String ese=kuju.getSelectedItem(); if(ese.equals("Ruut"))piltg.drawRect(a-5, b-5, nr, nr); if(ese.equals("Täis ring"))piltg.fillOval(a-5, b-5, nr, nr); if(ese.equals("Sõõrik"))piltg.drawOval(a-5, b-5, nr, nr); if(ese.equals("Kleebi"))piltg.drawImage(pufer, a1, b1, this); repaint(); } public void mouseReleased(MouseEvent e){ String ese=kuju.getSelectedItem(); int laius=Math.abs(a - px(e.getX())); int korgus=Math.abs(b -py(e.getY())); if(ese.equals("Joon"))piltg.drawLine(a,b, px(e.getX()),py(e.getY())); if(ese.equals("Lõika")){ pufer= new BufferedImage(laius, korgus,BufferedImage.TYPE_INT_RGB); Graphics2D puferg=pufer.createGraphics(); puferg.drawImage(pilt, -a, -b, this); } repaint(); } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] argumendid){ Frame f=new Frame(); f.add(new GT()); f.setSize(200, 200); f.setVisible(true); } }