Showing posts with label draw Round Rectangle. Show all posts
Showing posts with label draw Round Rectangle. Show all posts

Saturday, 22 June 2013

Draw Various shapes with radio button in Java swing


//WAP to check JRadioButton in panel

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

class mypanel extends JPanel implements ActionListener
{
    JRadioButton red,blue,green;
    ButtonGroup color;
    int a;

    mypanel()
    {
        setLayout(null);

        red=new JRadioButton("Rect");
        red.addActionListener(this);
        red.setBounds(50,50,100,30);
        add(red);

        blue=new JRadioButton("RoundRect");
        blue.addActionListener(this);
        blue.setBounds(50,100,100,30);
        add(blue);

        green=new JRadioButton("Circle");
        green.addActionListener(this);
        green.setBounds(50,150,100,30);
        add(green);

        color=new ButtonGroup();

        color.add(red);
        color.add(blue);
        color.add(green);
    }

    public  void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();


        if(str=="Rect")
        a=1;

        if(str=="RoundRect")
        a=2;

        if(str=="Circle")
        a=3;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(a==1)
        {
          g.drawRect(300,200,50,50);
        }
        if(a==2)
        {
          g.drawRoundRect(100,250,50,50,20,20);
        }
        if(a==3)
        {
          g.drawOval(200,100,50,100);
        }
    }
}
class myframe extends JFrame
{
    Container c;
    myframe()
    {
        c=getContentPane();
        mypanel p=new mypanel();
        c.add(p);
    }
}

class rbpanel
{
    public static void main(String args[])
    {
        myframe f=new myframe();
        f.setTitle("RadioButton panel");
        f.setSize(500,500);
        f.setVisible(true);
    }
}


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

Friday, 14 June 2013

program in java - draw Round Rectangle with awt


import java.awt.*;
class a4 extends Frame
{
    public static void main(String args[])
    {
        a4 a=new a4();
        a.setTitle("RoundRectangle");
        a.setSize(500,500);
        a.setVisible(true);
        //a.show();
    }
    public void paint(Graphics g)
    {


        g.drawRoundRect(180,180,200,200,20,20);

        g.drawRoundRect(60,60,120,120,20,20);
    }
}