Vous êtes sur la page 1sur 12

Program : 24 Aim: Write a program to print hello in an applet. import java.io.*; import java.applet.*; import java.awt.

*; /* <applet code="hello" width = 100 height = 100> </applet> */ public class hello extends Applet { String msg; public void paint(Graphics g) { g.drawString(msg,50,50); } } HTML File <html> <head> <title> hello world example </title> </head> <body> <applet code = "hello.class" width = 100 height = 100> </applet> </body> </html> OUTPUT

Date: 15.11.2012

Program: 25 Aim: Write a program to illustrate life cycle of applets import java.io.*; import java.applet.*; import java.awt.*; /* <applet code="hello" width = 100 height = 100> </applet> */ public class hello extends Applet { String msg ; public void init() { msg = " Init " ; } public void start() { msg = "Start"; repaint(); } public void stop() { msg = "Stop"; repaint(); } public void destroy() { msg = "destroy"; repaint(); } public void paint(Graphics g) { g.drawString(msg,50,50); } }

Date: 15.11.2012

HTML File <html> <head> <title> hello world example </title> </head> <body> <applet code = "hello.class" width = 100 height = 100> </applet> </body> </html>

OUTPUT

Program: 26 Date: 15.11.2012 Aim: Write a program to display a color bar having all colors on the screen using applets. import java.io.*; import java.applet.*; import java.awt.*; /* <applet code="hello" width = 100 height = 100> </applet> */ public class hello extends Applet { Color colors[]={ Color.blue, Color.cyan, Color.yellow, Color.red, Color.darkGray, Color.gray, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.white, Color.black}; public void paint(Graphics g) { Dimension d = getSize(); int x = d.width/colors.length; setBackground(Color.lightGray); for (int i=0; i<colors.length ; i++ ) { g.setColor(colors[i]); g.fillRect(i*x, 0, (i+1) * x, d.height); } } } HTML Code <html> <head> <title> ques 25 </title> </head> <body> <applet code = "hello.class" width = 600 height = 300> </applet> </body> </html>

Output

Program: 27 Aim: Write a program to create a applet using a moving banner. import java.io.*; import java.applet.*; import java.awt.*; /* <applet code="hello" width = 100 height = 100> </applet> */ public class hello extends Applet implements Runnable { String msg=" A Simple Moving Banner. "; Thread t = null; int state; boolean stopflag; public void init() { setForeground(Color.black); } 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=msg+ch; if(stopflag) break; }catch(InterruptedException e) {} } }

Date: 15.11.2012

public void stop() { stopflag = true; t = null; } public void paint(Graphics g) { g.drawString(msg,50,30); showStatus("Simple Banner"); } } Output :

Program: 28 Aim: Write a program to create a calculator. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class calc implements ActionListener { int c,n; String s1,s2,s3,s4,s5; JButton sum,sub,mul,div,eq; JButton one,two,three,four,five,six,seven,eight,nine,zero; JTextField jtf; Frame f; Panel p; GridLayout g1; calc() { f = new Frame("Calculator"); p = new Panel(); sum = new JButton("+"); sum.addActionListener(this); sub = new JButton("-"); sub.addActionListener(this); mul = new JButton("x"); mul.addActionListener(this); div = new JButton("/"); div.addActionListener(this); eq = new JButton("="); eq.addActionListener(this); zero = new JButton("0"); zero.addActionListener(this); one = new JButton("1"); one.addActionListener(this); two = new JButton("2"); two.addActionListener(this); three = new JButton("3"); three.addActionListener(this); four = new JButton("4"); four.addActionListener(this); five = new JButton("5");

Date: 15.11.2012

five.addActionListener(this); six = new JButton("6"); six.addActionListener(this); seven = new JButton("7"); seven.addActionListener(this); eight = new JButton("8"); eight.addActionListener(this); nine = new JButton("9"); nine.addActionListener(this); jtf = new JTextField(20); p.add(jtf); jtf.setBackground(Color.yellow); g1 = new GridLayout(4,4,10,20); p.setLayout(g1); p.add(one); p.add(two); .add(three); p.add(four); p.add(five); p.add(six); p.add(seven); p.add(eight); p.add(nine); p.add(zero); p.add(sum); p.add(sub); p.add(mul); p.add(div); p.add(eq); f.add(p); f.setSize(100,200); f.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==zero) { s3 = jtf.getText(); s4 = "0"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()==one) { s3 = jtf.getText(); s4 = "1"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()==two) { s3 = jtf.getText(); s4 = "2"; s5 = s3 + s4; jtf.setText(s5); }

if(e.getSource()==three) { s3 = jtf.getText(); s4="3"; s5=s3+s4; jtf.setText(s5); } if(e.getSource()==four) { s3 = jtf.getText(); s4 = "4"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()== five) { s3 = jtf.getText(); s4 = "5"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()== six) { s3 = jtf.getText(); s4 = "6"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()==seven) { s3 = jtf.getText(); s4 = "7"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()==eight) { s3 = jtf.getText(); s4 = "8"; s5 = s3 + s4; jtf.setText(s5); } if(e.getSource()==nine) { s3 = jtf.getText(); s4 = "9"; s5 = s3 + s4;

jtf.setText(s5); } if(e.getSource()== sum) { s1 = jtf.getText(); jtf.setText(""); c=1; } if(e.getSource()== sub) { s1 = jtf.getText(); jtf.setText(""); c=2; } if(e.getSource()== mul) { s1 = jtf.getText(); jtf.setText(""); c=3; } if(e.getSource()== div) { s1 = jtf.getText(); jtf.setText(""); c=4; } if(e.getSource()==eq) { s2 = jtf.getText(); if(c==1) { n=Integer.parseInt(s1)+Integer.parseInt(s2); jtf.setText(String.valueOf(n)); } else if(c==2) { n=Integer.parseInt(s1)-Integer.parseInt(s2); jtf.setText(String.valueOf(n)); }

else if(c==3) { n=Integer.parseInt(s1)*Integer.parseInt(s2); jtf.setText(String.valueOf(n)); } else if(c==4) { n=Integer.parseInt(s1)/Integer.parseInt(s2); jtf.setText(String.valueOf(n)); } } } public static void main(String args[]) { calc c=new calc(); } } Output :

Vous aimerez peut-être aussi