import java.awt.image.BufferedImage ;

import javax.swing.JFrame ;

import java.awt.Dimension ;

import java.awt.Graphics ;

import javax.swing.JPanel ;

import java.io.PrintStream ;

import java.awt.Graphics ;

import java.awt.event.KeyEvent ;

import java.awt.event.KeyListener ;

import java.awt.image.BufferedImage ;

import java.io.File ;

import java.io.IOException ;

import javax.imageio.ImageIO ;

import java.awt.Toolkit ;

import java.applet.* ;

import java.awt.* ;

import java.awt.image.* ;

import java.awt.event.* ;

import java.io.* ;

import java.net.* ;

import java.text.* ;

import java.util.* ;

import java.util.zip.* ;

public final class JGame extends JFrame

{

Grid grid ;

GameLoop gLoop ;

public static boolean p_up = false ;

public static boolean p_down = false ;

public static boolean p_left = false ;

public static boolean p_right = false ;

static Player player ;

static BufferedImage img ;

public JGame ( )

{

super ( "Eludë 0.1" ) ;

this . grid = new Grid ( ) ;

add ( this . grid ) ;

setDefaultCloseOperation ( 3 ) ;

this . gLoop = new GameLoop ( this ) ;

this . gLoop . start ( ) ;

}

public void Menu ( )

{

}

public static void main ( String [ ] args )

{

JGame fm = new JGame ( ) ;

fm. setVisible ( true ) ;

fm. setResizable ( false ) ;

fm. setExtendedState ( fm. MAXIMIZED_BOTH ) ;

fm. pack ( ) ;

//fm.show();

}

}

class Player

implements KeyListener

{

int x ;

int buildings ;

Grid grid ;

GameLoop GameLoop ;

int sleep = 4 ;

Dimension screenSize = Toolkit . getDefaultToolkit ( ) . getScreenSize ( ) ;

int y ;

int clevel = 1 ;

String level = "Level: " + this . clevel ;

String version = "Eludë 0.1" ;

String user = "Player.gif" ;

boolean p_up = false ;

boolean p_down = false ;

boolean p_left = false ;

boolean p_right = false ;

int loop = 400 ;

int startloop = 0 ;

int backgroundx = 50 ;

int backgroundy = 50 ;

int backgroundx2 = 50 ;

int backgroundy2 = 50 ;

int backgroundx3 = 50 ;

int backgroundy3 = 50 ;

int backgroundx4 = 50 ;

int backgroundy4 = 50 ;

int gamespeed = 5 ;

BufferedImage image ;

//Box 1!

static int followx = 400 ;

static int followy = 400 ;

static boolean morex = false ;

static boolean morey = false ;

int ydiff = this . y - followy ;

int xdiff = this . x - followx ;

int xp5 = x + 5 ;

int xm5 = x - 5 ;

int yp5 = y + 5 ;

int ym5 = y - 5 ;

int rangep = 400 ;

int rangen = - 400 ;

boolean boxagro = false ;

//Box 2!

static int followx2 = 500 ;

static int followy2 = 500 ;

static boolean morex2 = false ;

static boolean morey2 = false ;

int ydiff2 = this . y - followy2 ;

int xdiff2 = this . x - followx2 ;

int xp52 = x + 5 ;

int xm52 = x - 5 ;

int yp52 = y + 5 ;

int ym52 = y - 5 ;

int rangep2 = 400 ;

int rangen2 = - 400 ;

boolean boxagro2 = false ;

public Player ( int i, int a )

{

this . x = i ;

this . y = a ;

try

{

image = ImageIO. read ( Player. class . getResource ( this . user ) ) ;

}

catch ( IOException e )

{

System . out . println ( "Image Not In Root" ) ;

}

}

public void draw ( Graphics g )

{

int gamespeed = 6 ;

boolean boxagro = false ;

boolean boxagro2 = false ;

int Scorea = 0 ;

int Scoreb = 0 ;

int Scorec = 0 ;

int Scored = 1 ;

g. setColor ( Color . BLUE ) ;

g. fillRect ( followx, followy, 20 , 20 ) ;

g. setColor ( Color . BLACK ) ;

g. fillRect ( followx2, followy2, 20 , 20 ) ;

g. drawString ( "Player" , this . x - 2 , this . y - 5 ) ;

g. drawString ( "Score: " + Scorea + " " + Scoreb + " " + Scorec + " " + Scored, 10 , 10 ) ;

g. drawString ( this . level , 0 , screenSize. height - 25 ) ;

g. drawString ( this . version , screenSize. width - 90 , screenSize. height - 25 ) ;

//while ( startloop < loop)

//{

Random generator = new Random ( ) ;

backgroundx = generator. nextInt ( screenSize. width ) ;

// System.out.println("backgroundX: " + backgroundx);

backgroundy = generator. nextInt ( screenSize. height ) ;

backgroundx2 = generator. nextInt ( screenSize. width ) ;

//System.out.println("backgroundX2: " + backgroundx2);

backgroundy2 = generator. nextInt ( screenSize. height ) ;

backgroundx3 = generator. nextInt ( screenSize. width ) ;

//System.out.println("backgroundX3: " + backgroundx3);

backgroundy3 = generator. nextInt ( screenSize. height ) ;

backgroundx4 = generator. nextInt ( screenSize. width ) ;

backgroundy4 = generator. nextInt ( screenSize. height ) ;

//}

g. fillRect ( backgroundx,backgroundy, 5 , 5 ) ;

g. fillRect ( backgroundx2,backgroundy2, 5 , 5 ) ;

g. fillRect ( backgroundx3,backgroundy3, 5 , 5 ) ;

g. fillRect ( backgroundx4,backgroundy4, 5 , 5 ) ;

g. drawImage ( image, this . x , this . y , null ) ;

int ydiff = this . y - followy ;

int xdiff = this . x - followx ;

int ydiff2 = this . y - followy2 ;

int xdiff2 = this . x - followx2 ;

//System.out.println("Xdiff" + xdiff);

//System.out.println("Ydiff" + ydiff);

//Score engine (broken)

if ( Scored >= 10 )

{

Scored = 0 ;

Scorec ++;

}

if ( Scorec >= 10 )

{

Scorec = 0 ;

Scoreb ++;

}

if ( Scoreb >= 10 )

{

Scoreb = 0 ;

Scorea ++;

}

if ( ydiff < 300 || ( xdiff < 300 ) )

{

boxagro = true ;

Scored ++;

}

if ( ydiff > 300 || ( xdiff > 300 ) || ( xdiff < - 300 ) || ( ydiff < - 300 ) )

{

boxagro = false ;

//System.out.println(boxagro);

}

if ( boxagro == true )

{

if ( this . y < followy )

{

if ( followy > 0 )

{

followy --; followy --;

}

}

if ( this . y > followy )

{

if ( followy < screenSize. height - 30 )

{

followy ++; followy ++;

}

}

if ( this . x > followx )

{

if ( followx > 0 )

{

followx ++; followx ++;

}

}

if ( this . x < followx )

{

if ( followy < screenSize. width )

{

followx --; followx --;

}

}

}

//Box 2 Follow info

if ( ydiff2 > 150 || ( xdiff2 > 150 ) || ( xdiff2 < - 150 ) || ( ydiff2 < - 150 ) )

{

boxagro2 = false ;

System . out . println ( boxagro2 ) ;

}

if ( ydiff2 < 150 || ( xdiff2 < 150 ) )

{

boxagro2 = true ;

System . out . println ( boxagro2 ) ;

}

if ( boxagro2 == true )

{

if ( this . y < followy2 )

{

if ( followy2 > 0 )

{

followy2 --; followy2 --;

}

}

if ( this . y > followy2 )

{

if ( followy2 < screenSize. height - 30 )

{

followy2 ++; followy2 ++;

}

}

if ( this . x > followx2 )

{

if ( followx2 > 0 )

{

followx2 ++; followx2 ++;

}

}

if ( this . x < followx2 )

{

if ( followy2 < screenSize. width )

{

followx2 --; followx2 --;

}

}

}

//End of Follow

//Controls

if ( ( this . p_up == true ) && ( this . y > 25 ) )

{

//if (( ydiff > 5) || (ydiff2 > 5))

{

//System.out.println(ydiff);

this . y -= gamespeed ;

}

}

if ( ( this . p_down == true ) && ( this . y < screenSize. height - 55 ) )

{

//System.out.println(xdiff);

//if (( ydiff > 5) || (ydiff2 > 5))

{

this . y += gamespeed ;

}

}

if ( ( this . p_left == true ) && ( this . x > 10 ) )

{

//System.out.println(xdiff);

//if (( xdiff > 5) || (xdiff2 > 5))

{

this . x -= gamespeed ;

}

}

if ( ( this . p_right == true ) && ( this . x < screenSize. width - 35 ) )

{

//System.out.println(ydiff);

//if (( xdiff > 5) || (xdiff2 > 5))

{

this . x += gamespeed ;

}

}

}

public void keyPressed ( KeyEvent ke )

{

if ( ( ke. getKeyCode ( ) == 38 ) &&

( this . y > 15 ) )

{

//System.out.println("Y: " + this.y);

this . p_up = true ;

//System.out.println("FollowY" + followy);

}

if ( ( ke. getKeyCode ( ) == 40 ) &&

( this . y < screenSize. height - 45 ) )

{

//System.out.println("Y: " + this.y);

this . p_down = true ;

//System.out.println("FollowY" + followy);

}

if ( ( ke. getKeyCode ( ) == 37 ) &&

( this . x > 5 ) )

{

//System.out.println("X: " + this.x);

this . p_left = true ;

//System.out.println("FollowX" + followx);

}

if ( ( ke. getKeyCode ( ) == 39 ) &&

( this . x < screenSize. width - 35 ) )

{

//System.out.println("X: " + this.x);

this . p_right = true ;

//System.out.println("FollowX" + followx);

}

}

public void keyReleased ( KeyEvent ke )

{

if ( ke. getKeyCode ( ) == 38 )

{

this . p_up = false ;

}

if ( ke. getKeyCode ( ) == 40 )

{

this . p_down = false ;

}

if ( ke. getKeyCode ( ) == 37 )

{

this . p_left = false ;

}

if ( ke. getKeyCode ( ) == 39 )

{

this . p_right = false ;

}

}

public void keyTyped ( KeyEvent ke )

{

if ( ke. getKeyCode ( ) == 16 )

{

sleep = 0 ;

this . clevel += 1 ;

this . level = ( "Level: " + this . clevel ) ;

}

}

}

class Grid extends JPanel

{

Player player ;

public Grid ( )

{

setFocusable ( true ) ;

requestFocus ( ) ;

setBackground ( Color . WHITE ) ;

Dimension screenSize = Toolkit . getDefaultToolkit ( ) . getScreenSize ( ) ;

setBounds ( 0 , 0 ,screenSize. width , screenSize. height ) ;

setPreferredSize ( new Dimension ( screenSize. width , screenSize. height ) ) ;

this . player = new Player ( 16 , 16 ) ;

addKeyListener ( this . player ) ;

}

@Override

public void paintComponent ( Graphics g )

{

super . paintComponent ( g ) ;

this . player . draw ( g ) ;

}

}

class GameLoop extends Thread

{

//int gamespeed = 1;

JGame game ;

public GameLoop ( JGame g )

{

this . game = g ;

}

@Override

public void run ( )

{

Player player ;

Grid grid ;

int sleep = 4 ;

while ( true )

{

this . game . repaint ( ) ;

try

{

Thread . sleep ( sleep ) ;

}

catch ( Exception e )

{

System . out . println ( "Error @ Thread.Sleep" ) ;

}

}

}