Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Whats wrong with this?

  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Whats wrong with this?

    Well no movement... but the keylistener is set up as far as i know.
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.*;
     
    public class BallRacer extends JFrame implements KeyListener{
        
        static int PlayerX;
        static int PlayerY;
        
       public BallRacer(String title) {
          super(title);
       }
        
        @Override
        public void keyTyped(KeyEvent e) {}
    
        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
                PlayerY--;
            }
            if(e.getKeyChar() == KeyEvent.VK_RIGHT){
                PlayerX++;
            
            }
            if(e.getKeyChar() == KeyEvent.VK_DOWN){
                PlayerY++;
            }
            if(e.getKeyChar() == KeyEvent.VK_LEFT){
                PlayerX--;
            
            }
            if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
                System.exit(0);
            
            }
        }
        
        @Override
        public void keyReleased(KeyEvent e) {}
       
        @Override
       public void paint(Graphics g) {
          super.paint(g);
          g.setColor(Color.blue);
          g.fillOval(PlayerX, PlayerY, 25, 25);
          g.setColor(Color.black);
          g.fill3DRect(0, 0, 800, 50, rootPaneCheckingEnabled);
          g.fill3DRect(0, 575, 800, 50, rootPaneCheckingEnabled);
          g.fill3DRect(0, 0, 50, 800, rootPaneCheckingEnabled);
          g.fill3DRect(750, 0, 50, 800, rootPaneCheckingEnabled);
        }
     
       private static void createAndShowGui() {
          JFrame MainFrame = new BallRacer("BallRacer v1");
          MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          MainFrame.pack();
          MainFrame.setVisible(true);
          MainFrame.setResizable(false);
          MainFrame.setSize(800,600);
          
       }
       public static void main(String[] args) {
           
                while (PlayerY < 50) {
                    PlayerY++;
                }
                while (PlayerY > 575) {
                    PlayerY--;
                }
                while (PlayerX < 50) {
                    PlayerX++;
                }
                while (PlayerX > 750) {
                    PlayerX--;
                }
          SwingUtilities.invokeLater(new Runnable() {
                @Override
             public void run() {
                createAndShowGui();
    
                }
             }
          );
       }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Whats wrong with this?

    Whats wrong with this?
    It has a very poor subject line.

    I don't see any code there that adds a KeyListener to a Component.

    Also, I fail to see the point of the loops in the main(...) method that increment the badly named variables.

    Moving to AWT/Swing

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Whats wrong with this?

    Quote Originally Posted by Alkor View Post
    Well no movement... but the keylistener is set up as far as i know.
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.*;
     
    public class BallRacer extends JFrame implements KeyListener{
        
        static int PlayerX;
        static int PlayerY;
        
       public BallRacer(String title) {
          super(title);
       }
        
        @Override
        public void keyTyped(KeyEvent e) {}
    
        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
                PlayerY--;
            }
            if(e.getKeyChar() == KeyEvent.VK_RIGHT){
                PlayerX++;
            
            }
            if(e.getKeyChar() == KeyEvent.VK_DOWN){
                PlayerY++;
            }
            if(e.getKeyChar() == KeyEvent.VK_LEFT){
                PlayerX--;
            
            }
            if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
                System.exit(0);
            
            }
        }
        
        @Override
        public void keyReleased(KeyEvent e) {}
       
        @Override
       public void paint(Graphics g) {
          super.paint(g);
          g.setColor(Color.blue);
          g.fillOval(PlayerX, PlayerY, 25, 25);
          g.setColor(Color.black);
          g.fill3DRect(0, 0, 800, 50, rootPaneCheckingEnabled);
          g.fill3DRect(0, 575, 800, 50, rootPaneCheckingEnabled);
          g.fill3DRect(0, 0, 50, 800, rootPaneCheckingEnabled);
          g.fill3DRect(750, 0, 50, 800, rootPaneCheckingEnabled);
        }
     
       private static void createAndShowGui() {
          JFrame MainFrame = new BallRacer("BallRacer v1");
          MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          MainFrame.pack();
          MainFrame.setVisible(true);
          MainFrame.setResizable(false);
          MainFrame.setSize(800,600);
          
       }
       public static void main(String[] args) {
           
                while (PlayerY < 50) {
                    PlayerY++;
                }
                while (PlayerY > 575) {
                    PlayerY--;
                }
                while (PlayerX < 50) {
                    PlayerX++;
                }
                while (PlayerX > 750) {
                    PlayerX--;
                }
          SwingUtilities.invokeLater(new Runnable() {
                @Override
             public void run() {
                createAndShowGui();
    
                }
             }
          );
       }
    }
    Given past posting history, QFP

  4. #4
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Whats wrong with this?

    Could it be the static integers if it was, the while playerx and playery wouldn't work because for some reason it needs to be static any suggestions?

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

    Default Re: Whats wrong with this?

    Quote Originally Posted by Alkor View Post
    Could it be the static integers if it was, the while playerx and playery wouldn't work because for some reason it needs to be static any suggestions?
    The variables shouldn't be static, but Darryl already told you one problem -- where do you add a KeyListener to anything? If you don't add a listener to a component then nothing gets listened to. I also recommend against using KeyListeners with Swing apps but instead using Key Bindings (check the tutorial on this).

  6. #6
    Borkpaladin is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Whats wrong with this?

    Add the keylistener with 'mainFrame.addKeyListener(this);' and make sure you set 'mainFrame.setFocusable(true);'

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

    Default Re: Whats wrong with this?

    Quote Originally Posted by Borkpaladin View Post
    Add the keylistener with 'mainFrame.addKeyListener(this);' and make sure you set 'mainFrame.setFocusable(true);'
    And what happens once the user presses a JButton or enters a JTextField and the JFrame itself loses focus? Why are you recommending this when the much better recommendation to use key bindings, which solves all of these problems, has already been made?

  8. #8
    Borkpaladin is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Whats wrong with this?

    Quote Originally Posted by Fubarable View Post
    And what happens once the user presses a JButton or enters a JTextField and the JFrame itself loses focus? Why are you recommending this when the much better recommendation to use key bindings, which solves all of these problems, has already been made?
    Sorry, bit inexperienced with java, just trying to help.

Similar Threads

  1. whats wrong with this code?
    By Petee in forum New To Java
    Replies: 8
    Last Post: 09-30-2011, 05:39 PM
  2. Whats wrong with my code
    By zit1343 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 02:06 AM
  3. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 02:55 PM
  4. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 11:56 PM
  5. Database help... whats wrong?
    By neosnokia in forum JDBC
    Replies: 4
    Last Post: 06-10-2009, 12:17 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
  •