Java Applet
Java Applet
Java Applet
AIM : Using applet scroll a message from right to left across the applets window.
import java.awt.*;
import java.applet.*;
/*<applet code="SimpBa" width=500 height=500>
</applet>
*/
public class SimpBa extends Applet implements Runnable
{
String msg="This is a scrolling text....:)";
Thread t=null;
int state;
Font f;
boolean stopflag;
public void init()
{
f=new Font("Algerian",Font.BOLD,48);
Color t=new Color(78,65,58);
setBackground(t);
setForeground(Color.white);
}
public void start()
{
t=new Thread(this);
stopflag=false;
t.start();
}
public void run()
{
char ch;
for(;;)
{
try
{
repaint();
Thread.sleep(250);
ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg+=ch;
if(stopflag)
break;
}catch(InterruptedException e){}
}
}
public void stop()
{
stopflag=true;
t=null;
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString(msg,10,250);
D:\>cd s
D:\s>javac SimpBa.java
D:\s>appletviewer SimpBa.java
PROGRAM 27
menu
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="MenuB" width=600 height=600></applet>*/
public class MenuB extends Applet implements ActionListener
{
Frame f;
MenuBar mb;
Menu m1,m2,m3,me1;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8;
public void init()
{
f=new Frame("Menu");
f.setVisible(true);
f.setSize(600,600);
mb=new MenuBar();
f.setMenuBar(mb);
m1=new Menu("File");
mi1=new MenuItem("New");
mb.add(m1);
m1.add(mi1);
mi2=new MenuItem("Open");
mi2.addActionListener(this);
m1.add(mi2);
m1.addSeparator();
me1=new Menu("Save");
me1.add(mi3=new MenuItem("Save As"));
m1.add(me1);
m2=new Menu("Edit");
mb.add(m2);
mi4=new MenuItem("Cut");
m2.add(mi4);
m2.addSeparator();
mi5=new MenuItem("Copy");
m2.add(mi5);
m2.addSeparator();
mi6=new MenuItem("Paste");
m2.add(mi6);
PopupMenu p=new PopupMenu();
p.add(new MenuItem("Cut"));
p.add(new MenuItem("Copy"));
p.add(new MenuItem("Paste"));
f.add(p);
p.show(f,200,200);
m3=new Menu("Help");
mb.add(m3);
mi7=new CheckboxMenuItem("Help Topics");
m3.add(mi7);
mi8=new CheckboxMenuItem("About Applet");
m3.add(mi8);
}
public void start()
{
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Open")
{
FileDialog fd=new FileDialog(f,"FileDialog");
fd.setVisible(true);
}
}
public void stop()
{
f.setVisible(false);
}
}
OUTPUT :
PROGRAM NO: 36
DATE:
AIM:
Using JavaScript
a)
b)
c)
d)
PROGRAM
a) Create popup boxes
<html>
<title>popup</title>
<head>
<script language="javascript">
function disp_alert()
{
alert("Hello again! This is how we add line breaks to an alert box")
}
function disp_confirm()
{
confirm("hello! this is confirm box .OK")
a=prompt("enter ur lucky no")
document.write(a)
}
</script>
</head>
<body bgcolor="orangepink">
<center>
<h1><b><i>alertbox</i></b></h1>
<h1>confirmbox</h1>
</center>
<form>
<input type="button" name="alert" onclick="disp_alert()" value=" alert box"/>
<input type="button" name="confirm" onclick="disp_confirm()" value="confirmbox"/>
<input type="button" name="prompt" onclick="disp_prompt()" value="promptbox"/>
</form>
</body>
</html>
OUTPUT
b) Display the no:of time you moved your mouse over the link.
<html>
<title>mousemove</title>
<head>
<script language="javascript">
count=0
</script>
<head>
<body >
<center><br><br><br><br>
<h1>
EVENT HANDLING
</h1>
</center>
OUTPUT
C)While clicking each buttons, change the background color of the webpage.
<html>
<head>
<title>colour</title>
</head>
<body>
<center>
Please click the button...
<form>
<input type="button" name="orange" value="ORANGE" onclick=document.bgColor="orange">
<br><br>
<input type="button" name="green" value="GREEN" onclick=document.bgColor="green">
<br><br>
<input type="button" name="yellow" value="YELLOW" onclick=document.bgColor="yellow">
<br><br>
</center>
</form>
</body>
</html>
OUTPUT
PROGRAM 26
HOUSE
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code=house width=800 height=800>
</applet>*/
public class house extends Applet implements AdjustmentListener
{
Scrollbar s1,s2,s3;
int r,g,b;
Color c;
public void init()
{
setLayout(null);
setBackground(Color.white);
s1=new Scrollbar(Scrollbar.HORIZONTAL);
s2=new Scrollbar(Scrollbar.HORIZONTAL);
s3=new Scrollbar(Scrollbar.VERTICAL);
add(s1);
add(s2);
add(s3);
s1.setBounds(130,250,70,40);
s2.setBounds(370,250,70,40);
s3.setBounds(260,280,70,150);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
s3.addAdjustmentListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
int x[]={56,140,430,520};
int y[]={201,74,74,201};
g.fillPolygon(x,y,4);
g.setColor(Color.pink);
g.drawRect(100,200,370,30);
g.setColor(c);
g.fillRect(100,200,370,300);
g.setColor(c);
g.fillRect(100,200,370,300);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
r=s1.getValue();
g=s2.getValue();
b=s3.getValue();
c=new Color(r,g,b);
repaint();
}
PROGRAM 19
import java.io.*;
class Stats<T extends Number>
{
T[] nums;
Stats (T [] o)
{
nums=o;
}
double average()
{
double sum=0.0;
for(int i=0;i<nums.length;i++)
sum+=nums[i].doubleValue();
return sum/nums.length;
}
}
class BoundsDemo
{
public static void main(String args[])
{
Integer inums[]={1,2,3,4,5};
Stats<Integer> iob=new Stats<Integer>(inums);
double v=iob.average();
System.out.println("iob average is"+v);
Double dnums[]={1.5,2.5,3.1,4.2,5.4};
D:\>cd shana
D:\shana>javac BoundsDemo.java
D:\shana>java BoundsDemo
iob average is3.0
dob average is3.3400000000000007
PROGRAM 20
import java.io.*;
class Stats<T extends Number>
{
T[] nums;
Stats (T [] o)
{
nums=o;
}
double average()
{
double sum=0.0;
for(int i=0;i<nums.length;i++)
sum+=nums[i].doubleValue();
return sum/nums.length;
}
boolean sameAvg(Stats<?> ob)
{
if(average()==ob.average())
return true;
return false;
}
}
class WildcardDemo
{
public static void main(String args[])
{
Integer inums[]={1,2,3,4,5};
Stats<Integer> iob=new Stats<Integer>(inums);
double v=iob.average();
System.out.println("dob average is"+v);
Double dnums[]={1.5,2.5,3.1,4.2,5.4};
Stats<Double> dob=new Stats<Double>(dnums);
double w=dob.average();
System.out.println("dob average is"+w);
Float fnums[]={1.0F,2.0F,3.0F,4.0F,5.0F};
Stats<Float> fob=new Stats<Float>(fnums);
double x=fob.average();
System.out.println("dob average is"+x);
System.out.println("Averages of iob and dob:");
if(iob.sameAvg(dob))
System.out.println("are the same");
else
System.out.println("differ");
System.out.println("Averages of iob and fob:");
if(iob.sameAvg(fob))
System.out.println("are the same");
else
System.out.println("differ");
}
}
output
D:\shana>javac WildcardDemo.java
D:\shana>java WildcardDemo
dob average is3.0
dob average is3.3400000000000007
dob average is3.0
Averages of iob and dob:
differ
Averages of iob and fob:
are the same
PROGRAM 22
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="MouseEvents" width=300 height=100>
</applet>*/
public class MouseEvents extends Applet implements MouseListener,
MouseMotionListener
{
String msg= "";
int mouseX = 0, mouseY = 0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Clicked.";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Entered.";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Exited.";
repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Pressed.";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Released.";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX=0;
mouseY = 10;
msg = "Mouse Dragged.";
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Moving mouse at " +me.getX() + ","+me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}
PROGRAM 23
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="key" width=300 height=300>
</applet>*/
public class key extends Applet implements KeyListener
{
String msg="";
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Pressed");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Released");
}
public void keyTyped(KeyEvent ke)
{
msg=msg+ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,10,10);
}
}
PROGRAM 37
import java.net.*;
import java.io.*;
class DataClient
{
public static DatagramSocket ds;
public static int clientport=9999,serverport=10000;
public static void main(String st[])throws Exception
{
byte buffer[]=new byte[2300];
ds=new DatagramSocket(clientport);
System.out.println("client is waiting ");
DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
while(true)
{
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println(str);
}
}
}
import java.net.*;
import java.io.*;
class DataServer
{
public static DatagramSocket ds;
public static int clientport=9999,serverport=10000;
public static void main(String st[])throws Exception
{
byte buffer[]=new byte[2300];
ds=new DatagramSocket(serverport);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter text");
InetAddress ia=InetAddress.getByName("localhost");
while(true)
{
String s=br.readLine();
if((s==null)||s.equalsIgnoreCase("end"))
{
break;
}
buffer=s.getBytes();
ds.send(new DatagramPacket(buffer,buffer.length,ia,clientport));
}
}
}
OUTPUT :
C:\Documents and Settings\user>d:
D:\>cd shana
D:\shana>javac DataServer.java
D:\shana>java DataServer
Enter text
Hello
D:\shana>javac DataClient.java
D:\shana>java DataClient
client is waiting
Hello