Is Key Pressed statements
Hi,
I am making a game like pong. I made a Jframe for the start menu:
Code java:
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:
Code java:
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.
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)
Re: Is Key Pressed statements
Thanks. What about the background?
Re: Is Key Pressed statements
And, how do i find out all the different key codes? Is there some website I can look at?
Re: Is Key Pressed statements
@ serdar
Code :
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!
Re: Is Key Pressed statements
Quote:
find out all the different key codes
Read the API doc for KeyEvent. Go to this site and find KeyEvent
Java Platform SE 6
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
Code java:
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:
Code java:
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;
}
}