Results 1 to 8 of 8
  1. #1
    humanbeing is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    there is no movement
    What variable controls if there is movement? Is the value of that variable changed when you press on the keys?
    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.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Your panel has not the focus, so the listener does not respond.
    Try to request the focus in the constructor of OuterFrame, e.g.:
    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();
    	}
    Then you should override paintComponent instead of paint(dont forget to change the super.paint too)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    humanbeing is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default Regarding cross posting

    Hi dear moderator.
    Repetition of posts will not happen hence forth.
    Thanks. :)

  6. #6
    humanbeing is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

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

    :)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Yes, a number of us post on more than one site and it's very frustrating posting an answer only to find that that had already been given on another site hours earlier.

    It allows us to check what's been posted elsewhere before giving our own reply.

Similar Threads

  1. Game Help.working on it for days,.
    By xSkittlesx in forum Java Applets
    Replies: 12
    Last Post: 05-31-2010, 04:24 PM
  2. Swings
    By bsantosh in forum AWT / Swing
    Replies: 4
    Last Post: 08-31-2009, 03:10 PM
  3. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 04:01 AM
  4. awt and swings
    By masa in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2008, 08:09 AM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •