import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Jooned5 extends Applet implements MouseListener, MouseMotionListener, KeyListener{ Vector hoidla=new Vector(); int algx, algy; int otsx, otsy; static final int puutekaugus=10; int valitud=-1; //valitud joone number. Valimata=-1. Graphics gr; public Jooned5(){ addMouseListener(this); addMouseMotionListener(this); addKeyListener(this); } public void mousePressed(MouseEvent e){ algx=e.getX(); algy=e.getY(); valitud=leiaPunktiLahedaltJoon(algx, algy); otsx=algx; otsy=algy; gr=getGraphics(); if(valitud>=0){ gr.drawLine(algx, algy, otsx, otsy); } gr.setXORMode(Color.white); } public void mouseReleased(MouseEvent e){ if(valitud>=0){ Joon j=(Joon)hoidla.elementAt(valitud); int dx=e.getX()-algx; int dy=e.getY()-algy; hoidla.setElementAt(new Joon(j.x1+dx, j.y1+dy, j.x2+dx, j.y2+dy), valitud); } else { hoidla.addElement(new Joon(algx, algy, e.getX(), e.getY())); } repaint(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseDragged(MouseEvent e){ if(valitud>=0){ int dx=otsx-algx; int dy=otsy-algy; Joon j=(Joon)hoidla.elementAt(valitud); gr.drawLine(j.x1+dx, j.y1+dy, j.x2+dx, j.y2+dy); otsx=e.getX(); otsy=e.getY(); dx=otsx-algx; dy=otsy-algy; gr.drawLine(j.x1+dx, j.y1+dy, j.x2+dx, j.y2+dy); } else { gr.drawLine(algx, algy, otsx, otsy); otsx=e.getX(); otsy=e.getY(); gr.drawLine(algx, algy, otsx, otsy); } } public void mouseMoved(MouseEvent me){ } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_DELETE){ if(valitud>=0){ hoidla.removeElementAt(valitud); valitud=-1; repaint(); } } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void paint(Graphics g){ for(int i=0; i0)?px:-px;} double a=y2/(double)x2; double b=-1; double vastus= Math.abs((a*px+b*py)/Math.sqrt(a*a+b*b)); return vastus; } /** * Leitakse kaugus lõigust. Lõiku arvestatakse * sirgjoonena otspunktide vahel. */ static double punktiKaugusLoigust(int px, int py, int x1, int y1, int x2, int y2){ if(px>=Math.min(x1, x2) && px<=Math.max(x1, x2)){ return punktiKaugusJoonest(px, py, x1, y1, x2, y2); } return Math.min(punktideVahelineKaugus(px, py, x1, y1), punktideVahelineKaugus(px, py, x2, y2)); } static double punktideVahelineKaugus(int x1, int y1, int x2, int y2){ int dx=x2-x1; int dy=y2-y1; return Math.sqrt(dx*dx+dy*dy); } public static void main(String[] arg){ Frame f=new Frame(); f.add(new Jooned5()); f.setSize(300, 300); f.setVisible(true); } class Joon{ int x1, y1, x2, y2; Joon(int ux1, int uy1, int ux2, int uy2){ x1=ux1; y1=uy1; x2=ux2; y2=uy2; } } }