Monday, 3 November 2014

Create an applet which displays a message in the center of the screen. The message indicates the events taking place on the applet window. Handle events like mouse click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message should update each time an event occurs. The message should give details of the event such as which mouse button was pressed, which key is pressed etc.


/*

Name : Shreyal Mandot

Title : Assignment 8b1. Create an applet which displays a message in the center of the screen. The message indicates the events taking place on the applet window. Handle events like mouse click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message should update each time an event occurs. The message should give details of the event such as which mouse button was pressed, which key is pressed etc.

*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*
<applet code="ass8b1.class" width=400 height=200>

</applet>
*/

public class ass8b1 extends Applet implements MouseMotionListener,MouseListener,KeyListener
{

String msg="";

public void init()
{

setBackground(Color.cyan);
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);

}

public void paint(Graphics g)
{

g.drawString(msg,10,10);

}

public void mouseDragged(MouseEvent e)
{

msg="Mouse Dragged.";
repaint();

}

public void mouseMoved(MouseEvent e)
{

msg="Mouse Moved.";
repaint();

}

public void mouseClicked(MouseEvent e)
{

msg="Mouse Button "+e.getButton()+"clicked.";
repaint();

}

public void mousePressed(MouseEvent e)
{

msg="Mouse Button "+e.getButton()+"pressed.";
repaint();

}

public void mouseReleased(MouseEvent e)
{

msg="Mouse Button Released.";
repaint();

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public void keyTyped(KeyEvent e)
{

msg="Key Typed "+ e.getKeyChar();
repaint();

}

public void keyPressed(KeyEvent e)
{

msg="Key pressed "+ e.getKeyChar();
repaint();

}

public void keyReleased(KeyEvent e)
{

}

}

Create a conversion applet which accepts value in one unit and converts it to another. The input and output unit is selected from a list. Perform conversion to and from feet, inches, Centimeter, Meters and Kilometers.





Name : Shreyal Mandot

Title : Assignmeny 8a3. Create a conversion applet which accepts value in one unit and converts it to another. The input and output unit is selected from a list. Perform conversion to and from feet, inches, Centimeter, Meters and Kilometers.



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

/*
<applet code="ass8a3.class" width=800 height=800>

</applet>
*/

public class ass8a3 extends Applet implements ItemListener
{

int i,o;
double ip,op;
JComboBox j=new JComboBox();
JComboBox j1=new JComboBox();
JTextArea t=new JTextArea("0.0              ");
JTextArea t1=new JTextArea("0.0             ");

public void init()
{

j.addItem(new String("feet"));
j.addItem(new String("inch"));
j.addItem(new String("centimeter"));
j.addItem(new String("meter"));
j.addItem(new String("kilometer"));

j1.addItem(new String("feet"));
j1.addItem(new String("inch"));
j1.addItem(new String("centimeter"));
j1.addItem(new String("meter"));
j1.addItem(new String("kilometer"));

setLayout(new FlowLayout(FlowLayout.LEFT,10,10));

add(new Label(" Input "));add(t);

add(new Label(" Output  "));add(t1);

add(new Label(" Unit "));add(j);

add(new Label(" Unit "));add(j1);

j.addItemListener(this);
j1.addItemListener(this);

}

public void paint(Graphics g)
{



}
public void itemStateChanged(ItemEvent e)
{

i=j.getSelectedIndex();
o=j1.getSelectedIndex();
ip=Double.parseDouble(t.getText());
switch(i)
{

case 0: ip=ip*0.3048;break;
case 1:ip=(ip*2.54)/100;break;
case 2:ip=ip/100;break;
case 4:ip=ip*1000;

}
switch(o)
{

case 0:op=ip*3.2808;break;
case 1:op=ip*39.37;break;
case 2:op=ip*100;break;
case 3:op=ip;break;
case 4:op=ip/1000;

}
String tmp=" "+op;
t1.setText(tmp);

}

}

Create an applet to display coordinates of mouse movements in a text box.




Name : Shreyal Mandot

Title : Assignment 8a2. Create an applet to display coordinates of mouse movements in a text box.



import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*
<applet code="ass8a2.class" width=400 height=200>

</applet>
*/

public class ass8a2 extends Applet implements MouseMotionListener
{

int x=0,y=0;

public void init()
{

setBackground(Color.orange);
addMouseMotionListener(this);

}

public void paint(Graphics g)
{

String msg="X: "+x+" Y: "+y;
g.drawString(msg,10,10);

}
public void mouseDragged(MouseEvent e)
{



}
public void mouseMoved(MouseEvent e)
{

x=e.getX();
y=e.getY();
repaint();

}

}

Create an applet to display a message at the center of the applet. The message is passed as a parameter to the applet.




Name : Shreyal Mandot

Title : Assignment 8a1. Create an applet to display a message at the center of the applet. The message is passed as a parameter to the applet.




import java.awt.*;
import java.applet.*;


/*
<applet code="ass8a1.class" width=400 height=200>
</applet>
*/

public class ass8a1 extends Applet
{

String msg;

public void init()
{

Font f=new Font("big",Font.BOLD|Font.ITALIC,24);
setFont(f);
setBackground(Color.orange);
msg=getParameter("msg");

if(msg == null)
{

}

msg="No One Msg Passed";

}

public void paint(Graphics g)
{

int x,y;
Dimension d;

d=this.getSize();

FontMetrics f=g.getFontMetrics();

x=(d.width-f.stringWidth(msg))/2;

y=(d.height-f.getHeight())/2;

g.drawString(msg,x,y);

}

}

Write a program to implement a simple calculator. Display appropriate error messages in a dialog box.(Use the screen designed in Assignment 6b1.)




Name : Shreyal Mandot

Title : Assignment 7b1. Write a program to implement a simple calculator. Display appropriate error messages in a dialog box.(Use the screen designed in Assignment 6b1.)



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

class ass7b1 extends JFrame implements ActionListener
{

private JPanel p1,p2;
    private JTextField t1;
    private JButton b[],b1;
     
    StringBuffer s1 = new StringBuffer();
 
    double n1,n2;
    char ch;

    public ass7b1(String s)
    {
             
    super(s);
             
        p1=new JPanel();
        p2=new JPanel();
        t1=new JTextField(20);
        b1=new JButton("Reset");
             
        String str[]={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
             
        b=new JButton[str.length];
             
        for(int i=0;i<str.length;i++)
        b[i]=new JButton(str[i]);
             
        p1.setLayout(new BorderLayout());
        p1.add(t1,BorderLayout.NORTH);
        p1.add(b1,BorderLayout.EAST);
     
        p2.setLayout(new GridLayout(4,4));
        b1.addActionListener(this);
             
        for(int i=0;i<b.length;i++)
        {
       
        p2.add(b[i]);
            b[i].addActionListener(this);
             
        }
             
        setLayout(new BorderLayout());
     
        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.CENTER);

}

    public void actionPerformed(ActionEvent e)
    {
             
    if(e.getSource()==b1)
        {
                     
        n1=n2=0;
                     
            ch=' ';
                     
            t1.setText(" ");
             
        }
             
        for(int i=0;i<b.length;i++)
        if(e.getSource()==b[i])
            {
                             
            String s=b[i].getActionCommand();
                             
                if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
                {
             
                try
                {
                                     
                    ch=s.charAt(0);
                        n1=Double.parseDouble(new String(s1));
                        s1.replace(0,s1.length()," ");
                                     
                    }
                                   
                    catch(NumberFormatException ae)
                    {
                                     
                    JOptionPane.showMessageDialog (null,"Invalid","ERROR",JOptionPane.ERROR_MESSAGE);
                                   
                    }
                             
}
                             
                else if(s.equals("."))
                {
                                     
                s1.append(".");
                                     
                    String s22=new String(s1);
                    t1.setText(s22);
                             
                }
                             
                else if(s.equals("="))
                {
                                     
                double res=0;
                                     
                    n2=Double.parseDouble(new String(s1));

                    if(ch == '+')
                    res=n1+n2;
                 
                    else if(ch == '-')
                        res=n1-n2;
                                     
                    else if(ch == '*')
                        res=n1*n2;
                                     
                    else if(ch == '/')
                        res=n1/n2;

t1.setText(new Double(res).toString());
                    s1.replace(0,s1.length()," ");
                    n1=res;
                    res=0;ch=' ';
                             
}
                             
                else
                {
                                     
                for(int j=0;j<=b.length;j++)
                    if(s.equals(new Integer(j).toString()))
                        {
                                                     
                        s1.append(new Integer(j).toString());
                            String s22=new String(s1);
                            t1.setText(s22);
                                             
                        }
                             
                }
                     
            }
     
        }

        public static void main(String arg[])
        {
             
        ass7b1 c =new ass7b1("My Calculator");
            c.setSize(300,300);
            c.setVisible(true);
            c.setLocation(500,200);
            c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }

}

Write a java program to accept user name in a text box. Accept the class of the user (FY/SY/TY) using radio buttons and the hobbies of the user using checkboxes. Display the selected options in a text box.(Use the screen designed in Assignment 6a2.)




Name : Shreyal Mandot

Title : assignment 7a3. Write a java program to accept user name in a text box. Accept the class of the user (FY/SY/TY) using radio buttons and the hobbies of the user using checkboxes. Display the selected options in a text box.(Use the screen designed in Assignment 6a2.)



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

class ass7a3 extends JFrame implements ActionListener
{
     
private JLabel l1,l2,l3;
    private JButton b;
    private JRadioButton r1,r2,r3;
    private JCheckBox c1,c2,c3;
    private JTextField t1,t2;
    private ButtonGroup b1;
    private JPanel p1,p2;
    private StringBuffer s1=new StringBuffer();

    public ass7a3(String s)
    {
 
    super(s);
             
        b1=new ButtonGroup();
        p1=new JPanel();
        p2=new JPanel();
        b=new JButton("Clear");
     
        b.addActionListener(this);

        r1=new JRadioButton("FY");
        r2=new JRadioButton("SY");
        r3=new JRadioButton("TY");

        b1.add(r1);
        b1.add(r2);
        b1.add(r3);
             
        r1.addActionListener(this);
        r2.addActionListener(this);
        r3.addActionListener(this);

        c1=new JCheckBox("Music");
        c2=new JCheckBox("Dance");
        c3=new JCheckBox("Sports");

        c1.addActionListener(this);
        c2.addActionListener(this);
        c3.addActionListener(this);

        l1=new JLabel("Your Name");
        l2=new JLabel("Your Class");
        l3=new JLabel("Your Hobbies");
             
        t1=new JTextField(30);
        t2=new JTextField(45);

        p1.setLayout(new GridLayout(5,2));
        p1.add(l1);p1.add(t1);
        p1.add(l2);p1.add(l3);
        p1.add(r1);p1.add(c1);
        p1.add(r2);p1.add(c2);
        p1.add(r3);p1.add(c3);

        p2.setLayout(new FlowLayout());
        p2.add(b);
        p2.add(t2);

        setLayout(new BorderLayout());
        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.EAST);
     
}

    public void actionPerformed(ActionEvent e)
    {
             
    if(e.getSource()==r1)
        {
                     
        String s =t1.getText();
         
            s1.append("Name = ");
            s1.append(s);
                     
            s1.append(" Class = FY");
             
        }
             
        else if(e.getSource()==r2)
        {
                     
        String s =t1.getText();
                     
            s1.append("Name = ");
            s1.append(s);
                     
            s1.append(" Class = SY");
             
        }
             
        else if(e.getSource()==r3)
        {
                     
        String s =t1.getText();
         
            s1.append("Name = ");
            s1.append(s);
                     
            s1.append(" Class = TY");
             
        }
             
        else if(e.getSource()==c1)
        {
                     
        s1.append(" Hobbies = Music");
             
        }
     
        else if(e.getSource()==c2)
        {
                     
        s1.append(" Hobbies = Dance");
             
        }
             
        else if(e.getSource()==c3)
        {
                     
        s1.append(" Hobbies = Sports");
             
        }

        String s2=new String(s1);
        t2.setText(s2);

        if(e.getSource()==b)
        {
     
        t2.setText(" ");
            t1.setText(" ");
             
        }

}

    public static void main(String arg[])
    {
             
    ass7a3 s=new ass7a3("Profile");
             
        s.setSize(600,200);
        s.setVisible(true);
        s.setLocation(400,400);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}


Write a java program to validate user login and password. If they do not match, display appropriate message in a dialog box. The user is allowed maximum 3 chances. (Use the screen designed in Assignment 6a1.)





Name : Shreyal Mandot

Title : Assignment 7a2. Write a java program to validate user login and password. If they do not match, display appropriate message in a dialog box. The user is allowed maximum 3 chances. (Use the screen designed in Assignment 6a1.)




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

class ass7a2 extends JFrame implements ActionListener
{

private JTextField t1;
private JPasswordField t2;
private JButton b1,b2;
private JLabel l1,l2;

int n;

    public ass7a2(String s)
    {
 
    super(s);
     
        n=0;
             
        t1=new JTextField(10);
        t2=new JPasswordField(10);
     
        b1=new JButton("OK");
        b1.addActionListener(this);
     
        b2=new JButton("Cancel");
        b2.addActionListener(this);
             
        l1=new JLabel("Login Name");
        l2=new JLabel("Password");
             
        t2.setEchoChar('*');
     
        setLayout(new GridLayout(3,2));
        add(l1);add(t1);
        add(l2);add(t2);
        add(b1);add(b2);
     
}

    public void actionPerformed(ActionEvent e)
    {
             
    if(e.getSource()==b1)
        {
     
        String s=t1.getText();
         
            char c[]=new char[50];
            c=t2.getPassword();
                     
            String s1=new String(c);
                     
            if(s.equals("admin") && s1.equals("admin"))
            {
         
            JOptionPane.showMessageDialog(null,"Success");
                n=0;
                     
            }
                     
            else
            {
                             
            JOptionPane.showMessageDialog(null,"Fail");
             
                t1.setText(" ");
                t2.setText(" ");
                n++;
                             
                if(n==3)
                System.exit(0);
                     
            }
             
}
             
        else if(e.getSource()==b2)
        {
                     
        System.exit(0);
             
        }
     
}

public static void main(String arg[])
    {
 
    ass7a2 s=new ass7a2("Login Please");
        s.setSize(400,150);
        s.setVisible(true);
        s.setLocation(200,200);
        s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }

}