import java.awt.*;
import java.awt.event.*;
public class bowlingarray extends Frame{

private static final long serialVersionUID = 1L;
private int ballx = 250, bally = 380;//position of ball
private int ball1x = 150, ball1y = 30;
private int ball2x = 190, ball2y = 30;
private int ball3x = 230, ball3y = 30;
private int ball4x = 270, ball4y = 30;
private int ball5x = 170, ball5y = 70;
private int ball6x = 210, ball6y = 70;
private int ball7x = 250, ball7y = 70;
private int ball8x = 190, ball8y = 110;
private int ball9x = 230, ball9y = 110;
private int ball10x = 210, ball10y = 150;
private int x1=100, y1=1;
private int x2=350, y2=1;//position of paddle
 //define ball's direction,then we can know where it will go
int right=5,left= -5,up=5,down= -5,score=0;
int width, height; // Width and height of the ball
boolean game, gameOver;
//画图。
Image offScreenImage = null;
private Thread th;
public boolean live = true;
public void paint(Graphics g) {
Color c=g.getColor();
g.setColor(Color.red);
g.fillOval(ballx, bally, 30, 30);//draw pong
       g.fillOval(ball1x, ball1y, 30, 30);
g.fillOval(ball2x, ball2y, 30, 30);
g.fillOval(ball3x, ball3y, 30, 30);
g.fillOval(ball4x, ball4y, 30, 30);
g.fillOval(ball5x, ball5y, 30, 30);
g.fillOval(ball6x, ball6y, 30, 30);
g.fillOval(ball7x, ball7y, 30, 30);
g.fillOval(ball8x, ball8y, 30, 30);
g.fillOval(ball9x, ball9y, 30, 30);
       g.fillOval(ball10x, ball10y, 30, 30);
       if(live=false)
         {
        remove(this);
        }

        if (210 < ballx&&ballx < 240)
  {
   if (150<bally && bally<180)
      {
   live=false;
         score++;
//  g.setColor(Color.magenta);
//  g.fillOval(ball10x, ball10y, 30, 30);
 }
    }
g.fillRect(x1,y1, 20, 400);
g.fillRect(x2,y2, 20,400);//draw paddles

g.drawString("score: "+score, 20, 40);
}

//not stable,so use update.
public void update(Graphics g) {
if(offScreenImage == null){
   offScreenImage = this.createImage(500, 400);

}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.blue);
gOffScreen.fillRect(0, 0, 500, 400);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage,0,0,null);

}
public void launchgame(){
this.setLocation(400, 300);//frame position
this.setSize(500, 400);    //frame size
this.setBackground(Color.blue);//color

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});                            //close the window

this.setResizable(false);//不能改变大小。
this.setTitle("pong");//标题栏文字。
setVisible(true);
        th = new Thread(new PaintThread());
th.start();          //method:start the thread.
}

    public static void main (String[] args) {
    bowlingarray w=new bowlingarray();
    w.launchgame();

    public void newposition (int nx, int ny)
{
ballx= nx; 
bally= ny; 
this.width=this.getWidth();
this.height=this.getHeight();
       repaint();
 }
    private class PaintThread implements Runnable{
     public void run() {
     game = true;
     boolean LR=false;
     boolean UD=false;
    
     while(true){//true,使他不断的循环  
     if(game){
                // The ball moves from left to right
                        if (LR) {
                   ballx += right;
        if (ballx == 330)//ball的位置不是中心的位置,是左边点的位置
                              LR= false;
     }
     else
     {    ballx += left;
     if ( ballx == 120)
                                   LR =  true;   //越界后则lr是true返回执行if么,则位置就向右加了。所以又往右弹回呗。
     }
                // The ball moves from up to down
                        if (UD){
         UD= false;
     }
     else
     {   bally += down;
     if ( bally <= 0)
         UD =  true;
     }
                        newposition(ballx, bally);
               
     try 
     {
       Thread.sleep(50);//每隔50ms就会,调用改方法时,试图捕获异常。
     }
     catch(InterruptedException ex)
     {
     }
    
     if (150 < ballx&&ballx < 180)
             {
                                  if (30<bally && bally<60)
     {    score++;
     }
             }
     if (190 < ballx&&ballx < 220)
     {
                                  if (30<bally && bally<60)
     {
     score++;
     }
     }
     if (230 < ballx&&ballx < 260)
     { 
                                  if (30<bally && bally<60)
     {
     score++;
     }
     }
     if (270 < ballx&&ballx < 300)
     {
                                  if (30<bally && bally<60)
     {
     score++;
     }
     }
     if (170 < ballx&&ballx < 200)
     {
                                 if (70<bally && bally<100)
     {
     score++;
     }
     }
     if (210 < ballx&&ballx < 240)
     {
                                if (70<bally && bally<100)
     {
     score++;
     }
     }
     if (250 < ballx&&ballx < 280)
     {if (70<bally && bally<100)
     {
     score++;
     }
     }
     if (190 < ballx&&ballx < 220)
     {if (110<bally && bally<140)
     {
     score++;
     }
     }
     if (270 < ballx&&ballx < 300)
     {if (110<bally && bally<140)
     {
     score++;
     }
     }
     if (210 < ballx&&ballx < 240)
     {
     if (150<bally && bally<180)
     {
     score++;
     }
     }     
     if(score==10)
                                  {
         game=false;
         gameOver=true;
     }
    
     // The ball stroke with the paddle1
     if(ballx==x1+10 && bally>=y1 && bally<=(y1+50))
        LR=true;
    
                        // The ball stroke with the paddle2
     if(ballx==(x2-5) && bally>=y2 && bally<=(y2+50))
        LR=false;
     }
     }
        } }

public void remove() {
// TODO Auto-generated method stub

}}