import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class myJPanel extends JPanel implements ActionListener, KeyListener
{
//-----------Attributes
JButton[] bricks1;
JButton[] bricks2;
JButton[] bricks3;
JButton ball;
JButton b1;
int xpos=100,x1=5,x2=5,x3=5;
int ypos=100,y1=15,y2=25,y3=35;
int delay=0;
int direction=1;
Timer tim=new Timer(delay,this);
JButton b2;
int x=0,y=550;
int xpos1=100;
int ypos1=100;
// ---------- Constructor
public myJPanel()
{
super();
setLayout(null);
setBackground(Color.black);
//---------add bricks to panel
bricks1 = new JButton[16];
for (int i=0; i<16; i++)
{
bricks1[i] = new JButton();
add(bricks1[i]);
bricks1[i].setBounds(x1,y1,50,15);
x1+=51;
}
bricks2 = new JButton[16];
for (int i=0; i<16; i++)
{
bricks2[i] = new JButton();
add(bricks2[i]);
bricks2[i].setBounds(x2,y2,50,15);
x2+=51;
}
bricks3 = new JButton[16];
for (int i=0; i<16; i++)
{
bricks3[i] = new JButton();
add(bricks3[i]);
bricks3[i].setBounds(x3,y3,50,15);
x3+=51;
}
//-------add start button to panel
b1 = new JButton("Start");
add(b1);
b1.setFont(new Font ("Helvetica", Font.PLAIN, 20));
b1.setHorizontalAlignment(JLabel.CENTER);
b1.setBounds(620,600,60,20);
b1.setBackground(Color.blue);
//-----------add ball to panel
ball = new JButton();
add(ball);
ImageIcon img= new ImageIcon("Images/ball.gif");
ball.setIcon(img);
ball.setBounds(xpos,ypos,20,20);
ball.setBorder(null);
ball.setContentAreaFilled(false);
// AddListeners to button b1 in panel p1
b1.addActionListener(this);
b2 = new JButton();
add(b2);
b2.setBackground(Color.cyan);
b2.setBounds(0,550,150,15);
b2.addKeyListener(this);
} // end constructor
// ---------- Methods
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
// if button b1 in game panel gm1 is clicked start game
if(obj ==b1)
{
tim.start();
}
if(obj==tim)
{
if(direction==1)
{
xpos=xpos+2;
ypos=ypos+2;
if(ypos+20>=this.getHeight())
{
direction=2;
}
else
if(xpos+20>=this.getWidth())
{
direction=4;
}
}else
if(direction==2)
{
xpos=xpos+2;
ypos=ypos-2;
if(xpos+20>=this.getWidth())
{
direction=3;
}
else
if(ypos<=0)
{
direction=1;
}
}else
if(direction==3)
{
xpos=xpos-2;
ypos=ypos-2;
if(ypos<=0)
{
direction=4;
}
else
if(xpos<=0)
{
direction=1;
}
}else
if(direction==4){
xpos=xpos-2;
ypos=ypos+2;
if(xpos<=0)
{
direction=1;
}
else
if(ypos+20>=this.getHeight())
{
direction=3;
}
}
for (int i=0; i<16; i++)
{
if(ball.getBounds().intersects(bricks3[i].getBounds()))
{
remove(bricks3[i]);
if(direction==1)
direction=2;
else
if(direction ==2)
direction=3;
else
if(direction==3)
direction=4;
}
}
ball.setLocation(xpos,ypos);
validate();
repaint();
for (int i=0; i<16; i++)
{
if(ball.getBounds().intersects(bricks3[i].getBounds()))
{
remove(bricks2[i]);
if(direction==1)
direction=2;
else
if(direction ==2)
direction=3;
else
if(direction==3)
direction=4;
}
}
ball.setLocation(xpos,ypos);
validate();
repaint();
for (int i=0; i<16; i++)
{
if(ball.getBounds().intersects(bricks3[i].getBounds()))
{
remove(bricks1[i]);
if(direction==1)
direction=2;
else
if(direction ==2)
direction=3;
else
if(direction==3)
direction=1;
}
}
ball.setLocation(xpos,ypos);
validate();
repaint();
}
//-----------------what happens when the ball hits the paddle
if(b2.getBounds().intersects(ball.getBounds()))
{
direction=2;
validate();
repaint();
}
} // end method ball and brick and paddle interaction
public void keyPressed(KeyEvent event)
{
int key= event.getKeyCode();
if(key==event.VK_LEFT){
x=x-30;
}else
if(key==event.VK_RIGHT)
{
x=x+30;
}
b2.setLocation(x,y);
}
public void keyReleased(KeyEvent event)
{
}
public void keyTyped(KeyEvent event)
{
}
} // end class