import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class Pildiredaktor3 extends Applet { Joonistusala ala=new Joonistusala(); Teatehaldus th=new Teatehaldus(ala); public Pildiredaktor3(){ setLayout(new BorderLayout()); add(ala, BorderLayout.CENTER); ala.tyhjenda(); ala.paneVarv(Color.blue); ala.addMouseListener(th); } public static void main(String[] arg){ Frame f=new Frame("Pildiredaktor"); f.add(new Pildiredaktor3()); f.setSize(300, 300); f.setVisible(true); } class Teatehaldus implements MouseListener{ int algx, algy; Joonistusala jAla; public Teatehaldus(Joonistusala uusAla){ jAla=uusAla; } public void mousePressed(MouseEvent e){ algx=e.getX(); algy=e.getY(); } public void mouseReleased(MouseEvent e){ int loppx=e.getX(); int loppy=e.getY(); jAla.joon(algx, algy, loppx, loppy); } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} } class Joonistusala extends Panel{ BufferedImage pilt=new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB); Graphics2D piltg=pilt.createGraphics(); public void paint(Graphics g){ g.drawImage(pilt, 0, 0, this); } public void tyhjenda(){ Color abivarv=piltg.getColor(); piltg.setColor(Color.white); piltg.fillRect(0, 0, 300, 300); piltg.setColor(abivarv); repaint(); } public void paneVarv(Color varv){ piltg.setColor(varv); } public void joon(int x1, int y1, int x2, int y2){ piltg.drawLine(x1, y1, x2, y2); repaint(); } } }