Results 1 to 8 of 8
Thread: Game using swings not working
- 10-05-2010, 07:29 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
Game using swings not working
Hi 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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.g etX(),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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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). :)
- 10-05-2010, 01:49 PM #2
What variable controls if there is movement? Is the value of that variable changed when you press on the keys?there is no movement
Try debugging your code by Adding some print out statements to show when the keys are pressed and when the controlling variables change and what the variables values are when the paint method is called.
For Swing components you should override the paintComponent method instead of the paint method.
- 10-05-2010, 01:50 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Your panel has not the focus, so the listener does not respond.
Try to request the focus in the constructor of OuterFrame, e.g.:
Then you should override paintComponent instead of paint(dont forget to change the super.paint too)Java Code:OuterFrame() { Wall wall = new Wall(); add(wall); setSize(400, 400); setResizable(false); setTitle("BALL BREAKER"); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); wall.requestFocus(); }
-
Cross-post: Please-check-if-paint-method
Please do not cross-post without informing all posts/cross-posts that you are doing this. I know that you are only trying to get a helpful solution, but please look at this from our side -- No one likes putting in effort to help someone solve a problem only to find out that it's already been solved elsewhere. So in effect by doing this you are asking a volunteer to waste his time. All we ask is that next time you provide links in all cross-posts to all cross-posts. Thanks for your cooperation.
- 10-06-2010, 08:31 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
Regarding cross posting
Hi dear moderator.
Repetition of posts will not happen hence forth.
Thanks. :)
- 10-06-2010, 08:36 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
Regarding cross posting
Hi.
I didn't know that i am not supposed to post in two different sites the same post.
Cause what i thought was if i didn't get any help from this site then surely i would get it from another.
So that's the reason i posted my question at two places.
But it wont happen since there seems to be some relation in these sites.
:)
- 10-06-2010, 01:59 PM #7
Posting on more than one site is OK if you tell everyone about the other sites by adding a link to the other sites in your post. Then everyone can see if your question has been answered somewhere else and not waste time answering a question that has already been answered.
- 10-06-2010, 03:04 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Game Help.working on it for days,.
By xSkittlesx in forum Java AppletsReplies: 12Last Post: 05-31-2010, 03:24 PM -
Swings
By bsantosh in forum AWT / SwingReplies: 4Last Post: 08-31-2009, 02:10 PM -
Gueesing Game Almost done, but not working correctly
By mbnumba6 in forum New To JavaReplies: 5Last Post: 03-18-2009, 03:01 AM -
awt and swings
By masa in forum AWT / SwingReplies: 2Last Post: 11-24-2008, 07:09 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks

Bookmarks