sanjay manohar wrote:
=Two possible problems
=1) your JPanel mightn't have the focus. Your panel should be allowed
to
=get focus when clicked on, or else contain a component that is.
How do I give my <JPanel> the focus, or allow it to get focus when
clicked on?
=2) as chris said, you need to record which key was last pressed,
=because the paintComponent() method may be called more than once (even
=though you only call repaint once). This might immediately remove what
=you had painted.
Okay, I changed my code so it should remember every key that's
pressed, and prints them all everytime <paintComponent> is called. I
also added some code so that if the mouse is clicked, that event is
remembered too. It works; if I click somewhere on my <JPanel> the ap-
plication prints out where I clicked on it; but I still type 'a' or
'b' and get nothing.
---Kevin Simonson
"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
####################################################################
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Rosser extends JPanel implements KeyListener,
MouseListener
{
int[] keysEntered = new int[ 100];
int numberOfKeys = 0;
public Rosser ()
{
setPreferredSize( new Dimension( 800, 800));
addKeyListener( this);
addMouseListener( this);
}
protected void paintComponent ( Graphics page)
{
int enteredKey;
page.setColor( Color.black);
page.fillRect( 0, 0, 800, 800);
page.setColor( Color.green);
page.drawString( "Entered keys:", 400, 200);
page.setColor( Color.cyan);
for (enteredKey = 0; enteredKey < numberOfKeys; enteredKey++)
{ if (keysEntered[ enteredKey] == KeyEvent.VK_A)
{ page.drawString( "VK_A", 400, 215 + 15 * enteredKey);
}
else if (keysEntered[ enteredKey] == KeyEvent.VK_B)
{ page.drawString( "VK_B", 400, 215 + 15 * enteredKey);
}
else if (keysEntered[ enteredKey] == KeyEvent.VK_C)
{ page.drawString( "VK_C", 400, 215 + 15 * enteredKey);
}
else
{ page.drawString
( "other value (" + keysEntered[ enteredKey] + ")"
, 400, 215 + 15 * enteredKey);
}
}
page.setColor( Color.green);
page.drawString( "End of entered keys.", 400, 215 + 15 *
numberOfKeys);
}
public void keyPressed ( KeyEvent kyEvnt)
{
keysEntered[ numberOfKeys++] = kyEvnt.getKeyCode();
repaint();
}
public void mousePressed ( MouseEvent msEvnt)
{
Point pressed = msEvnt.getPoint();
keysEntered[ numberOfKeys++] = 10000 * pressed.x + pressed.y;
repaint();
}
public void keyReleased ( KeyEvent kyEvnt) {}
public void keyTyped ( KeyEvent kyEvnt) {}
public void mouseClicked ( MouseEvent msEvnt) {}
public void mouseEntered ( MouseEvent msEvnt) {}
public void mouseExited ( MouseEvent msEvnt) {}
public void mouseReleased ( MouseEvent msEvnt) {}
public static void main ( String[] arguments)
{
JFrame idFrame = new JFrame( "id program");
Rosser idPanel = new Rosser();
idFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
idFrame.getContentPane().add( idPanel);
idFrame.pack();
idFrame.setVisible( true);
}
}