• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Keylisteners and AppletViewer  RSS feed

 
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);
}
}
 
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!