help me urgent(about image movement on keyboard event)

M

mukesh

I want to develope standalone application by using java AWT in which an
image would move when down key pressed.
the code is as follows.
................................................
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
class event extends Frame implements KeyListener
{


Toolkit tk;
public Image ig;
Graphics g,g1;

event()
{
setLayout(null);
setSize(300,300);
// b=new Button();
//b.setBounds(20,20,30,30);
//add(b);

tk =Toolkit.getDefaultToolkit();
ig=tk.getImage("C:/pro/New Folder/Mukesh/sky.jpg");
ig.addKeyListener(this);
//addKeyListener(this);

setVisible(true);
}

public void keyPressed(KeyEvent ke) {

if(ke.getKeyCode()==KeyEvent.VK_DOWN)
{
paint(g1);
}
}

public void keyReleased(KeyEvent ke)
{

}

public void keyTyped(KeyEvent ke) {

}

public void paint(Graphics g)
{
g.drawImage(ig,100,100,40,40,this);

}

public static void main(String[] args)
{
event as=new event();
}
}
................................................
Afther compilation following error genrated :----

C:\pro\New Folder\Mukesh>javac event.java
event.java:22: cannot find symbol
symbol : method addKeyListener(event)
location: class java.awt.Image
ig.addKeyListener(this);
^
1 error

So, plz help me.
 
R

Ralf Seitner

mukesh said:
I want to develope standalone application by using java AWT in which an
image would move when down key pressed.
the code is as follows.
...............................................
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
class event extends Frame implements KeyListener
{


Toolkit tk;
public Image ig;
Graphics g,g1;

event()
{
setLayout(null);
setSize(300,300);
// b=new Button();
//b.setBounds(20,20,30,30);
//add(b);

tk =Toolkit.getDefaultToolkit();
ig=tk.getImage("C:/pro/New Folder/Mukesh/sky.jpg");
ig.addKeyListener(this);
//addKeyListener(this);

setVisible(true);
}

public void keyPressed(KeyEvent ke) {

if(ke.getKeyCode()==KeyEvent.VK_DOWN)
{
paint(g1);
}
}

public void keyReleased(KeyEvent ke)
{

}

public void keyTyped(KeyEvent ke) {

}

public void paint(Graphics g)
{
g.drawImage(ig,100,100,40,40,this);

}

public static void main(String[] args)
{
event as=new event();
}
}
...............................................
Afther compilation following error genrated :----

C:\pro\New Folder\Mukesh>javac event.java
event.java:22: cannot find symbol
symbol : method addKeyListener(event)
location: class java.awt.Image
ig.addKeyListener(this);
^
1 error

So, plz help me.
Hi!
java.awt.Image is not a subclass of java.awt.Component, where
addKeyListener() is defined. So add the KeyListener to a subclass of
Component or a Component. (e.g. Frame is a subclass of Component). Have
a look at the Java API and the Tutorial, how to use Listeners.
bye, Ralf
 
Z

Zaph0d

Since I gather you want the picture to move when the user press "down"
from anywhere in your application, I suggest you look at
KeyboardFocusManager instead of keyboard event listeners.
If I'm wrong, disregard.
 

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,780
Messages
2,569,608
Members
45,253
Latest member
BlytheFant

Latest Threads

Top