Game(ball breaker) developed using swings not running.
mayur khatri
Greenhorn
Joined: Aug 10, 2010
Posts: 10
posted
0
I all.I have started developing a ball breaker game using swings and i am posting the code for the same.
I am not able to move the user controlled base used for avoiding the ball from falling(controlling is to be done using the arrow keys i.e
left and right)
Please check if there is any error in calling the "paint" method and please suggest any changes if to be done.
Thanks in advance.
THE CODE :
-----------------------------------------------------------------------------------------------------
OuterFrame.java
//This is the outer frame in which the game panel is to be put
OuterFrame()
{
add(new Wall());
setSize(400,400);
setResizable(false);
setTitle("BALL BREAKER");
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new OuterFrame();
//This class is the Panel in which the game components will be put
Wall() {
addKeyListener(new BoardAdapter());
setBackground(Color.yellow);
setForeground(Color.BLACK);
setDoubleBuffered(true); // using a buffer to paint
baseboard = new BaseBoard();
timer = new Timer(5,this);
timer.start();
private class BoardAdapter extends KeyAdapter {
//If there is any key pressed/relased when the Wall(the panel) is
//displayed then such presses or releases are to be passed to the
//the BaseBoard class for handling such events.
@Override
public void keyPressed(KeyEvent e) {
baseboard.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
baseboard.keyReleased(e);
}
public class BaseBoard {
//This is the class for the Base BaseBoard will is given to user to preventing
//the ball from falling.
//User controls movement of this board using the arrow keys
ImageIcon ii;
Image image;
private String baseboardimg = "baseboard.jpeg";
private int x;
private int dx;
BaseBoard() {
ii = new ImageIcon(this.getClass().getResource(baseboardimg));
image = ii.getImage();
x = 200;
}
public void move() {
x += dx;
}
public int getX() {
return x;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT) {
dx = -20;
}
else if(key == KeyEvent.VK_RIGHT) {
dx = 20;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
The output which i am getting is just the ball and the baseboard painted inside the frame but there is no movement
when ever i press the keyboard arrow keys(left arrow key and right arrow key).