Meaningless Drivel is fun!
The moose likes Game Development and the fly likes Game(ball breaker) developed using swings not running. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Reply locked New topic

Game(ball breaker) developed using swings not running.

mayur khatri

Joined: Aug 10, 2010
Posts: 10
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.


package ballbreaker;

import javax.swing.JFrame;

public class OuterFrame extends JFrame{

//This is the outer frame in which the game panel is to be put
add(new Wall());
setTitle("BALL BREAKER");

public static void main(String[] args) {
new OuterFrame();




package ballbreaker;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import javax.swing.Timer;
import java.awt.Toolkit;

public class Wall extends JPanel implements ActionListener {

private Timer timer;
private BaseBoard baseboard;

//This class is the Panel in which the game components will be put
Wall() {
addKeyListener(new BoardAdapter());
setDoubleBuffered(true); // using a buffer to paint

baseboard = new BaseBoard();
timer = new Timer(5,this);


public void actionPerformed(ActionEvent e) {
try {
} catch(Exception ex) {

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(baseboard.getBaseBoard(),baseboard.getX(),350,40,15, Color.yellow, this);
g2d.fillOval(200,335, 10, 10);


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.

public void keyPressed(KeyEvent e) {

public void keyReleased(KeyEvent e) {



package ballbreaker;

import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;

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();

if(key == KeyEvent.VK_LEFT) {
dx = 0;
if(key == KeyEvent.VK_RIGHT) {
dx = 0;

public Image getBaseBoard() {
return image;


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).

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Do not duplicate threads. Continue there.

[My Blog]
All roads lead to JavaRanch
subject: Game(ball breaker) developed using swings not running.
It's not a secret anymore!