import java.awt.*;

import java.awt.event.*;

import java.util.Random;


import javax.swing.*;


import java.util.*;


public class Game extends Frame {


private JButton restart;

public JFrame replay;


  boolean isExploding = false;

  final int Ball_Size=20;

  final int Frame_WIDTH = 700;

  final int Frame_HEIGHT= 600;

  final int TOP; 

  final int BOTTOM; 

  final int LEFT; 

  final int RIGHT;

  public int x;   

  public int y;  

  public int b_x;    

  public int b_y;

  public int mouseX;

  public int mouseY;

  public int mouseX1;

  public int mouseY1;

  public int score =0;

  public int explosionFrameNumber; 

  public Random randomize = new Random();

  boolean isPlaying =true;  // 프레임 실행

  ArrayList<Ball> balls= new ArrayList();



      Game(String title){

   

 super(title);

        x=Frame_WIDTH/2 -Ball_Size/2;      

        y=Frame_HEIGHT/2 -Ball_Size/2;

        setBackground(Color.BLACK);

        setBounds(300,100, Frame_WIDTH, Frame_HEIGHT);

        setResizable(false);

        setVisible(true);

        Insets insets = getInsets();

 

        TOP = insets.top;

        LEFT=insets.left;

        BOTTOM= Frame_HEIGHT - insets.bottom;

        RIGHT = Frame_WIDTH - insets.right;

         registerEventHandler();  

        

      }

     //Frame

  

     private void moving(MouseEvent evt ){

      

      mouseX1 = evt.getX();

      mouseY1 = evt.getY();

      mouseX=mouseX1-10;

      mouseY=mouseY1-10;

      repaint();

      

       }

     //moving

     

     void registerEventHandler() {

       addKeyListener(new KeyAdapter(){

      public void keyPressed(KeyEvent e){}

       

       });                  

       addWindowListener(new WindowAdapter(){

             public void windowClosing(WindowEvent e){

           System.exit(0);

             }

         

          });// WindowListene

  

            addMouseMotionListener(new MouseMotionAdapter(){

              

             public void mouseMoved(MouseEvent evt) {

     if(!isPlaying) return;

     mouseX1 = evt.getX();

         mouseY1 = evt.getY();

         mouseX=mouseX1-10;

         mouseY=mouseY1-10;

        }

 

             public void mouseDragged(MouseEvent evt) {

 if(!isPlaying) return;

 mouseX1 = evt.getX();

     mouseY1 = evt.getY();

     mouseX=mouseX1-10;

     mouseY=mouseY1-10;

           

              }

          }); //MouseMotionListener


       }

     //registerEventHandler

     

     void start(){              //빨간공 팅기기및 생성 

  

      new BallGenerator().start();

    

      while(isPlaying){

          int size = balls.size();

          for(int i=0;i <size; i++) {

            Ball b =(Ball)balls.get(i);

            b.x+=b.xStep;

            b.y+=b.yStep;

              

               if(b.x <=LEFT) {

                 b.x = LEFT;

                 b.xStep = -b.xStep;

                 }       

     

               if(b.x >= RIGHT-b.size) {

                 b.x = RIGHT-b.size;

                 b.xStep = -b.xStep;

                  }

     

               if(b.y <= TOP) {

                 b.y = TOP;

                 b.yStep = -b.yStep;

                  }

     

                if(b.y >= BOTTOM-b.size) {

                 b.y = BOTTOM-b.size;

                 b.yStep = -b.yStep;

                  }

             

                if(collisionCheck(b)) { // 충돌하면 실행을 멈춘다.

                 isPlaying = false;

                 isExploding =true;

                 break;

                  }

               }//for 

       repaint();

       try{Thread.sleep(10);}

       catch(Exception e){}

        }//while end

     } 

     //start() end

 

       boolean collisionCheck(Ball b){

        

    if(mouseX>=RIGHT-20){

            double dX = (b.x + b.size/2) - (mouseX-20 + Ball_Size/2);

            double dY = (b.y + b.size/2) - (mouseY + Ball_Size/2);

            int distance = (int)(Math.sqrt(dX*dX+dY*dY));

            return distance < (b.size + Ball_Size)/2;

            }

         

    else if(mouseY>=BOTTOM-20){

            double dX = (b.x + b.size/2) - (mouseX + Ball_Size/2);

            double dY = (b.y + b.size/2) - (mouseY-20 + Ball_Size/2);

            int distance = (int)(Math.sqrt(dX*dX+dY*dY));

            return distance < (b.size + Ball_Size)/2;

            }

      

    else{

            double dX = (b.x + b.size/2) - (mouseX + Ball_Size/2);

            double dY = (b.y + b.size/2) - (mouseY + Ball_Size/2);

            int distance = (int)(Math.sqrt(dX*dX+dY*dY));

            return distance < (b.size + Ball_Size)/2;

             }

  

     } 

     //collisionCheck(Ball b)    

       public void paint(Graphics g){

   

    g.setColor(Color.YELLOW);

    if(mouseX>=RIGHT-20){

            g.fillOval(mouseX-20, mouseY, Ball_Size, Ball_Size);

      }

     

    else if(mouseY>=BOTTOM-20){

    g.fillOval(mouseX, mouseY-20, Ball_Size, Ball_Size);

           }

     

    else if(mouseY>=BOTTOM-20&&mouseX>=RIGHT-20){

            g.fillOval(mouseX-20, mouseY-20, Ball_Size, Ball_Size);

           }

   

    else{

    g.fillOval(mouseX, mouseY, Ball_Size, Ball_Size);

           }

     

        

    g.setColor(Color.RED); 

         g.drawString("Number of balls:"+balls.size(),20,50);

         int size =balls.size();

         

         g.setColor(Color.CYAN);

    for(int i=0; i<size; i++){

    Ball b = (Ball)balls.get(i);

    g.fillOval(b.x, b.y, b.size, b.size);

   

   

    if (isExploding) {

  explosionFrameNumber++;

   

            if (explosionFrameNumber == 15) { 

             isExploding = false;

             g.clearRect(0,0,700,600);

               }//if(explosionFrameNumber)

        

            g.setColor(Color.ORANGE);

           

            g.fillOval(b.x - 10*explosionFrameNumber,

                       b.y - 5*explosionFrameNumber,

                       20*explosionFrameNumber,

                       10*explosionFrameNumber);

     

            g.fillOval(mouseX - 20*explosionFrameNumber,

                       mouseY - 10*explosionFrameNumber,

                       40*explosionFrameNumber,

                       20*explosionFrameNumber);

     

          

            g.setColor(Color.RED);

            g.fillOval(mouseX - 10*explosionFrameNumber,

                       mouseY -5*explosionFrameNumber/2,

                       20*explosionFrameNumber,

                       5*explosionFrameNumber);

            repaint();

            try{Thread.sleep(50);}

            catch(Exception e){} 

   

          } //if(isExploding) 

   

    if(collisionCheck(b)){

           g.clearRect(0,0,700,600);

           g.setColor(Color.WHITE);

      g.setFont(new Font("굴림", Font.PLAIN, 100));

           g.drawString("Score:"+balls.size(),160,200);

           g.setColor(Color.BLACK);

          

     }//collisionCheck(b)

   

      } //for

  }

     //paint(g) 

   

     

 class BallGenerator extends Thread{

 


      public void run(){

        while(isPlaying){

           int x =(int)(Math.random()*(RIGHT-LEFT-Ball.MAX_SIZE))+LEFT;

           int y =(int)(Math.random()*(BOTTOM-TOP-Ball.MAX_SIZE))+TOP;

           balls.add(new Ball(x,y));

           try{Thread.sleep(1000);}

           catch(Exception e){} 

        }//while(isPlaying)

      }// void run

    }

   //class BallGenerator


 class Ball{


      int x =100;

      int y =100;

      int size =30;

      static final int MAX_SIZE =30;

      static final int MIN_SIZE =10;

      final int SPEED =(int)(Math.random()*3+(-3));

      final int SPEED1 =(int)(Math.random()*3+(-3));

      int xStep =SPEED;

      int yStep =SPEED1;


      Ball(int x, int y){

         this(x,y,(int) (Math.random()*(MAX_SIZE-MIN_SIZE))+MIN_SIZE);

           } 

      

      Ball(int x,int y, int size){

          if(x!= mouseX){

       this.x= x;

       this.size=size;

           }

          if(x!= mouseY){

       this.y =y;

            this.size=size;

           }

         }

     }

    // class Ball

   }//class mainsystem


 class Play{

     public static void main(String[] args){

        new Game("Balls").start();

          }

     } 

     

        자바의정석뒤에있는 공피하는예제 바꿔보고있는데

      공이랑충돌한뒤에 엔터키누르면 재시작이 되도록하고싶은데 어떻게해야할지 감이안옴