Jump to content

Can't get java to work quite right (developing)


hanes
 Share

Recommended Posts

Hi, Im making an quick program in java. It all seems to work, but the applets never get keyEvents.... For example, the code below is an applet that when ran, should move a red dot left or right depending on key presses. But for some reason in linux, NO KEY EVENTS for the applet at all. In windoes or mac it works fine. I have tested this with konqueror, appletviewer and firefox.. no dice.

 

Anyone know whats going on?

 

example code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class Bubbles extends JApplet implements KeyListener
{

int msg = 0;

public void init()
{
msg = 10;
addKeyListener(this);
}
//this function happens when someone hits a button
public void keyPressed(KeyEvent e)
{
System.out.println("SFDF");
msg = e.getKeyCode();
repaint();
}

public void keyReleased(KeyEvent e)
{
System.out.println("SFDF");
repaint();
}
public void keyTyped(KeyEvent e)
{
System.out.println("SFDF");
msg = e.getKeyCode();
repaint();
}

public void paint(Graphics g)
{
g.drawString("code = " + msg, 20,20);

}
}

Edited by hanes
Link to comment
Share on other sites

Seems like the JApplet is unable to get the keyboard focus - I'm not quite sure why but even a setFocusable(true) doesn't fix it. Of course, usually there's a bit more in the applet so it's not usually a problem.

 

Workaround fix: use a Canvas instead:

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JApplet;

public class Testclick extends JApplet implements KeyListener
{

int msg = 0;
Canvas canvas = null;

public void init()
{
	msg = 1;
	canvas = new Canvas() {
		public void paint(Graphics g)
		{
			g.drawString("code = " + msg, 20,20);
		}
	};
	canvas.addKeyListener(this);
	getContentPane().add(canvas);
}

//	this function happens when someone hits a button
public void keyPressed(KeyEvent e)
{
	System.out.println("key press: " + e.getKeyCode());
	msg = e.getKeyCode();
	canvas.repaint();
}

public void keyReleased(KeyEvent e)
{
	System.out.println("key release");
}
public void keyTyped(KeyEvent e)
{
	System.out.println("key typed: " + e.getKeyChar());
}
}

Does that work?

Link to comment
Share on other sites

Yes it works. Thanks a lot.

 

It also really pisses me off. How many years has sun had to get this thing right? I remember how excited I was when I was learning Java, thinking that I was learning a professional and well thought out language. Now, I see the bug reports grow, the lag persist (remember when Sun was touting that Java was fast?!)

 

I gotta find another language to teach :)

 

Anyways, thanks for the help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...