soruce code name: KeyDemo3.java
import java.awt.*;
import java.awt.event.*;
class KeyDemo3 extends Frame implements KeyListener
{
public KeyDemo3()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
System.out.println("key is Pressed");
char ch=ke.getKeyChar();
Graphics g=getGraphics();
if(ch=='c' || ch=='C')
g.drawOval(100,100,30,30);
if(ch=='s' || ch=='S')
g.drawRect(100,160,30,30);
if(ch=='r' || ch=='R')
g.drawRect(100,200,130,30);
}
public void keyReleased(KeyEvent me){}
public void keyTyped(KeyEvent me){}
public static void main(String[] args)
{
KeyDemo3 f=new KeyDemo3();
f.setTitle("Key Event program");
f.setSize(400,500);
f.setVisible(true);
}
}
output:
2 Comments
sir,
ReplyDeleteI like your program of circle, rectangle, square. I want to fill the color with red, blue if possible please suggest.
For this you refer windows programs fills color to circle , rectangle etc. Add this code in current program, we will get your expected output. If not I will upload new program for requirement.
ReplyDeletePost a Comment