hi all
i am facing a problem with the below code
i used keylistener so that when i click letter 'v' the program has to exit.
But the following code is not working.
Help me please..
i think we should add listener to the code but how to add listener in the below code ??
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;
public class Games extends Frame implements KeyListener
{
int x=250,y=460;
int points=0;
boolean istop=false;
Thread t=new Thread();
Games()
{
setVisible(true);
setSize(500,500);
}
public void repeat()
{
if(istop)
{
y+=1;
}
if(!istop)
{
y-=1;
}
if(y==30)
{
points++;
System.out.println("Points are : "+points );
istop=true;
}
if(y==460)
{
istop=false;
}
try
{
Thread.sleep(3);
}
catch(InterruptedException e)
{
repaint();
}
}
public void paint(Graphics g)
{
while(true)
{
g.clearRect(0,0,500,500);
g.fillOval(x,y,30,30);
repeat();
}
}
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode()== KeyEvent.VK_V)
{
setVisible(false);
}
}
public void keyTyped(KeyEvent ke)
{
if(ke.getKeyCode()== KeyEvent.VK_V)
{
System.exit(0);
}
}
public void keyReleased(KeyEvent ke)
{
}
public static void main(String args[])
{
new Games();
}
}
thanks in advance