[Logo]
Forums Register Login
Keylisteners and AppletViewer
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);
}
}
Try putting a call to requestFocus() in your applet's init() method. This usually does the trick.
------------------
- Daniel
yep, that did it,
Thanks Daniel.

This thread has been viewed 810 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 10, 2018 20:23:04.