Showing posts with label jframe. Show all posts
Showing posts with label jframe. Show all posts

Monday, 17 June 2013

Program for key event to draw various shapes


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);
    }
}

Program in java for JComboBox in swing


//WAP to check the combobox

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

class combo extends JFrame implements ItemListener
{
    JComboBox cb;
    Container c;

    combo()
    {
        c=getContentPane();
        c.setLayout(null);

        cb=new JComboBox();
        cb.addItem("Red");
        cb.addItem("Blue");
        cb.addItem("Pink");
        cb.setBounds(50,50,100,30);
        cb.addItemListener(this);
        c.add(cb);
    }
    public void itemStateChanged(ItemEvent ie)
    {
        String str=(String)cb.getSelectedItem();

        if(str=="Red")
        {
            c.setBackground(Color.red);
        }
        if(str=="Blue")
        {
            c.setBackground(Color.blue);
        }

        if(str=="Pink")
        {
            c.setBackground(Color.pink);

        }
    }
    public static void main(String args[])
    {
        combo f=new combo();
        f.setTitle("demo");
        f.setSize(500,500);
        f.setVisible(true);
    }
}

Java program for typing on frame


//WAP to handle Key event

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

class key1 extends JFrame implements KeyListener
{
    String msg=" ";
    char ch;

    key1()
    {
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent ke)
    {
        ch=ke.getKeyChar();
        msg=msg+ch;
    }
    public void keyTyped(KeyEvent ke)
    {

    }
    public void keyReleased(KeyEvent ke)
    {
        Graphics g=getGraphics();
        g.drawString(msg,100,100);
    }
    public static void main(String args[])
    {
        key1 k=new key1();
        k.setTitle("Key event");
        k.setSize(400,400);
        k.setVisible(true);
    }
}