Results 1 to 7 of 7
- 05-19-2011, 02:11 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Why isnt my program detecting any key presses?
public class Tester
{
// instance variables - replace the example below with your own
public static void main ( String [ ] args )
{
JFrame myFrame;
MainBoard myMainSwitchBoard = new MainBoard ( );
myFrame = new JFrame ( "BomberMan" );
myFrame.getContentPane( ).add ( myMainSwitchBoard );
myFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
myFrame.setSize ( 1000, 735);
myFrame.setResizable ( false );
myFrame.setVisible ( true );
}
}
public class MainBoard extends JPanel
{
// instance variables - replace the example below with your own
private Image myBackground;
private Image myRight;
private Image myLeft;
private Timer myTimer;
private int intMyRandom;
/**
* Constructor for objects of class MainBoard
*/
public MainBoard()
{
// initialise instance variables
myBackground = new ImageIcon ( "Background.jpg" ).getImage( );
myRight = new ImageIcon ( "Right.jpg" ).getImage( );
myLeft = new ImageIcon ( "Left.jpg" ).getImage( );
myTimer = new Timer ( true );
myTimer.scheduleAtFixedRate( new TimerTask( )
{
public void run()
{
repaint( );
}//Ends the run method
}, 0, 100);
addKeyListener ( new KeyAdapter ( )
{
/**
* This method is going detect any key pressed evenets
*
* @pre: none
* @param: e
* @return: none
* @post: Does particular tasks according to key stroke
*/
public void keyPressed ( KeyEvent e )
{
//Gets the movements from the MoveBomber class
int k = e.getKeyCode( );
if ( k == KeyEvent.VK_LEFT )
{
System.out.println ( "Left" );
}
}//Ends the keyPressed method
/**
* This method is going detect any key released evenets
*
* @pre: none
* @param: e
* @return: none
* @post: Does particular tasks according to key release
*/
public void keyReleased ( KeyEvent e )
{
//Gets the released movements from MoveBomber class
}//Ends the keyReleased method
}//Ends the addKeyListener method which adds the KeyListener movements
);
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void paintComponent (Graphics g )
{
// put your code here
Graphics2D grGraphics = (Graphics2D ) g;
grGraphics.drawImage ( myBackground ,0 ,0 ,null );
drawImage( g );
}
public void drawImage ( Graphics g )
{
intMyRandom = 0 + (int)(Math.random() * ((1) + 1));
Graphics2D grGraphics = (Graphics2D ) g;
if ( intMyRandom == 0 )
{
grGraphics.drawImage ( myRight ,500 ,367 ,null );
}
else if ( intMyRandom == 1 )
{
grGraphics.drawImage ( myLeft ,500 ,367 ,null );
}
try
{
Thread.sleep ( 60 );
}
catch ( Exception ex )
{
}
}
}
I am kind of confused why it isnt catching any key presses. Can someone help me out?
- 05-19-2011, 02:28 PM #2
Please put the code in [ CODE][ /CODE] tags (without the space) to make it more readable.
- 05-19-2011, 02:34 PM #3
Also you left off the import statements. Please add them. Can't compile and test without them.
- 05-19-2011, 02:39 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Please help meJava Code:import javax.swing.JFrame; public class Tester { // instance variables - replace the example below with your own public static void main ( String [ ] args ) { JFrame myFrame; MainBoard myMainSwitchBoard = new MainBoard ( ); myFrame = new JFrame ( "BomberMan" ); myFrame.getContentPane( ).add ( myMainSwitchBoard ); myFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); myFrame.setSize ( 1000, 735); myFrame.setResizable ( false ); myFrame.setVisible ( true ); } } import java.awt.Color; 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 javax.swing.JPanel; import java.awt.Image; import javax.swing.ImageIcon; import java.awt.Toolkit; import java.awt.image.BufferStrategy; import java.awt.Canvas; import java.awt.Color; import java.awt.Frame; import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import javax.swing.*; import sun.audio.*; public class MainBoard extends JPanel { // instance variables - replace the example below with your own private Image myBackground; private Image myRight; private Image myLeft; private Timer myTimer; private int intMyRandom; /** * Constructor for objects of class MainBoard */ public MainBoard() { // initialise instance variables myBackground = new ImageIcon ( "Background.jpg" ).getImage( ); myRight = new ImageIcon ( "Right.jpg" ).getImage( ); myLeft = new ImageIcon ( "Left.jpg" ).getImage( ); myTimer = new Timer ( true ); myTimer.scheduleAtFixedRate( new TimerTask( ) { public void run() { repaint( ); }//Ends the run method }, 0, 100); addKeyListener ( new KeyAdapter ( ) { /** * This method is going detect any key pressed evenets * * @pre: none * @param: e * @return: none * @post: Does particular tasks according to key stroke */ public void keyPressed ( KeyEvent e ) { //Gets the movements from the MoveBomber class int k = e.getKeyCode( ); if ( k == KeyEvent.VK_LEFT ) { System.out.println ( "Left" ); } }//Ends the keyPressed method /** * This method is going detect any key released evenets * * @pre: none * @param: e * @return: none * @post: Does particular tasks according to key release */ public void keyReleased ( KeyEvent e ) { //Gets the released movements from MoveBomber class }//Ends the keyReleased method }//Ends the addKeyListener method which adds the KeyListener movements ); } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public void paintComponent (Graphics g ) { // put your code here Graphics2D grGraphics = (Graphics2D ) g; grGraphics.drawImage ( myBackground ,0 ,0 ,null ); drawImage( g ); } public void drawImage ( Graphics g ) { intMyRandom = 0 + (int)(Math.random() * ((1) + 1)); Graphics2D grGraphics = (Graphics2D ) g; if ( intMyRandom == 0 ) { grGraphics.drawImage ( myRight ,500 ,367 ,null ); } else if ( intMyRandom == 1 ) { grGraphics.drawImage ( myLeft ,500 ,367 ,null ); } try { Thread.sleep ( 60 ); } catch ( Exception ex ) { } } }
- 05-19-2011, 03:09 PM #5
1. Use a javax.swing.Timer, not java.util.Timer. The Swing Timer is designed to be used in conjunction with Swing GUIs.
How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
2. Never never call sleep(...) on the EDT. What are you trying to achieve by calling sleep(...) in painting code anyways?
3. Load images using ImageIO#read(...). It's pointless to construct an ImageIcon only to retrieve its image.
As to your primary question, a JPanel isn't focusable by default, and key events only go to the focused component. You can setFocusable(true) or (much better) achieve the desired results with key bindings.
How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
db
- 05-19-2011, 05:22 PM #6
Cross posted at:
Why isnt my program detecting any key presses? - Dev Shed
- 05-19-2011, 05:45 PM #7
Similar Threads
-
Multiple keyboard presses
By abbeywell in forum Java GamingReplies: 6Last Post: 05-02-2011, 04:57 PM -
Why isnt this working?
By GoingThroAPhase in forum New To JavaReplies: 4Last Post: 04-03-2010, 02:36 AM -
How can i use keyevent even tho the program isnt selected?
By Addez in forum New To JavaReplies: 11Last Post: 12-25-2009, 10:30 PM -
why isnt it sending value
By snitdesne in forum New To JavaReplies: 5Last Post: 10-24-2008, 07:24 PM -
Responding to button presses
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks