Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Is Key Pressed statements

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Is Key Pressed statements

    Hi,

    I am making a game like pong. I made a Jframe for the start menu:

     
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
      class ImageTest {
     
      public static void main(String[] args) {
        ImagePanel panel = new ImagePanel(new ImageIcon("pongintropage.png").getImage());
     
        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
      }
    }
     
    class ImagePanel extends JPanel {
     
      private Image img;
     
      public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
      }
     
      public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
      }
     
      public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
      }
    }


    I tried to do an is key pressed statement to change backgrounds:

    if(isKeyPressed(KeyEvent.VK_SPACE)
     
    {
           setBackground(newBackground);
    }


    Is set background the right command or will I have to do the whole Jframe thing shown up above again in the if statement?
    Also, is SPACE the right command for spacebar?

    What I am trying to do is get the background to change when the key spacebar is pressed.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Is Key Pressed statements

    each key has different Key Code for example space is 32
    you should add KeyListener to your component
    and the you can check which key is pressed like
    if(event.getKeyCode()==32)

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Is Key Pressed statements

    Thanks. What about the background?

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Is Key Pressed statements

    And, how do i find out all the different key codes? Is there some website I can look at?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Is Key Pressed statements

    @ serdar
    if(event.getKeyCode()==32)
    That code can be made to be self documentating. When I read 32 it has no meaning.
    What is the variable that defines the value for that key press? Use that instead!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Is Key Pressed statements

    find out all the different key codes
    Read the API doc for KeyEvent. Go to this site and find KeyEvent
    Java Platform SE 6

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Is Key Pressed statements

    to change background image you should have

    *array with image names on ImageTest class
    *setImg() method on ImagePanel class
    * I'm using ImageHelper.class to load Images
    *counter to go to next index of array
    *repaint() method calls paintComponent() method
    frame.addKeyListener(new KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            if(evt.getKeyCode()==KeyEvent.VK_SPACE)
                    panel.setImg(ImageHelper.loadImage(array[counter]).getImage());
                    panel.repaint();
                    counter++;
                }
        });
    ImageHelper.class:
     
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class ImageHelper {
     
        private ImageHelper() {
        }
     
        public static ImageIcon loadImage(String name) {
            ImageIcon image = null;
            try {
                URL url = ImageHelper.class.getResource(name);
                if (url != null) {
                    java.awt.Image img = Toolkit.getDefaultToolkit().createImage(url);
                    if (img != null) {
                        image = new ImageIcon(img);
                    }
                }
            } catch (Throwable ex) {
                System.out.println("ERROR: loading image " + name + " failed");
            }
            return image;
        }
    }

Similar Threads

  1. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  2. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  3. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM
  4. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM