import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class BouncingBall1 extends JFrame implements ActionListener {
private JPanel paper;
private JButton btnGo, btnStop, btnFinish, btnLeft, btnRight, btnUp, btnDown;
private int x, y, xChange, yChange, diameter;
private int x1, y1;
private Timer timer1;
private JTextField txtScore;
private JLabel lblScore;
private int score;
private Random random;
public static void main(String[] args)
{
BouncingBall1 ball1 = new BouncingBall1();
ball1.setSize(550, 550);
ball1.setBackground(Color.GREEN);
ball1.setVisible(true);
}
public BouncingBall1(){
setLayout(new FlowLayout());
btnGo = new JButton("Start");
btnGo.setPreferredSize(new Dimension (70,40));
btnGo.setBackground(Color.gray);
btnStop = new JButton("Stop");
btnStop.setPreferredSize(new Dimension (70,40));
btnStop.setBackground(Color.gray);
btnFinish = new JButton("Finish");
btnFinish.setPreferredSize(new Dimension (70,40));
btnFinish.setBackground(Color.gray);
btnLeft = new JButton("Left");
btnLeft.setPreferredSize(new Dimension (70,40));
btnLeft.setBackground(Color.gray);
btnRight = new JButton("Right");
btnRight.setPreferredSize(new Dimension (70,40));
btnRight.setBackground(Color.gray);
btnUp = new JButton("Up");
btnUp.setPreferredSize(new Dimension (70,40));
btnUp.setBackground(Color.gray);
btnDown = new JButton("Down");
btnDown.setPreferredSize(new Dimension (70,40));
btnDown.setBackground(Color.gray);
lblScore = new JLabel("Score");
lblScore.setPreferredSize(new Dimension(50,40));
lblScore.setBackground(Color.gray);
txtScore = new JTextField(3);
txtScore.setFont(new Font("TimesRoman", Font.BOLD,16));
txtScore.setBackground(Color.white);
txtScore.setForeground(Color.blue);
paper = new JPanel();
paper.setPreferredSize(new Dimension(500,400));
paper.setBackground(Color.black);
add(btnGo);
add(lblScore);
add(txtScore);
add(btnStop);
add(btnFinish);
add(paper);
add(btnLeft);
add(btnRight);
add(btnUp);
add(btnDown);
btnGo.addActionListener(this);
btnStop.addActionListener(this);
btnFinish.addActionListener(this);
btnLeft.addActionListener(this);
btnRight.addActionListener(this);
btnUp.addActionListener(this);
btnDown.addActionListener(this);
random = new Random();
x=10; y=10; xChange = 10; yChange = 0; diameter = 10;
}
public void moveBall()
{
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.black);
myPen.fillOval(x, y, diameter, diameter);
x = x + xChange;
y = y + yChange;
if(x <= 0)
xChange = -xChange;
if(y <= 0)
yChange = -yChange;
if(x >= paper.getWidth())
xChange = -xChange;
if(y >= paper.getHeight())
yChange = -yChange;
myPen.setColor(Color.white);
myPen.fillOval(x, y, diameter, diameter);
if((x>=80 && x<=110) && (y==110))
removeRectangle();
}
public void drawRectangle()
{
int x1, y1, width, height, col1, col2, col3;
Graphics myPen1 = paper.getGraphics();
myPen1.fillRect(80, 100, paper.getWidth(), paper.getHeight());
col1 = random.nextInt(255);
col2 = random.nextInt(255);
col3 = random.nextInt(255);
myPen1.setColor(new Color(col1, col2, col3));
x1= 80;
y1= 100;
width = 30;
height = 10;
myPen1.drawRect(x1, y1, width, height);
}
public void removeRectangle()
{
Graphics myPen1 = paper.getGraphics();
myPen1.setColor(Color.black);
myPen1.fillRect(x1, y1, paper.getWidth(),paper.getHeight());
Score();
}
public void Score()
{
score = score+10;
txtScore.setText(score + "");
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==btnGo)
{
timer1 = new Timer(50,this);
timer1.start();
}
if(event.getSource()==timer1)
{
moveBall();
}
if(event.getSource()==btnStop)
{
timer1.stop();
}
if(event.getSource()==btnGo)
{
drawRectangle();
}
if(event.getSource()==btnFinish)
{
JOptionPane.showMessageDialog(null, "Thank You for Playing! You scored " +score++);
System.exit(0);
}
if(event.getSource()==btnLeft)
{
xChange = -10; yChange = 0;
}
if(event.getSource()==btnRight)
{
xChange = 10; yChange = 0;
}
if(event.getSource()==btnUp)
{
xChange = 0; yChange = -10;
}
if(event.getSource()==btnDown)
{
xChange = 0; yChange = 10;
}
moveBall();
}
}