import java.io.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.util.*; import java.lang.*; import java.awt.geom.Line2D; import java.lang.Math; public class kulgur extends Applet implements KeyListener{ double x,y,samm=10; Vector plangud = new Vector(); Plank pp; public kulgur()throws IOException{ TeePlangud(); addKeyListener(this); } public void paint(Graphics g){ JoonistaRuut(g); JoonistaPlangud(g); } /* Kontrollib puutumist plankudega */ public boolean Kontrolli(int suund, double xx,double yy){ boolean vas=true; double xplus=0,yplus=0; if(suund==KeyEvent.VK_LEFT){xplus=10;} if(suund==KeyEvent.VK_RIGHT){xplus=0;} if(suund==KeyEvent.VK_UP){yplus=10;} if(suund==KeyEvent.VK_DOWN){yplus=0;} for (Enumeration e = plangud.elements(); e.hasMoreElements();){ pp = (Plank)e.nextElement(); double kaugus = Math.round(Line2D.ptLineDist(pp.GetX1(),pp.GetY1(),pp.GetX2(),pp.GetY2(),xx+xplus,yy+yplus)); if (kaugus==0 || kaugus < 10){ if ((suund==KeyEvent.VK_LEFT || suund==KeyEvent.VK_RIGHT) && (yy>=pp.GetY1() && (yy+10) <= pp.GetY2())){ vas = false; break; } if ((suund==KeyEvent.VK_UP || suund==KeyEvent.VK_DOWN) && xx>=pp.GetX1() && (xx+10) <= pp.GetX2()){ vas = false; break; } } } return vas; } public void keyPressed(KeyEvent e){ int kood=e.getKeyCode(); if(kood==KeyEvent.VK_LEFT){ if (Kontrolli(kood,x-samm,y)){ x-=samm; } } if(kood==KeyEvent.VK_RIGHT){ if (Kontrolli(kood,x+samm,y)){ x+=samm; } } if(kood==KeyEvent.VK_UP){ if (Kontrolli(kood,x,y-samm)){ y-=samm; } } if(kood==KeyEvent.VK_DOWN){ if (Kontrolli(kood,x,y+samm)){ y+=samm; } } repaint(); } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /* Joonistab liikuva ruudu */ public void JoonistaRuut(Graphics g){ g.drawOval(PI(x),PI(y),10,10); g.setColor(Color.green); g.fillOval(PI(x)+1,PI(y)+1,9,9); } /* Loeb vektorist kõikide plankude koordinaadid ja joonistab need */ public void JoonistaPlangud(Graphics g){ for (Enumeration e = plangud.elements(); e.hasMoreElements();){ pp = (Plank)e.nextElement(); g.setColor(Color.black); g.drawLine((int)pp.GetX1(),(int)pp.GetY1(),(int)pp.GetX2(),(int)pp.GetY2()); } } /*Loeb failidest plankude andmed!*/ public void TeePlangud() throws IOException{ try{ BufferedReader sisse=new BufferedReader(new FileReader("plangud.txt")); String rida=sisse.readLine(); while(rida!=null){ StringTokenizer koordinaadid = new StringTokenizer(rida,":"); double x1=-1,x2=-1,y1=-1,y2=-1; int pos=0; while (koordinaadid.hasMoreTokens()) { pos++; switch (pos){ case 1: x1=Integer.parseInt(koordinaadid.nextToken()); break; case 2: y1=Integer.parseInt(koordinaadid.nextToken()); break; case 3: x2=Integer.parseInt(koordinaadid.nextToken()); break; case 4: y2=Integer.parseInt(koordinaadid.nextToken()); break; } if(x1!=-1&&x2!=-1&&y1!=-1&&y2!=-1){ plangud.addElement(new Plank(x1,x2,y1,y2)); Log(" > Tehti uus Plank(" + x1 + "," + x2 + "," + y1 + "," + y2 + ")"); } } rida=sisse.readLine(); } sisse.close(); }catch(Exception FailiViga){} } public int PI(double val){ return (int)val; } //* Kirjutab systemisse */ public void Log(String txt){ System.out.println(txt); } //* MAIN */ public static void main(String args[])throws IOException{ Frame FrameKulgur = new Frame("Kulgur"); FrameKulgur.add(new kulgur()); FrameKulgur.setResizable(false); FrameKulgur.setSize(600,500); FrameKulgur.setLocation(100,100); FrameKulgur.show(); FrameKulgur.addWindowListener(new Raamikuular()); } }; //* Et akent saaks sulgeda ka :) */ class Raamikuular extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.out.println("Raam suleti"); System.exit(0); } } //* Klass Plank, kus hoitakse plankude koordinaate*/ class Plank{ double x1,x2,y1,y2; public Plank(double x1,double x2,double y1,double y2){ this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; } double GetX1(){return this.x1;} double GetX2(){return this.x2;} double GetY1(){return this.y1;} double GetY2(){return this.y2;} };