/**
* Sille kodut - Pinx!
**/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.awt.geom.Line2D;


public class Silxi_kodutoo 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;
        int joon2x1=150;
	int joon2y1=150;
	int joon2x2=200;
	int joon2y2=200;
	double realX,realY;
	Tee tee=new Tee();
	boolean suundx=false;
	boolean suundy=false;

	public Silxi_kodutoo(){
		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);
                g.drawLine(joon2x1,joon2y1,joon2x2,joon2y2);
	}
	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;}
				}
			}
		}
                	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(joon2x1,joon2y1,joon2x2,joon2y2,realX,realY));
			if(kaugus==0 || kaugus < 4){
				if(realX >= joon2x1 && realX<=joon2x2 && realY >= joon2y1 && realY <= joon2y2){
					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;
			}
		}
                	if(e.getKeyCode()==KeyEvent.VK_A){
			if(joon2x1>=0){
				joon2x1-=2; joon2x2-=2;
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_S){
			if(joon2x2<=Laius){
				joon2x1+=2; joon2x2+=2;
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_W){
			if(joon2y1>=0){
				joon2y1-=2; joon2y2-=2;
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_Z){
			if(joon2y2<=Korgus){
				joon2y1+=2; joon2y2+=2;
			}
		}
	}
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}
	public static void main(String[] args){
		Frame f=new Frame();
		f.add(new Silxi_kodutoo());
		f.setSize(300,300);
		f.setVisible(true);
	
	}
	class Tee extends Thread{
		public Tee(){
		}
		public void run(){
			while(true){
				repaint();
				try{Thread.sleep(30);}catch(Exception e){}
			}
		}
	};
}
