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 tennis extends Applet implements Runnable,MouseMotionListener{ int lives=3; double x=20,y=20,samm=10; int Ooteaeg = 30; int alusx=10; boolean veel=false; Vector plangud = new Vector(); Plank pp; final int mUL = 1,mUR=2,mDR=3,mDL=4; int dir=mDR; final int modePikali = 1, modePysti = 2; public tennis()throws IOException{ Plank alus = new Plank(10,110,470,470,2); plangud.addElement(alus); addMouseMotionListener(this); TeePlangud(); start(); } public void start(){ veel=true; new Thread(this).start(); } public void stop(){} public void mouseMoved(MouseEvent e){ int xs = e.getX(); pp = (Plank)plangud.get(0); pp.setX1(xs); pp.setX2(xs+100); } public void mouseDragged(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void run(){ while(veel){ if (IsDead()){ lives--; x=20; y=20; } switch(dir){ case mUL: switch((int)Kontrolli(dir,x-samm,y-samm)){ case 0: x-=samm; y-=samm; break; case modePikali: dir=mDL; break; case modePysti: dir=mUR; break; } break; case mUR: switch((int)Kontrolli(dir,x+samm,y-samm)){ case 0: x+=samm; y-=samm; break; case modePikali: dir=mDR; break; case modePysti: dir=mUL; break; } break; case mDR: switch((int)Kontrolli(dir,x+samm,y+samm)){ case 0: x+=samm; y+=samm; break; case modePikali: dir=mUR; break; case modePysti: dir=mDL; break; } break; case mDL: switch((int)Kontrolli(dir,x-samm,y+samm)){ case 0: x-=samm; y+=samm; break; case modePikali: dir=mUL; break; case modePysti: dir=mDR; break; } break; } repaint(); try{Thread.sleep(Ooteaeg);}catch(Exception viga){}; } } public void paint(Graphics g){ g.setColor(Color.red); g.fillRect(10,490,480,30); g.setColor(Color.blue); g.drawString("Elud : " + lives , 230,550); JoonistaRuut(g); JoonistaPlangud(g); } public boolean IsDead(){ boolean dead=false; if (x>=10 && x<=490 && y>=500){ dead=true; }else{ dead=false; } return dead; } /* Kontrollib puutumist plankudega */ public int Kontrolli(int suund, double xx,double yy){ int vas=0; double xplus=0,yplus=0; if(suund==mUL || suund==mDL){xplus=10;} if(suund==mUR || suund==mDR){xplus=0;} if(suund==mUL || suund==mUR){yplus=10;} if(suund==mDL || suund==mDR){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 ((pp.GetMode()==modePikali) && (yy>=pp.GetY1() && (yy+10) <= pp.GetY2())){ vas = modePysti; break; } if ((pp.GetMode()==modePysti) && (xx>=pp.GetX1() && (xx+10) <= pp.GetX2())){ vas = modePikali; break; } } } return vas; } 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 mode=-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; case 5: mode=Integer.parseInt(koordinaadid.nextToken()); break; } if(x1!=-1&&x2!=-1&&y1!=-1&&y2!=-1&&mode!=-1){ plangud.addElement(new Plank(x1,x2,y1,y2,mode)); 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 FrameTennis = new Frame("Tennis"); FrameTennis.add(new tennis()); FrameTennis.setResizable(false); FrameTennis.setSize(500,600); FrameTennis.setLocation(100,100); FrameTennis.show(); FrameTennis.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; int mode; public Plank(double x1,double x2,double y1,double y2,int mode){ this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; this.mode=mode; } double GetX1(){return this.x1;} double GetX2(){return this.x2;} double GetY1(){return this.y1;} double GetY2(){return this.y2;} int GetMode(){return this.mode;} void setX1(double x1){this.x1=x1;} void setX2(double x2){this.x2=x2;} void setY1(double y1){this.y1=y1;} void setY2(double y2){this.y2=y2;} };