java basic applet programs

Document Sample
java basic applet programs Powered By Docstoc
					1)Write an application that draws a rectangle the dimension of the applet should be 500X300 pixels. The rectangle should be centered in the applet is have a width & height 300X200 pixels. import java.applet.*; import java.awt.*; /* <applet code="ass8q1" width=500 height=300> </applet> */ public class ass8q1 extends Applet { public void paint(Graphics g) { g.drawRect(100,50,300,200); } }

Output :-

2) Write an applet that contains three checkbox & a 30X30 pixels canvas. The 3checkbox should be labeled as red, green, blue. The selection of the checkboxes determine the color of the canvas for example if the user select both red and blue the canvas should be purple. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ass8q2" width=300 height=300> </applet> */ public class ass8q2 extends Applet implements ItemListener { Checkbox red,blue,green; Canvas can; public void init() { red=new Checkbox("Red",false); blue=new Checkbox("Blue",false); green=new Checkbox("Green",false); add(red); add(blue); add(green); red.addItemListener(this); blue.addItemListener(this); green.addItemListener(this); can=new Canvas(); can.setSize(30,30); add(can); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { int i,j,k; if(red.getState()) i=255; else i=0; if(blue.getState()) j=255; else

j=0; if(green.getState()) k=255; else k=0; Color c= new Color(i,k,j); g.setColor(c); can.setBackground(c); can.repaint(); } }

Output :-

3) Write an application that create a frame with one button each time button is pressed a model dialog box appears that displays the current date. import java.awt.*; import java.awt.event.*; import java.util.*; class ass8q3 extends Frame { String msg=""; Button b; Frame f; ass8q3() { setLayout(new FlowLayout()); setTitle("Click to get date"); b=new Button("Date"); add(b); b.addActionListener(new MH()); addWindowListener(new WH()); } class WH extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } class MH implements ActionListener { public void actionPerformed(ActionEvent ae) { GregorianCalendar gc=new GregorianCalendar(); if(ae.getActionCommand().equals("Date")) { ass8q3 ob=new ass8q3(); msg=""+gc.get(Calendar.MONTH)+"/"+gc.get(Calendar.DATE)+"/"+gc.get(Calendar.Y EAR); dialogbox d=new dialogbox(ob,"Masssage Dialog",true,msg);; } } } public static void main(String arg[]) { Frame m=new ass8q3(); m.setBounds(1,1,300,300);

m.setVisible(true); } } class dialogbox extends Dialog { dialogbox(Frame p,String ti,boolean m,String msg) { super(p,ti,m); super.setSize(100,100); Label lab=new Label(msg); add(lab); addWindowListener(new WH()); } class WH extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); } } }

Output :-

4) Create an application that displays a frame with menu-bar when a user select any menu-bar or menu-item display that selection on a text area in center of the frame. import java.awt.*; import java.awt.event.*; class menudemo extends Frame { String msg=""; MenuBar mbar; Menu file; MenuItem nw,open,close,quit,minimize; Menu edit; MenuItem cut,copy,paste; Menu special; MenuItem first,second,third; menudemo() { setTitle("Sample"); mbar=new MenuBar(); setLayout(new FlowLayout()); setMenuBar(mbar); file=new Menu("File"); nw=new MenuItem("New"); open=new MenuItem("OPen"); close=new MenuItem("Close"); quit=new MenuItem("Quit"); minimize=new MenuItem("Minimize"); file.add(nw); file.add(open); file.add(minimize); file.add(close); file.add(quit); mbar.add(file); nw.addActionListener(new MH()); open.addActionListener(new MH()); close.addActionListener(new MH()); quit.addActionListener(new MH()); close.addActionListener(new MH()); cut= new MenuItem("Cut"); copy= new MenuItem("Copy"); paste= new MenuItem("Paste"); edit=new Menu("Edit");

edit.add(cut); edit.add(copy); edit.add(paste); cut.addActionListener(new MH()); copy.addActionListener(new MH()); paste.addActionListener(new MH()); first= new MenuItem("First"); second= new MenuItem("Second"); third= new MenuItem("Third"); special= new Menu("Special"); mbar.add(edit); special.add(first); special.add(second); special.add(third); edit.add(special); first.addActionListener(new MH()); second.addActionListener(new MH()); third.addActionListener(new MH()); addWindowListener(new WH()); } class WH extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } class MH implements ActionListener { public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("New")) msg="New"; if(ae.getActionCommand().equals("Open")) msg="Open"; if(ae.getActionCommand().equals("Minimize")) msg="Minimize"; if(ae.getActionCommand().equals("Close")) msg="Close"; if(ae.getActionCommand().equals("Quit")) msg="Quit"; if(ae.getActionCommand().equals("Cut"))

msg="Cut"; if(ae.getActionCommand().equals("Copy")) msg="Copy"; if(ae.getActionCommand().equals("Paste")) msg="Paste"; if(ae.getActionCommand().equals("First")) msg="First"; if(ae.getActionCommand().equals("Second")) msg="Second"; if(ae.getActionCommand().equals("Third")) msg="Third"; repaint(); } } public void paint(Graphics g) { g.drawString(msg,150,200); } public static void main(String arg[]) { Frame m=new menudemo(); m.setBounds(1,1,300,300); m.setVisible(true); } } Output :-

Shared By:
Description: java basic applet programs help you to learn applet programming in java and makind softewares.