import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class keydraw extends JFrame implements KeyListener
{
Label lbl_title;
int ch;
int a;
keydraw()
{
a=0;
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
ch=ke.getKeyCode();
}
public void keyTyped(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
if(ch==ke.VK_LEFT)
a=1;
if(ch==ke.VK_RIGHT)
a=2;
if(ch==ke.VK_UP)
a=3;
if(ch==ke.VK_DOWN)
a=4;
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
if(a==1)
g.drawLine(200,100,300,200);
if(a==2)
g.drawOval(200,100,200,100);
if(a==3)
g.drawRoundRect(200,100,50,50,20,20);
if(a==4)
g.drawRect(300,200,50,50);
}
public static void main(String arge[])
{
keydraw f=new keydraw();
f.setTitle("Press direction keys");
f.setVisible(true);
f.setSize(400,400);
}
}
No comments:
Post a Comment