0% found this document useful (0 votes)
109 views29 pages

Java Lab Assignment 4

The document contains code for several Java applet and event handling programs: 1. An applet that sets the background color based on values from three scrollbars controlling red, green, and blue levels. 2. An applet that draws shapes on mouse drag and displays coordinates. 3. An applet that draws a circle when the mouse is clicked. 4. An applet that displays mouse click coordinates in text fields. 5. An applet example that passes a parameter to display a message. 6. Applet code to draw shapes like a house and traffic signal.

Uploaded by

SUNIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views29 pages

Java Lab Assignment 4

The document contains code for several Java applet and event handling programs: 1. An applet that sets the background color based on values from three scrollbars controlling red, green, and blue levels. 2. An applet that draws shapes on mouse drag and displays coordinates. 3. An applet that draws a circle when the mouse is clicked. 4. An applet that displays mouse click coordinates in text fields. 5. An applet example that passes a parameter to display a message. 6. Applet code to draw shapes like a house and traffic signal.

Uploaded by

SUNIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Name :- SUNIL KUMAR YADAV

Class :- MCA-I Div:- B


Roll N0 :-51

1. Study and implement Applet and event handling


a. Write a program for an applet to set the background color as per following
situation: Draw 3 scrollbar for color ranges from 0 to 255 for red, blue,
green. Change the color of background based on combination of these 3
colors.

import java.awt.*;

import java.awt.event.*;

class ColorScroll extends Frame implements AdjustmentListener

final Label label,label1,label2,label3,title;

final Scrollbar s1,s2,s3;

ColorScroll()

label = new Label();

label1 = new Label();

label2 = new Label();

label3 = new Label();

title = new Label();

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

public void adjustmentValueChanged(AdjustmentEvent e)

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

Color c1=new Color(R,0,0);

label.setBackground(c1);

Color c2=new Color(0,G,0);

label1.setBackground(c2);

Color c3=new Color(0,0,B);

label2.setBackground(c3);

Color c4=new Color(R,G,B);

label3.setBackground(c4);

public static void main(String args[]){ new ColorScroll(); } }

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;

public class Drow extends Frame implements 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);

public void mouseDragged(MouseEvent e)

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

public void mouseMoved(MouseEvent e)

l.setText("X="+e.getX()+",Y="+e.getY());

public static void main(String args[])

new Drow();

C . Write an applet program to draw circle whenever a mouse is clicked on Applet


import java.awt.*;

66
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51

import java.awt.event.*;

public class MouseListenerExample2 extends Frame implements MouseListener

MouseListenerExample2()

addMouseListener(this);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e)

Graphics g=getGraphics();

g.setColor(Color.BLUE);

g.fillOval(e.getX(),e.getY(),30,30);

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public static void main(String[] args)

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

d) Write a program to show coordinates of mouse clicked in following way.

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

tfMousey = new TextField(10);


tfMousey.setEditable(false);
add(tfMousey);

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

e) Write a program for passing parameters to an applet.

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

E) Write an applet to draw the following the following shapes:


1. House

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;

public class Signal extends Applet


implements ActionListener
{
int colourNum; //global variable which is responible for changing the light
Button bttn1 = new Button ("Stop Traffic");
Button bttn2 = new Button ("Caution");
Button bttn3 = new Button ("Proceed");
public void init ()
{
setBackground (Color.lightGray);
bttn1.addActionListener (this); // stop light
bttn2.addActionListener (this); // yellow light
bttn3.addActionListener (this); // green light
add(bttn1);
add(bttn2);
add(bttn3);
}
public void paint(Graphics g)// responsible for graphics 'within' the window
{
g.setColor (Color.black);

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

2. Program to create rich User interface using various swing component

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:

d) Design a swing application to demonstrate JTable, JScrollPane.

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:

e) Write a program to use following dialog boxes:


 showMessageDialog()
 showConfirmDialog.

87
Name :- SUNIL KUMAR YADAV
Class :- MCA-I Div:- B
Roll N0 :-51

 showInputDialog()
 showOptionDialog()

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

class DialogExample extends JFrame implements ActionListener


{
JButton b1,b2,b3,b4;
DialogExample()
{
setLayout(null);
b1 = new JButton("CONFIRM DIALOG");
b2 = new JButton("MESSAGE DIALOG");
b3 = new JButton("INPUT DIALOG");
b4 = new JButton("OPTION DIALOG");
b1.setBounds(130, 50, 200, 30);
b2.setBounds(130, 100, 200, 30);
b3.setBounds(130, 150, 200, 30);
b4.setBounds(130, 200, 200, 30);
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setSize(500,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
{
JOptionPane.showConfirmDialog(null,"Are you sure You want to Exit
?","ASSIGNMENT 4", JOptionPane.YES_NO_OPTION);

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

You might also like