Showing posts with label java swing. Show all posts
Showing posts with label java swing. Show all posts

Saturday, 22 June 2013

Java swing program for multicasting


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

class multicast extends JFrame implements ActionListener
{
    JButton b,b1;
    int c=2,x=50;

    multicast()
    {
        setLayout(new FlowLayout());
        b=new JButton("click one time o/p multicast");
        b.addActionListener(this);
        add(b);

        b1=new JButton("cancel");
        b1.addActionListener(this);
        add(b1);

    }

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

        if(str=="click one time o/p multicast")
        {
            myframe f=new myframe();
            f.setSize(300,300);
            f.setLocation(20*c,20*c);
            f.setVisible(true);
            c=c+2;
        }


        if(str=="cancel")
        {
            System.exit(0);
        }
    }

    public static void main(String args[])
    {
        multicast m=new multicast();
        m.setSize(400,400);
        m.setVisible(true);
    }
}


write a java program to demo for Paint Mode (XOR Mode) in Swing Programming


//wap to demo for Paint Mode in Swing Programming

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mypanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        g.setColor(Color.red);
        g.fillOval(100,50,50,50);
        g.setXORMode(Color.pink);
        g.setColor(Color.green);
        g.fillOval(100,80,60,60);
    }
}
class myframe3 extends JFrame
{
    Container c;
    myframe3()
    {
        mypanel p=new mypanel();
        c=getContentPane();
        c.add(p);
    }
    public static void main(String args[])
    {
        myframe3 f=new myframe3();
        f.setTitle("paint");
        f.setSize(600,600);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
}

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


Friday, 21 June 2013

Creat Menu bar in Java Swing and draw various shapes




// TO create Menu Bar

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

class menu extends JFrame implements ActionListener
{
    JMenuBar mb;
    JMenu draw;
    JMenuItem rect,line,oval;
    //Container c;
    menu()
    {
        //c=getContentPane();
        setLayout(null);
        mb=new JMenuBar();

        draw=new JMenu("draw");
        draw.setMnemonic('d');
        mb.add(draw);

        rect=new JMenuItem("rect");
        rect.addActionListener(this);
        rect.setMnemonic('r');
        draw.add(rect);

        line=new JMenuItem("line");
        line.addActionListener(this);
        line.setMnemonic('l');
        draw.add(line);

        oval=new JMenuItem("oval");
        oval.addActionListener(this);
        oval.setMnemonic('o');
        draw.add(oval);

        setJMenuBar(mb);
    }

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

        if(str=="rect")
        g.drawRect(100,100,50,50);

        if(str=="line")
        g.drawLine(300,50,400,350);

        if(str=="oval")
        g.drawOval(400,50,50,50);
    }

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