aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Game Development
Reply locked New topic
Author

Game(ball breaker) developed using swings not running.

mayur khatri
Greenhorn

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.

THE CODE :
-----------------------------------------------------------------------------------------------------
OuterFrame.java

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

}

}

-------------------------------------------------------------------------------
Wall.java

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());
setBackground(Color.yellow);
setForeground(Color.BLACK);
setDoubleBuffered(true); // using a buffer to paint

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

}

public void actionPerformed(ActionEvent e) {
baseboard.move();
repaint();
try {
Thread.sleep(20);
} catch(Exception ex) {
ex.printStackTrace();
}
}

@Override
public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(baseboard.getBaseBoard(),baseboard.getX(),350,40,15, Color.yellow, this);
g2d.setColor(Color.red);
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.

@Override
public void keyPressed(KeyEvent e) {
baseboard.keyPressed(e);
}

@Override
public void keyReleased(KeyEvent e) {
baseboard.keyReleased(e);
}

}
}

------------------------------------------------------------------------------------------
BaseBoard.java


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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Do not duplicate threads. Continue there.


[My Blog]
All roads lead to JavaRanch
 
wood burning stoves
 
subject: Game(ball breaker) developed using swings not running.
 
Similar Threads
Applet refuses to repaint
Please check if the paint method used properly
Please help with this program
Beginner game
KeyEvent Problem