import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class BreakOut extends JFrame implements ActionListener,MouseListener, MouseMotionListener {
private JPanel paper;
private JButton btnInsert£, btnGo;
private JTextField txtHowManyGoes;
private int x, y, xChange, yChange, diameter, batX, batY, ballX, ballY;
private int preX=0, preY=0 ;
private int counter;
private Boolean firstTime=true, brk1;
private JLabel lblDisplay;
private Timer timer1;
private Random RandNum;
public static void main(String[] args){
BreakOut breakOut = new BreakOut();
breakOut.setSize(500, 500);
breakOut.setBackground(Color.green);
breakOut.setVisible(true);
}
public BreakOut(){
setLayout(new FlowLayout());
btnInsert£ = new JButton("Insert £");
txtHowManyGoes = new JTextField(2);
add(btnInsert£); add(txtHowManyGoes);
btnGo = new JButton("Go!..");
btnGo.setEnabled(false);
add(btnGo);
btnInsert£.addActionListener(this);
btnGo.addActionListener(this);
lblDisplay = new JLabel("....X Y Coordinates......");
paper = new JPanel();
paper.setPreferredSize(new Dimension(400, 400));
paper.setBackground(Color.green);
add(paper);
add(lblDisplay);
addMouseListener(this);
addMouseMotionListener(this);
x =0; y =0; xChange = 2; yChange =7; diameter = 11;
counter=0;
timer1 =new Timer (50, this);
}
public void moveBall(){
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.GREEN);
myPen.fillOval(x, y, diameter, diameter);
x = x + xChange ;
y = y + yChange ;
if (x <= 0 || x >= paper.getWidth())
xChange = -xChange;
if (y <= 0 || y >= paper.getHeight()){
timer1.stop();
counter--;
txtHowManyGoes.setText(counter+"");
}
myPen.setColor(Color.blue);
myPen.fillOval(x, y, diameter, diameter);
ballX =x; ballY = y;
}
public void actionPerformed(ActionEvent event){
//moveBall();
if (event.getSource()==btnInsert£){
btnGo.setEnabled(true);
counter = counter + 5;
txtHowManyGoes.setText(counter +"");
}
if (event.getSource()==timer1){
moveBall();
}
if (event.getSource()==btnGo){
if (counter >0){
Random randNum; randNum = new Random();
x= randNum.nextInt((paper.getWidth()));
y=0;
timer1 = new Timer(10, this)
;timer1.start();
}
else
btnGo.setEnabled(false);
}
}
public void drawBat(){
int batY;
Graphics myPen = paper.getGraphics();
//paint(myPen); // stops flickering!
batY = paper.getHeight()-20; // bottom of the screen!
myPen.clearRect(0, 0, paper.getWidth(),
paper.getHeight());
// myPen.fillRect(batX-60,batY , 60, 10);
myPen.setColor(Color.BLACK);
batX = batX - 70;//co-ordinates of the batY position
batY = batY - 30;
//batY = paper.getHeight()-40;
myPen.fillRect(batX,batY , 70, 10); //adjusts bat size
preX = batX; preY = batY;
}
private void checkCollision(){
Graphics myPen = paper.getGraphics();
// ball hitting bat!
if ((ballY + ballX) >= 30 && ballY >= batX && ballY <= (batX + 60))
//if((ballX >= batX)&& (ballX <= batX +30 && (ballY >= batY )))
yChange = -5;
}
public void mouseMoved(MouseEvent event){
lblDisplay.setText("X is " +x + " and Y is " + y );
batX = event.getX();
//batY = event.getY();
drawBat();
checkCollision();
batX = batX - 70;
batY = batY - 40;
//batY = paper.getHeight()-40;
//getMousePosition();
}
public void drawBrks(){
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.BLUE);
if (brk1== true)
myPen.fillRect(80, 10, 40, 10);
}
public void mouseClicked(MouseEvent e){
/* x=1;y=1;
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.GREEN);
myPen.fillRect(0, 0, paper.getWidth(),
paper.getHeight());
timer1 = new Timer(10, this);
timer1.start();
}*/
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseDragged(MouseEvent e){
}
}// end of BouncingBall