Getting a <JPanel> to Print Out Characters Typed In

K

KevinSimonson

I've written a Java program that brings up a <JFrame> and its <JPanel>
and lets me click at different parts of the <JPanel> and draws x's
there. That much works. But I also want to type letters from the
keypad and have them appear on the <JPanel> too. I've written the
code for that (I'm including it below); I've got a class <Listener>
that implements all three of interfaces <MouseListener>,
<MouseMotionListener>, and <KeyListener>, and in my constructor I have
a call to <addKeyListener()>.

But it's not working. I click on different parts of the <JPanel> and
the x's get drawn, but when I start typing, nothing happens. I even
put <System.out.println()>s in my <keyPressed()>, <keyReleased()>, and
<keyTyped()> methods to verify that those three methods don't get
called. They don't.

Can anyone tell me what I'm doing wrong that's keeping my code from
printing out the characters I type when the program's running?

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.Font;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;

public class PointWrite extends JPanel
{
private class Written
{
Point location;
char[] string;
int length;
int maxWidth;
int height;
int xPstn;

private Written ( Point lctn
, int strngLngth)
{
location = new Point( lctn);
string = new char[ strngLngth];
length = 0;
maxWidth = 0;
height = 12;
xPstn = 0;
}

private void draw ( Graphics page)
{
if (length > 0)
{ Point pstn
= new Point( location.x - maxWidth / 2, location.y -
height / 2);
int leftMost = pstn.x;
page.setColor( Color.red);
page.drawRect( pstn.x, pstn.y, maxWidth, height);
pstn.y += 10;
page.setColor( Color.green);
for (int chrctr = 0; chrctr < length; chrctr++)
{ if (string[ chrctr] == '\n')
{ pstn.y += 12;
pstn.x = leftMost;
}
else
{ page.drawString( "" + string[ chrctr], pstn.x, pstn.y);
pstn.x += 7;
}
}
}
else
{ page.setColor( Color.orange);
page.drawLine
( location.x - 5, location.y - 5, location.x + 5, location.y
+ 5);
page.drawLine
( location.x - 5, location.y + 5, location.x + 5, location.y
- 5);
}
}

private void insert ( char insertee)
{
if (length < string.length)
{ string[ length++] = insertee;
if (insertee == '\n')
{ System.out.println( "Found a newline!");
height += 12;
xPstn = 0;
}
else
{ xPstn += 7;
if (maxWidth < xPstn)
{ maxWidth = xPstn;
}
}
}
else
{ System.err.println( "Overran string!");
}
}
}

private class Listener
implements MouseListener, MouseMotionListener, KeyListener
{
public void mouseClicked ( MouseEvent evnt) {}

public void mousePressed ( MouseEvent evnt)
{
writes[ count++] = new Written( evnt.getPoint(), stringLength);
repaint();
}

public void mouseReleased ( MouseEvent evnt) {}
public void mouseEntered ( MouseEvent evnt) {}
public void mouseExited ( MouseEvent evnt) {}
public void mouseDragged ( MouseEvent evnt) {}
public void mouseMoved ( MouseEvent evnt) {}

public void keyPressed ( KeyEvent evnt)
{
System.out.println( "Executed <keyPressed()>!");
writes[ count - 1].insert( evnt.getKeyChar());
repaint();
}

public void keyReleased ( KeyEvent evnt)
{
System.out.println( "Executed <keyReleased()>!");
}
public void keyTyped ( KeyEvent evnt)
{
System.out.println( "Executed <keyTyped()>!");
}
}

int width;
int height;
Written[] writes;
int stringLength;
int count;

private PointWrite ( int wdth
, int hght
, int nmbrWrts
, int strngLngth)
{
Listener lstnr = new Listener();
width = wdth;
height = hght;
writes = new Written[ nmbrWrts];
stringLength = strngLngth;
count = 0;
addMouseListener( lstnr);
addMouseMotionListener( lstnr);
addKeyListener( lstnr);
setPreferredSize( new Dimension( width, height));
}

public void paintComponent ( Graphics page)
{
Point pstn;
page.setColor( Color.black);
page.fillRect( 0, 0, width, height);
for (int strng = 0; strng < count; strng++)
{ writes[ strng].draw( page);
}
}

public static void main ( String[] arguments)
{
if (arguments.length == 4)
{ try
{ int wdth = Integer.parseInt( arguments[ 0]);
int hght = Integer.parseInt( arguments[ 1]);
int nw = Integer.parseInt( arguments[ 2]);
int sl = Integer.parseInt( arguments[ 3]);
PointWrite pntWrte = new PointWrite( wdth, hght, nw, sl);
JFrame pwFrame
= new JFrame
( "PointWrite " + wdth + " by " + hght + " with " + nw +
" and "
+ sl + '.');
pwFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
pwFrame.getContentPane().add( pntWrte);
pwFrame.pack();
pwFrame.setVisible( true);
}
catch ( NumberFormatException excptn)
{ System.err.println( "Unable to convert string to an
integer!");
}
}
else
{ System.out.println( "Usage is");
System.out.println
( " java PointWrite <wdth> <hght> <#-writes> <string-
length>");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top