Java Lab Assignment 4
Java Lab Assignment 4
import java.awt.*;
import java.awt.event.*;
ColorScroll()
s1=new Scrollbar();
s2=new Scrollbar();
s3=new Scrollbar();
label.setAlignment(Label.CENTER);
label.setBounds(250,100,40,40);
label1.setAlignment(Label.CENTER);
label1.setBounds(250,150,40,40);
label2.setAlignment(label2.CENTER);
label2.setBounds(250,200,40,40);
label3.setBounds(70,250, 150,50);
title.setBounds(80,60, 100,30);
62
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
s1.setBounds(50,100, 150,40);
s1.setOrientation(Scrollbar.HORIZONTAL);
s1.setMaximum (265);
s2.setBounds(50,150, 150,40);
s2.setOrientation(Scrollbar.HORIZONTAL);
s2.setMaximum (265);
s3.setBounds(50,200, 150,40);
s3.setOrientation(Scrollbar.HORIZONTAL);
s3.setMaximum (265);
title.setText("Scroll Color");
add(title);
add(s1);add(label);
add(s3);add(label1);
add(s2);add(label2);
add(label3);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
s3.addAdjustmentListener(this);
setTitle("Scroll Event");
setSize(400,400);
setLayout(null);
setVisible(true);
int R=s1.getValue();
int G=s2.getValue();
int B=s3.getValue();
63
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
label.setBackground(c1);
label1.setBackground(c2);
label2.setBackground(c3);
label3.setBackground(c4);
64
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
B . Write a program to draw any thing on Frame using MouseMotionListener and display
coordinates as well
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
Label l;
Color c=Color.RED;
Drow()
l=new Label();
l.setBounds(20,40,100,20);
add(l);
addMouseMotionListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
65
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
l.setText("X="+e.getX()+",Y="+e.getY());
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
l.setText("X="+e.getX()+",Y="+e.getY());
new Drow();
66
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
import java.awt.event.*;
MouseListenerExample2()
addMouseListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
new MouseListenerExample2();
67
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
68
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener
{
private TextField tfMousex;
private TextField tfMousey;
public MouseEventDemo()
{
setLayout(new FlowLayout());
tfMousex = new TextField(10);
tfMousex.setEditable(false);
add(tfMousex);
addMouseListener(this);
setSize(400,400);
setTitle("MouseDemo");
setVisible(true);
}
public void mouseClicked(MouseEvent me)
{
tfMousex.setText(me.getX()+" ");
tfMousey.setText(me.getY()+" ");
}
public void mousePressed(MouseEvent evt)
{
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseEntered(MouseEvent evt)
{
69
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
}
public void mouseExited(MouseEvent evt)
{
}
public static void main(String args[])
{
new MouseEventDemo();
}
}
70
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
AppletEg.java
import java.applet.Applet;
import java.awt.Graphics;
public class AppletEg extends Applet
{
public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}
public static void main(String args[])
{
new AppletEg ();
}
}
Myapplet.html
<html>
<body>
<applet code=" AppletEg.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>
71
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
import java.awt.*;
import java.applet.*;
public class House extends Applet
{
public void paint(Graphics g)
{
int [] x = {150, 300, 225};
int [] y = {150, 150, 25};
g.drawRect(150, 150, 150, 200);
g.drawRect(200, 200, 50, 150);
g.drawOval(200, 75, 50, 50);
g.drawPolygon(x, y, 3);
}
}
/*
<applet code="House.class" width=400 height=450></applet>
*/
72
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
73
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
3 Traffic signal
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet;
74
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
g.setColor(colourNum == 1? Color.red:Color.red.darker().darker());
g.fillOval (30, 40, 20, 20); // red light
g.setColor(colourNum == 2? Color.yellow : Color.yellow.darker().darker());
g.fillOval (30, 70, 20, 20); // yello light
g.setColor(colourNum == 3? Color.green : Color.green.darker().darker());
g.fillOval (30, 100, 20, 20); // green light
}
public void actionPerformed (ActionEvent evt)
{
if (evt.getSource() ==bttn1)
colourNum = 1;
else if (evt.getSource()== bttn2)
colourNum = 2;
else
colourNum = 3;
repaint ();
}
}
/*
<html>
<head>
<title></title>
</head>
<body>
75
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
<p>Traffic Applet
<applet code="Signal.class" width=300 height=200>
</applet>
</body>
</html>
*/
76
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
a) Develop an application with a Menu File and two items color and font. The submenu
of the menu item color will contain different colors which when selected should
change the background of the applet. The submenu of the menu item font should
contain the list of fonts. Create a Text Field in the center of the container. When the
font is selected from the font list of the menu, the Text Field text should be appeared
in the font.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class menu extends JFrame implements ActionListener
{
JMenuBar menubar;
TextField t;
Font f=new Font("Arial Black",Font.BOLD,35);
public menu()
{
//getContentPane().setBackground(new Color(240,175,126));
setLayout(null);
JPanel j=new JPanel();
t=new TextField();
add(t);
t.setBounds(100,180,200,100);
menubar=new JMenuBar();
setJMenuBar(menubar);
JMenu admin=new JMenu("COLOR");
menubar.add(admin);
JMenuItem red=new JMenuItem("RED");
red.addActionListener(this);
JMenuItem blue=new JMenuItem("BLUE");
blue.addActionListener(this);
JMenuItem green=new JMenuItem("GREEN");
green.addActionListener(this);
77
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
admin.add(red);
admin.add(blue);
admin.add(green);
JMenu font=new JMenu("FONT");
menubar.add(font);
JMenuItem tnr=new JMenuItem("ARIAL BLACK");
tnr.addActionListener(this);
font.add(tnr);
setVisible(true);
setSize(1500,1500);
setTitle("ASSIGNMENT 4");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
String cmp=(String)ae.getActionCommand();
if(cmp.equals("ARIAL BLACK"))
{
t.setFont(f);
}
}
public static void main(String args[])
{
new menu();
}
}
78
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
b) Create one registration form using Frame having fields Username, password, mobile
Number, address, state, gender on click event all selected data should appear in Label.
79
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
import java.awt.*;
import java.awt.event.*;
public class Form extends Frame implements ActionListener
{
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18;
TextField t1,t2,t3,t4,t5,t6;
Button b1,b2;
Form()
{
setTitle("ASSIGNMENT 4");
l1=new Label("USERNAME");
l2=new Label("PASSWORD");
l3=new Label("MOBILE NUMBER");
l4=new Label("ADDRESS");
l5=new Label("STATE");
l6=new Label("GENDER");
l7=new Label("USERNAME");
l8=new Label("PASSWORD");
l9=new Label("MOBILE NUMBER");
l10=new Label("ADDRESS");
l11=new Label("STATE");
l12=new Label("GENDER");
l13=new Label(" ");
l14=new Label(" ");
l15=new Label(" ");
l16=new Label(" ");
l17=new Label(" ");
l18=new Label(" ");
t1=new TextField();
t2=new TextField();
t3=new TextField();
t4=new TextField();
t5=new TextField();
t6=new TextField();
b1=new Button("SUBMIT");
b2=new Button("EXIT");
80
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
add(l10);
add(l11);
add(l12);
add(l13);
add(l14);
add(l15);
add(l16);
add(l17);
add(l18);
add(t1);
add(t2);
add(t3);
add(t4);
add(t5);
add(t6);
add(b1);
add(b2);
l1.setBounds(50,50,80,30);
l2.setBounds(50,100,80,30);
l3.setBounds(50,150,80,30);
l4.setBounds(50,200,80,30);
l5.setBounds(50,250,80,30);
l6.setBounds(50,300,80,30);
t1.setBounds(180,50,80,30);
t2.setBounds(180,100,80,30);
t3.setBounds(180,150,80,30);
t4.setBounds(180,200,80,30);
t5.setBounds(180,250,80,30);
81
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
t6.setBounds(180,300,80,30);
b1.setBounds(180,350,80,30);
b2.setBounds(300,350,80,30);
l7.setBounds(50,400,80,30);
l8.setBounds(50,450,80,30);
l9.setBounds(50,500,80,30);
l10.setBounds(50,550,80,30);
l11.setBounds(50,600,80,30);
l12.setBounds(50,650,80,30);
l13.setBounds(180,400,80,30);
l14.setBounds(180,450,80,30);
l15.setBounds(180,500,80,30);
l16.setBounds(180,550,80,30);
l17.setBounds(180,600,80,30);
l18.setBounds(180,650,80,30);
b1.addActionListener(this);
b2.addActionListener(this);
setLayout(null);
setSize(800,800);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
l13.setText(t1.getText());
l14.setText(t2.getText());
l15.setText(t3.getText());
l16.setText(t4.getText());
l17.setText(t5.getText());
l18.setText(t6.getText());
}
}
public static void main(String args[])
{
Form f=new Form();
}
82
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
OUTPUT:
83
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
84
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
c) Write a program of card layout having 4 cards and display following components in
card scroll bar, list, check box and text area respectively.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements ActionListener
{
CardLayout card;
Container c;
JTextArea t;
JList l;
JScrollPane scroll;
CardLayoutExample()
{
c=getContentPane();
card=new CardLayout(10,10);
c.setLayout(card);
l=new JList();
t=new JTextArea();
scroll=new JScrollPane(c);
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100,100, 50,50);
c.add("d",l);c.add("g",scroll);c.add("e",t);c.add("f",checkbox1);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
85
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
OUTPUT:
import java.awt.event.*;
import javax.swing.*;
public class TableExample
{
JFrame f;
TableExample()
86
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
{
f=new JFrame();
String
data[][]={{"101","abc","24000"},{"102","pqr","25000"},{"103","mno","26000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String args[])
{
new TableExample();
}
}
OUTPUT:
87
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
showInputDialog()
showOptionDialog()
import java.awt.event.*;
import javax.swing.*;
88
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
}
if (e.getSource() == b2)
{
JOptionPane.showMessageDialog(this,"MESSAGE DIALOG");
}
if (e.getSource() == b3)
{
JOptionPane.showInputDialog(null,"ENTER VALUE ?","ASSIGNMENT 4",
JOptionPane.YES_NO_OPTION);
}
if (e.getSource() == b4)
{
JOptionPane.showConfirmDialog(null,"Do You Want To Exit ?","ASSIGNMENT
4", JOptionPane.YES_NO_OPTION);
}
}
public static void main(String args[])
{
new DialogExample();
}
}
OUTPUT:
89
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51
90