import java.awt.*; import java.applet.*; import java.awt.event.*; import java.math.*; import java.util.*; import java.awt.geom.Line2D; public class Kodune extends Applet implements KeyListener{ int R = 10; int x=250; int y=27; int wx=2*R; int wy=2*R; int Laius=290; int Korgus=275; int joonx1=100; int joony1=100; int joonx2=150; int joony2=150; double realX,realY; Tee tee=new Tee(); boolean suundx=false; boolean suundy=false; public Kodune(){ tee.start(); this.addKeyListener(this); } public void paint(Graphics g){ Ringike(g); Jooneke(g); Kontroll(); } public void Ringike(Graphics g){ g.drawOval(x,y,wx,wy); if(suundx){x+=3;}else{x-=3;} if(suundy){y+=2;}else{y-=2;} } public void Jooneke(Graphics g){ g.drawLine(joonx1,joony1,joonx2,joony2); } public void Kontroll(){ if(x<=0){suundx=true;} if(x>=(Laius-wx)){suundx=false;} if(y<=0){suundy=true;} if(y>=(Korgus-wy)){suundy=false;} for(double i=0;i<6.3;i+=0.1){ realX = x + Math.cos(i)*R; realY = y + Math.sin(i)*R; double kaugus = Math.round(Line2D.ptLineDist(joonx1,joony1,joonx2,joony2,realX,realY)); if(kaugus==0 || kaugus < 4){ if(realX >= joonx1 && realX<=joonx2 && realY >= joony1 && realY <= joony2){ if(suundx){suundx=false;}else{suundx=true;} if(suundy){suundy=false;}else{suundy=true;} } } } } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_LEFT){ if(joonx1>=0){ joonx1-=2; joonx2-=2; } } if(e.getKeyCode()==KeyEvent.VK_RIGHT){ if(joonx2<=Laius){ joonx1+=2; joonx2+=2; } } if(e.getKeyCode()==KeyEvent.VK_UP){ if(joony1>=0){ joony1-=2; joony2-=2; } } if(e.getKeyCode()==KeyEvent.VK_DOWN){ if(joony2<=Korgus){ joony1+=2; joony2+=2; } } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String[] args){ Frame f=new Frame(); f.add(new Kodune()); f.setSize(300,300); f.setVisible(true); f.addWindowListener(new Raamikuular()); } class Tee extends Thread{ public Tee(){ } public void run(){ while(true){ repaint(); try{Thread.sleep(30);}catch(Exception e){} } } }; } /* void kontroll() { // siin kontrollitaxe joone ja kasti põrkamist ja muudetaxe vastavalt suunda. if( ots1x[i] >= (getWidth()-8) || ots2x[i] >= (getWidth()-8)) {a[i] =-Math.abs(a[i]);} if( ots1y[i] >= (getHeight()-8) || ots2y[i] >= (getHeight()-8)) {b[i] =-Math.abs(b[i]);} if( ots1x[i] <= 8 || ots2x[i] <= 8) {a[i] =Math.abs(a[i]);} if( ots1y[i] <= 8 || ots2y[i] <= 8) {b[i] =Math.abs(b[i]);} if( kOts1x[i] >= (getWidth()-2) || kOts1x[i]+kOts2x[i] >= (getWidth()-2)) {d[i] =-Math.abs(d[i]);} if( kOts1y[i] >= (getHeight()-2) || kOts1y[i]+kOts2y[i] >= (getHeight()-2)) {e[i] =-Math.abs(e[i]);} if( kOts1x[i] <= 2 || kOts1x[i]+kOts2x[i] <= 2) {d[i] =Math.abs(d[i]);} if( kOts1y[i] <= 2 || kOts1y[i]+kOts2y[i] <= 2) {e[i] =Math.abs(e[i]);} } */