Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keylisteners and AppletViewer

 
peter waterhouse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works well with IE5 and works on some others AppletViewers but with my version of appletviewer (and a few other peoples) the delete button does not clear the screen, in fact no key events are being registered at all!! whats going on?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class NewEvent extends Applet
{
protected int CurrentX = -1;
protected int CurrentY = -1;

class ClearScreen /*implements KeyListener */ extends KeyAdapter
{
public void keyPressed(KeyEvent f)
{
doClearScreen(f);
}
//public void keyReleased(KeyEvent g)
//{ }
//
//public void keyTyped(KeyEvent h)
//{ }
}
class Display extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent event)
{
doCdisplay(event);
}
}
public void init()
{
ClearScreen jj = new ClearScreen();
this.addKeyListener(jj);
Display kk = new Display();
this.addMouseMotionListener(kk);
this.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
doClick(e);
}
}
);
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
doMouseClicked(event);
doCdisplay(event);
}
}
);
this.addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent even)
{
CurrentX=-1;
}
}
);
}

public void doCdisplay(MouseEvent event)
{
Graphics g = this.getGraphics();
g.clearRect(1,1,43,11);
int x=event.getX();
int y=event.getY();
String cstring="" + x + "," + y;
Font font = new Font("TimesRoman", Font.PLAIN, 10);
g.setFont(font);
g.drawString(cstring, 10, 10);
}
public void doMouseClicked(MouseEvent e)
{
Graphics g = this.getGraphics();
int x = e.getX();
int y = e.getY();
if (CurrentX != -1)
g.drawLine(CurrentX, CurrentY, x, y);
CurrentX=x; CurrentY=y;
}
public void doClearScreen(KeyEvent f)
{
if(f.getKeyCode()==KeyEvent.VK_DELETE)
repaint();
}
public void doClick(MouseEvent e)
{
Graphics g = this.getGraphics();
Font font = new Font("TimesRoman", Font.PLAIN, 10);
g.setFont(font);
int x = e.getX();
int y = e.getY();
CurrentX=x;
CurrentY=y;
if(CurrentX != -1)
g.drawString(":", CurrentX, CurrentY);
}
}
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try putting a call to requestFocus() in your applet's init() method. This usually does the trick.
------------------
- Daniel
 
peter waterhouse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep, that did it,
Thanks Daniel.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic