applet_prog
E N D
Presentation Transcript
JAVA - APPLET PROGRAMS 1.Write a java program to draw a temple using applet and fill appropriate colors. import java.applet.*; import java.awt.*; public class temple extends Applet { int x[]={531,373,689}; int y[]={189,326,326}; public void paint(Graphics g) { g.setColor(Color.yellow); g.fillRect(420,326,225,204); g.setColor(Color.DARK_GRAY); g.fillRect(500,408,69,121); g.setColor(Color.orange); g.fillRect(529,126,63,33); g.setColor(Color.black); g.drawLine(529,129,531,190); g.setColor(Color.red); g.fillPolygon(x,y,3); } } 2.Write a java program to draw a smiley face using java applet. import java.applet.*; import java.awt.*; public class smile extends Applet { public void paint(Graphics g) { g.setColor(Color.yellow); g.fillOval(365,140,431,426); g.setColor(Color.black); g.fillOval(432,232,67,70); g.fillOval(639,227,67,70); g.drawArc(486,411,198,57,0,-180); } } 3.Write java program to make a sunset animation using applet. MIHIR SUDANI 1
JAVA - APPLET PROGRAMS import java.applet.*; import java.awt.*; public class Sunset extends Applet implements Runnable { int y=50; Thread t; public void init() { setBackground(Color.cyan); t = new Thread(this); t.start(); } public void run() { try { while(true) { y+=2; Thread.sleep(100); repaint(); } } catch(Exception e) { } } public void paint(Graphics g) { g.setColor(Color.orange); g.fillOval(700,y,159,158); if(y>=400) { g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); } g.setColor(Color.blue); g.fillRect(0,402,getWidth(),getHeight()); } } 4.Write a java program to make a bus and put an animation on it using applet. MIHIR SUDANI 2
JAVA - APPLET PROGRAMS import java.applet.*; import java.awt.*; public class bus extends Applet implements Runnable { Thread t1; int busX=16; int wheel1=35; int wheel2=417; int ang; public void init() { t1 = new Thread(this); t1.start(); } public void run() { try { while(true) { busX+=2; wheel1+=2; wheel2+=2; ang+=2; Thread.sleep(100); repaint(); } } catch(Exception e) { } } public void paint(Graphics g) { g.drawLine(0,291,getWidth(),289); g.setColor(Color.orange); g.fillRect(busX,44,529,192); g.setColor(Color.DARK_GRAY); g.fillArc(wheel1,205,82,85,ang,360); g.fillArc(wheel2,205,82,85,ang,360); } } MIHIR SUDANI 3
JAVA - APPLET PROGRAMS 5.Write java program to make windmill using applet. import java.applet.*; import java.awt.*; public class windmill extends Applet implements Runnable { int x1=546; int y1=340; int ang=0; Thread t1; public void init() { t1=new Thread(this); } public void start() { t1.start(); } public void run() { while(true) { try { ang+=5; Thread.sleep(100); repaint(); } catch(InterruptedException e) { } } } public void paint(Graphics g) { g.setColor(Color.orange); g.fillRect(630,420,31,228); g.setColor(Color.black); g.fillArc(x1,y1,203,203,0+ang,30); g.fillArc(x1,y1,203,203,120+ang,30); g.fillArc(x1,y1,203,203,240+ang,30); } } 6.Write a java applet program of bouncing ball. MIHIR SUDANI 4
JAVA - APPLET PROGRAMS import java.applet.*; import java.awt.*; /*<applet code="bounce" height=400 width=500></applet>*/ public class bounce extends Applet implements Runnable { int x=20,y=20; int a=1,b=1; Thread t1; public void init() { t1=new Thread(this); t1.start(); } public void run() { try { while(true) { } } catch(Exception e) { Thread.sleep(1); if(x+70==getWidth()) { a=-1; } if(x==0) { a=1; } if(y+70 == getHeight()) { b=-1; } if(y==0) { b=1; } x=x+a; y=y+b; repaint(); MIHIR SUDANI 5
JAVA - APPLET PROGRAMS } } public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(x,y,70,70); } } 7.Write a java program to make a digital clock using java applet. import java.util.*; import java.applet.*; import java.awt.*; import java.text.*; public class digital_clock extends Applet implements Runnable { Thread t1; public void init() { t1=new Thread(this); t1.start(); } public void run() { try { while(true) { Thread.sleep(1000); repaint(); } } catch(InterruptedException e) { } } public void paint(Graphics g) { g.setFont(new Font("Arial", Font.BOLD, 50)); Calendar cale=Calendar.getInstance(); SimpleDateFormat date=new SimpleDateFormat("hh:mm:ss"); String current_time=date.format(cale.getTime()); MIHIR SUDANI 6
JAVA - APPLET PROGRAMS g.drawString(current_time,300,300); } } 8.Write a java applet program in which you have to include airplane image with animation. import java.applet.*; import java.awt.*; public class img extends Applet implements Runnable { Image img; Thread t1; int x=0; public void init() { img = getImage(getCodeBase(),"filght.png") ; } public void start() { t1=new Thread(this); t1.start(); } public void run() { try { while(true) { x++; Thread.sleep(10); repaint(); } } catch(InterruptedException e) { } } public void paint(Graphics g) { g.drawImage(img,x,45,this); } } MIHIR SUDANI 7
JAVA - APPLET PROGRAMS 9.Write a java program to take three textbox in which accept values in textbox1 and textbox2 and print sum in textbox3 when you click on button. import java.applet.*; import java.awt.*; import java.awt.event.*; /*<applet code="form1" height=1000 width=1000></applet>*/ public class form1 extends Applet implements ActionListener { Label l1,l2,ans; TextField t1,t2,t3; Button b1; int n1,n2,n3; public void init() { l1=new Label("Num 1 : "); t1=new TextField(10); l2=new Label("Num 2 : "); t2=new TextField(10); ans=new Label("Answer :: "); t3=new TextField(10); b1=new Button("Click to get answer"); add(l1); add(t1); add(l2); add(t2); add(ans); add(t3); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { try { n1 = Integer.parseInt(t1.getText()); n2 = Integer.parseInt(t2.getText()); n3 = n1 + n2; t3.setText(String.valueOf(n3)); } catch(NumberFormatException e) { System.out.println("please Enter values properly !!!"); } } MIHIR SUDANI 8
JAVA - APPLET PROGRAMS } 10.Write a java applet program in which includes three button red,green and blue,if you click on red then background color w be red,if you click on green then background color should be green and blue then bacjground should be blue. import java.applet.*; import java.awt.*; import java.awt.event.*; /*<applet code="light" height=400 width=400></applet>*/ public class light extends Applet implements ActionListener { Button b1,b2,b3; String str; public void init() { b1=new Button("red"); b2=new Button("green"); b3=new Button("blue"); add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae) { str=ae.getActionCommand(); if(str.equals("red")) { MIHIR SUDANI 9
JAVA - APPLET PROGRAMS } setBackground(Color.red); } else if(str.equals("green")) { setBackground(Color.green); } else { setBackground(Color.blue); } } MIHIR SUDANI10