User:Bhushanmehta/Learn java

About Advance Java Technology is:- Web Application Technologies the hypertext transfer protocol(HTTP) is similar to many protocol for example Simple Mail Transfer Protocol(SMTP). and file transfer protocol(FTP), because http is used to transfer data from server to client. one fundamental difference between most other internet protocol and http is that the typical internet protocol supports several request as per connection.this means that with HTTP the client connects to the several to retrieve one file and then disconnects.

SOME EXAMPLES WITH PROGRAMS

Before you start on java to control panel, system, advanced, environment variable, under system variables select path edit, and the end of the path after a semicolon add C:\jdk1.3\bin Ok to close. Then create a new variable CLASSPATH by clicking on new C:\jdk1.3\lib; c:\clb; Clb stands for your directory name in which you are saving your java programs.

Q1. A_1 Define a class to download a file from the Internet and either copy it as a file on the local machine, or output it to the screen. import java.net.*; import java.io.*;

class A_1_Download {	public static void main(String args[])throws Exception {		int b;		char c; if(args.length==0) {			throw new Exception("Invalid Number of argument"); }		URL u=new URL(args[0]); InputStream is=u.openStream; OutputStream os; if(args.length==1) {			while ((b=is.read)!=-1) {					System.out.print((char)b); }		}		else {	File f2=new File(args[1]); if(f2.exists==true) {				System.out.println("This file exists"); System.exit(0); }			else {os=new FileOutputStream(f2); while ((b=is.read)!=-1) {					os.write(b); }			}		}	}//main }//class

Go to dos prompt Go to your directory javac A_1_Download.java java A_1_Download

Results in error

java A_1_Download http://localhost/localstart.asp results in the file being displayed on the screen java A_1_Download http://localhost/localstart.asp abc.txt Will result the file being copied to abc.txt.

Q2. A_2. Define a class that displays information about a file URL like its type, encoding, length, dates of creation, last modification and expiry. Additionally the class should display the request method, response message and the response code for a Web URL.

import java.net.*; import java.util.Date; class A_2_URLDemo {		long d;	public static void main(String args[])throws Exception {		URL u=new URL(args[0]); URLConnection uc=u.openConnection; HttpURLConnection huc=(HttpURLConnection)uc;

Date d=new Date(uc.getDate); System.out.println("File Name = "+u.getFile); System.out.println("Host Name= "+u.getHost); System.out.println("Path Name= "+u.getPath); System.out.println("Port Name= "+u.getPort); System.out.println("Protocol Name= "+u.getProtocol); System.out.println("Reference Name= "+u.getRef); System.out.println("User Info= "+u.getUserInfo); System.out.println("Content Name= "+u.getContent); System.out.println("Authority Name= "+u.getAuthority); System.out.println("Content Type= "+uc.getContentType); System.out.println("Length= "+uc.getContentLength); System.out.println("Expiration Date= "+uc.getExpiration); System.out.println("Encoding Type= "+uc.getContentEncoding); System.out.println("Last Modified Date= "+uc.getLastModified); System.out.println("Date= "+d.toString);

System.out.println("Request Method= "+huc.getRequestMethod); System.out.println("Response Message= "+huc.getResponseMessage); System.out.println("Response Code= "+huc.getResponseCode); } }

run it as java A_2_URLDemo	http://localhost/localstart.asp

Q3. A_3 Build an application that sends a datagram to a server each time that it is started or stopped. Further build a server application that receives the sent datagram and prints it out. import java.net.*; import java.io.*;

class A_3_DataGramServer {	public static DatagramSocket ds; public static byte buffer[]=new byte[1024]; public static int cp=1510,sp=1511; public static void main(String args[])throws Exception {		ds=new DatagramSocket(sp); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress ia=InetAddress.getByName(args[0]); while(true) {			String str=br.readLine; buffer=str.getBytes; ds.send(new DatagramPacket(buffer,str.length,ia,cp)); if(str.length==0) {				ds.close; break; }		}	}//main }//class

import java.net.*; import java.io.*;

class A_3_DataGramClient {	public static DatagramSocket ds; public static byte buffer[]=new byte[1024]; public static int cp=1510,sp=1511; public static void main(String args[])throws Exception {		ds=new DatagramSocket(cp); System.out.print("Client is waiting for server to send data...."); while(true) {			DatagramPacket dp=new DatagramPacket(buffer,buffer.length); ds.receive(dp); String str=new String(dp.getData,0,dp.getLength); if(dp.getLength==0) {				break; }			System.out.println(str); }	}//main }//class

Run the server program first. In another command prompt window run the client with argument localhost when you type something it will appear in the server window. To end press enter twice.

Q4. A_4 Write a simple server that reports the current time (in textual form) to any client that connects. This server should simply output the current time and close the connection, without reading anything from the client. You need to choose a port number that your service listens on. import java.io.*; import java.net.*; import java.util.*;

class A_4_TimeServer {	public static void main(String args[])throws Exception { 		ServerSocket s=new ServerSocket(1234); Socket c=s.accept;

Calendar calendar = new GregorianCalendar; PrintWriter out=new PrintWriter(c.getOutputStream);

out.println(new Date); out.println("Time :"); out.print(calendar.get(Calendar.HOUR)+"HRS."+calendar.get(Calendar.MINUTE)+"MIN."+calendar.get(Ca lendar.SECOND)+"SEC");

out.flush; s.close; c.close; } }

import java.net.*;

import java.io.*;

class A_4_TimeClient {	public static void main(String args[])throws Exception {	 	Socket c=new Socket(InetAddress.getLocalHost,1234); BufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream)); String userInput; while((userInput=br.readLine)!=null) {			System.out.println(userInput); }		c.close; } }

Run the server program then client program in another window. Out put will come in the client window

Q5. A_5. Write a Java program for datagram communication between two client machines using Unreliable Datagram Protocol. Create the user interface with the help of SWING. This program will not work with network. So you can try it onl in lab import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; import java.net.*;

public class A_5_ChatApp extends JFrame implements ActionListener,Runnable {	JLabel lblName; JTextField txtSysName; JButton cmdSend; JTextArea txtMsg; Thread t=new Thread(this); JTextArea txtReceive; JButton cmdClear; public static DatagramSocket ds; public static DatagramSocket dsServer; public static byte buffer[]=new byte[1024]; public static int cp=1510,sp=1511; InetAddress ia; InetAddress a;	BufferedReader br; GridBagLayout gbl; GridBagConstraints gbc; public A_5_ChatApp {		gbl=new GridBagLayout; gbc=new GridBagConstraints; lblName=new JLabel("Plz Enter A Sys Name:"); txtSysName=new JTextField; txtMsg=new JTextArea(7,7); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp=new JScrollPane(txtMsg,v,h); cmdSend=new JButton("Send"); cmdSend.addActionListener(this); txtReceive=new JTextArea(7,7); int v1=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h1=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp1=new JScrollPane(txtReceive,v1,h1); cmdClear=new JButton("Clear"); cmdClear.addActionListener(this);

Container con=getContentPane; con.setLayout(new GridBagLayout); gbc.fill=GridBagConstraints.BOTH; gbc.weightx=1.0; gbc.insets=new Insets(10,10,10,10);

gbc.gridx=0;gbc.gridy=0; con.add(lblName,gbc); gbc.gridx=1;gbc.gridy=0; con.add(txtSysName,gbc); gbc.gridx=0;gbc.gridy=1; gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.weighty=1.0; gbc.gridheight=1; con.add(jsp,gbc);

gbc.weighty=0.0; gbc.gridx=0;gbc.gridy=2; gbc.gridheight=1; con.add(cmdSend,gbc);

gbc.weighty=1.0; gbc.gridx=0;gbc.gridy=3; gbc.gridheight=1; con.add(jsp1,gbc); gbc.weighty=0.0; gbc.gridx=0;gbc.gridy=4; con.add(cmdClear,gbc); try {	a=InetAddress.getLocalHost; ds=new DatagramSocket(cp); }		catch(Exception e){} try {			dsServer=new DatagramSocket(sp); }		catch(Exception e){} t.start; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

public void actionPerformed(ActionEvent ae) {		Object obj=ae.getSource; if (obj==cmdSend) {			try{ ia=InetAddress.getByName(txtSysName.getText); sendMsg; }			catch(Exception e){} }		if(obj==cmdClear) {			txtMsg.setText(""); txtReceive.setText(""); }	}

public void run {		while(true) {			try {			t.sleep(100); receiveMsg; }			catch(Exception e){} }	}	public void sendMsg {		try {		String str1,str2,str3; str1=txtMsg.getText; str2=a.getHostName; str3=str2+":"+"\t"+str1; buffer=str3.getBytes; ds.send(new DatagramPacket(buffer,str3.length,ia,sp)); }		catch(Exception e){} }

public void receiveMsg {	try {		String str,msg,main; DatagramPacket dp=new DatagramPacket(buffer,buffer.length); dsServer.receive(dp); msg=txtReceive.getText; str=new String(dp.getData,0,dp.getLength); main=msg+str+"\n"; txtReceive.setText(main); }		catch(Exception e){} }

public static void main(String args[]) {		A_5_ChatApp c=new A_5_ChatApp; c.setSize(650,650); c.setVisible(true); } }

Q6. B_1. Write a program to create a form to enter bio-data of student. Use various components such as JLabel, JButton, JTextField, JTextArea, JComboBox, JTable, JScrollPane, JOptionButton, and JCheckBox. Use GridBagLayout and GridBagConstraints to lay the components import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.Object;

public class B_1_BioData2 extends JFrame { int i;	int x=1970; JLabel lblm,lblName,lblAddress,lblTel,lblGender,lblHobbies,lblEQ,lblLang,lblDate; JTextField txtName,txtTel; JTextArea txtAddress; JCheckBox c1,c2,c3; JRadioButton r1,r2; JTable t1; JList l1; JButton cmdOk,cmdCancel; JComboBox date,month,year; int[] year1; GridBagLayout gb1=new GridBagLayout; GridBagConstraints gbc=new GridBagConstraints;

final String[] colHeads={"Year","Percenatage","Class"}; final Object[][] data={ {"2000","87%","S.S.C"}, {"2002","82%","H.S.C"}, {"2003","78%","F.Y.Bsc"}, {"2004","82%","S.Y.Bsc"}};

String[] listData={"English","Hindi","Marathi"}; String[] mon={"Jan","Feb","March","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"}; String[] dat={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20" ,"21","22","23","24","25","26","27","28","29","30","31"};

public B_1_BioData2 {		Container con=getContentPane; con.setLayout(gb1); lblm=new JLabel; lblName=new JLabel("Name"); lblAddress=new JLabel("Address"); lblTel=new JLabel("Tel No"); lblGender=new JLabel("Gender"); lblHobbies=new JLabel("Hobbies"); lblEQ=new JLabel("Educational Qualification"); lblLang=new JLabel("Language Known"); lblDate=new JLabel("DOB");

date=new JComboBox(dat); month=new JComboBox(mon); year=new JComboBox; for(i=1970;i<=2010;i++) {   		String str=Integer.toString(i); year.addItem((Object)str); }

txtName=new JTextField; txtTel=new JTextField;

txtAddress=new JTextArea; int v1=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h1=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane s1=new JScrollPane(txtAddress,v1,h1);

cmdOk=new JButton("OK"); ButtonListener1 b1=new ButtonListener1(this); cmdOk.addActionListener(b1); cmdCancel=new JButton("Cancel"); c1=new JCheckBox("Reading"); c2=new JCheckBox("Music"); c3=new JCheckBox("Movies"); ButtonGroup b=new ButtonGroup; r1=new JRadioButton("Male"); r2=new JRadioButton("Female"); b.add(r1); b.add(r2);

t1=new JTable(data,colHeads); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane s=new JScrollPane(t1,v,h);

l1=new JList(listData); l1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

//JPanel p=new JPanel(new FlowLayout(FlowLayout.LEFT)); //p.add(r1); //p.add(r2);

//JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT)); //p1.add(c1); //p1.add(c2); //p1.add(c3); gbc.insets=new Insets(10,10,10,10); gbc.weightx=1.0; gbc.fill=GridBagConstraints.HORIZONTAL; gbc.gridx=0; gbc.gridy=0; con.add(lblName,gbc);

gbc.gridx=1; gbc.gridy=0; con.add(txtName,gbc);

gbc.gridx=0; gbc.gridy=1; gbc.gridheight=2; con.add(lblAddress,gbc);

gbc.gridx=1; gbc.gridy=1; gbc.gridheight=2; gbc.weighty=1.0; gbc.fill=GridBagConstraints.BOTH; //gbc.gridwidth=gbc.REMAINDER; con.add(s1,gbc);

gbc.gridx=0; gbc.gridy=3; gbc.gridheight=1; gbc.weighty=0.0; con.add(lblTel,gbc);

gbc.gridx=1; gbc.gridy=3; con.add(txtTel,gbc);

gbc.gridx=0; gbc.gridy=4; con.add(lblGender,gbc);

gbc.gridx=1; gbc.gridy=4; con.add(r1,gbc);

gbc.gridx=2; gbc.gridy=4; con.add(r2,gbc);

gbc.gridx=0; gbc.gridy=5; con.add(lblDate,gbc);

gbc.gridx=1; gbc.gridy=5; con.add(date,gbc);

gbc.gridx=2; gbc.gridy=5; con.add(month,gbc);

gbc.gridx=3; gbc.gridy=5; con.add(year,gbc);

gbc.gridx=0; gbc.gridy=6; con.add(lblHobbies,gbc);

gbc.gridx=1; gbc.gridy=6; con.add(c1,gbc);

gbc.gridx=2; gbc.gridy=6; con.add(c2,gbc);

gbc.gridx=3; gbc.gridy=6; con.add(c3,gbc);

gbc.gridx=0; gbc.gridy=7;

con.add(lblEQ,gbc);

gbc.gridx=1; gbc.gridy=7; gbc.weighty=1.0; gbc.gridwidth=gbc.REMAINDER; con.add(s,gbc);

gbc.gridx=0; gbc.gridy=8; gbc.weighty=0.0; con.add(lblLang,gbc);

gbc.gridx=1; gbc.gridy=8; gbc.gridwidth=gbc.REMAINDER; con.add(l1,gbc); gbc.gridx=1; gbc.gridy=9; gbc.gridwidth=1; con.add(cmdOk,gbc);

gbc.gridx=2; gbc.gridy=9; con.add(cmdCancel,gbc); gbc.gridx=0; gbc.gridy=10; con.add(lblm,gbc);

} public static void main(String args[]) {	B_1_BioData2 b=new B_1_BioData2; b.setVisible(true); b.setSize(500,500); //addWindowListener(new WindowAdapter	//	{	//		public void windowClosing(WindowEvent e)	//	 	{	//			System.exit(0);	//	  	}	//	}); } }

class ButtonListener1 implements ActionListener {		B_1_BioData2 bd; public ButtonListener1(B_1_BioData2 bd) {		  this.bd=bd; }         	public void actionPerformed(ActionEvent e)         	{ String a,b,c; a=bd.txtName.getText; b=bd.txtTel.getText; c=bd.txtAddress.getText; if(a.equals("") || b.equals("") || c.equals("")) {      		   JOptionPane.showMessageDialog(bd, "Please enter all the values", "Alert",  JOptionPane.ERROR_MESSAGE); }		if((bd.r1.isSelected==false) && (bd.r2.isSelected==false)) {      		   JOptionPane.showMessageDialog(bd, "Please select the gender", "Alert",  JOptionPane.ERROR_MESSAGE); }

if((bd.c1.isSelected==false && bd.c2.isSelected==false && bd.c3.isSelected==false)) {		   JOptionPane.showMessageDialog(bd, "Please select the hobies", "Alert",  JOptionPane.ERROR_MESSAGE); }	  } 	}

Q7. B_2. Define a class that enables the drawing of freehand lines on a screen through mouse clicking and dragging. Use anonymous inner classes to implement event listeners. The drawing should be cleared when a key is pressed and the line color should be selectable. Define a test class to demonstrate the program. import java.awt.event.*; import java.awt.*; import javax.swing.*;

//

public class B_2_FreeHand extends JApplet {	int lastx,lasty,newx,newy; JButton b1=new JButton("Color Chooser"); JColorChooser c1= new JColorChooser; Graphics g;	Color ss; public void init {		FreeHandListener fhl=new FreeHandListener(this); g=getGraphics; JPanel jp=(JPanel)getContentPane; jp.setLayout(new FlowLayout); b1.addActionListener(fhl); jp.add(b1); addMouseListener(fhl); addMouseMotionListener(fhl); addKeyListener(fhl); } }// Class FH class FreeHandListener implements ActionListener,MouseMotionListener,MouseListener,KeyListener {		B_2_FreeHand fh;

public FreeHandListener(B_2_FreeHand fh) {			this.fh=fh; }		public void actionPerformed(ActionEvent e)		{ JDialog jd=JColorChooser.createDialog(fh,"Choose Color",true,fh.c1,new SetColor(fh),null); jd.setVisible(true); }

public class SetColor implements ActionListener {			B_2_FreeHand fh;

public SetColor(B_2_FreeHand fh) {				this.fh=fh; }			public void actionPerformed(ActionEvent e)			{ fh.ss=fh.c1.getColor; }		}// inner class public void mousePressed(MouseEvent e)		{ fh.lastx=e.getX; fh.lasty=e.getY; }		public void mouseDragged(MouseEvent e)		{ fh.g.setColor(fh.ss); fh.newx=e.getX; fh.newy=e.getY; fh.g.drawLine(fh.lastx,fh.lasty,fh.newx,fh.newy); fh.lastx=fh.newx; fh.lasty=fh.newy; }		public void keyPressed(KeyEvent e)		{ if(e.getKeyCode==KeyEvent.VK_C) fh.repaint; }		public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){}

public void mouseReleased(MouseEvent e)	{} public void mouseExited(MouseEvent e)	{} public void mouseClicked(MouseEvent e)	{} public void mouseMoved(MouseEvent e)	{} public void mouseEntered(MouseEvent e)	{} }//class fhl

//for running give the following command at dos prompt //appletviewer B_2_FreeHand.java Q8. B_3. Create a class called “ColouredCanvas” which extends Canvas and whose constructor takes three arguments, its color, width and height. When a “ColouredCanvas” is initialized, it should set its size and background color as per the arguments. Create a class which extents JApplet and adds to the Applet a “ColouredCanvas” of red color with size 50,100. import javax.swing.JApplet; import java.awt.*; import java.awt.event.*; //

class ColoredCanvas extends Canvas {	public ColoredCanvas(Color c,int w,int h) 	{ setSize(w,h); setBackground(c); } } public class B_3_MyColoredCanvas extends JApplet {	public void init {		ColoredCanvas cc=new ColoredCanvas(new Color(1,1,250),200,150); Container con=getContentPane; con.setLayout(new GridBagLayout); GridBagConstraints gbc=new GridBagConstraints; gbc.gridx=2; gbc.gridy=2; con.add(cc,gbc); } } //run the same way as before.

Q9. B_4 Create an applet that displays a small figure on the screen. When mouse is moved, determine whether the mouse is close to the position occupied by the figure. If so redraw the figure at a new position further away from the mouse. If the figure strikes a wall of the screen, cause it to bounce off the wall. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; //

public class B_4_CatchRect extends Applet {		int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5; int mx,my; public void init {			CatchRectListener crl=new CatchRectListener(this); addMouseMotionListener(crl); x1=50; y1=75; x2=80; y2=100; }	public void paint(Graphics g)	{ x3=x1-50; y3=y1-50; x4=x2+50; y4=y2+50;

g.drawRect(x5,y5,x2,y2); } }//class

class CatchRectListener implements MouseMotionListener {	B_4_CatchRect c;	public CatchRectListener(B_4_CatchRect c)	{ this.c=c; }	public void mouseMoved(MouseEvent e)	{ int newx,newy; c.mx=e.getX; c.my=e.getY; if (c.mxc.x3 || c.my>c.y3) {			c.x5=(int)(Math.random*500); c.y5=(int)(Math.random*500); //			g.drawRect(x3,y3,x4,y4); }		c.repaint; }	public void mouseDragged(MouseEvent e){} }//class

Q10. B_5 Create an application, which is a subclass of the JFrame class with a menu on the JFrame. One of the menu items is for the background color and the other is for the text color. The application contains a text component (any text component can be used) where user can type any text. When a user presses either of the menus mentioned above, three scroll bars are shown on the screen for red, green and blue color components. The user can adjust the positions of these scroll bars. On pressing the ‘ok’ button, the foreground or background of a text component on the frame changes according to the menu choice made earlier import java.awt.*; import java.awt.event.*; import javax.swing.*;

class B_5_MyFrame extends JFrame implements ActionListener, AdjustmentListener {

Container c;	JButton ok; JTextField tb; JPanel p;

JScrollBar r,g,b;

int rd,gr,bl; MenuBar mb; Menu m;	MenuItem m1,m2; boolean fore;

GridBagLayout gridbag; GridBagConstraints gc;

B_5_MyFrame {		super;

setTitle("Practical 3"); c=getContentPane;

mb= new MenuBar ; m=new Menu("Menu");

m1=new MenuItem("ForeGround "); m2=new MenuItem("BackGround"); m.add(m1); m.add(m2); mb.add(m); setMenuBar(mb);

m1.addActionListener(this); m2.addActionListener(this);

c.setLayout(new BorderLayout);

tb=new JTextField("text");

c.add(tb,BorderLayout.NORTH);

p=new JPanel; gridbag=new GridBagLayout ; gc=new GridBagConstraints ;

p.setLayout(gridbag);

gc.fill = GridBagConstraints.HORIZONTAL;

//gc.weightx = 1.0; //gc.ipady=40; gc.insets=new Insets(10,10,10,10);

r=new JScrollBar(Scrollbar.HORIZONTAL,0,1,0,255); r.addAdjustmentListener(this);

gc.gridwidth = GridBagConstraints.REMAINDER; gc.weightx = 1.0; gridbag.setConstraints(r,gc);

p.add(r);

g=new JScrollBar(Scrollbar.HORIZONTAL,0,1,0,255); g.addAdjustmentListener(this);

//	gc.weightx=GridBagConstraints.REMAINDER; gc.weightx = 0.0; gridbag.setConstraints(g,gc);

p.add(g);

b=new JScrollBar(Scrollbar.HORIZONTAL,0,1,0,255); b.addAdjustmentListener(this);

//		gc.weightx=GridBagConstraints.REMAINDER; gc.weightx = 0.0; gridbag.setConstraints(b,gc);

p.add(b);

ok= new JButton("ok"); ok.addActionListener(this);

c.add(ok,BorderLayout.SOUTH);

c.add(p,BorderLayout.CENTER);

p.setVisible(false); //pack; setVisible(true); setSize(200,200);

addWindowListener(new WindowAdapter{

public void windowClosing(WindowEvent we){ System.exit(0); }				});			}

public void actionPerformed(ActionEvent ae) {		if(ae.getSource==m1) {			p.setVisible(true); fore=true; //pack; }		if(ae.getSource==m2) {			p.setVisible(true); fore=false;

//pack;

}

if(ae.getSource==ok) {			if(fore){ tb.setForeground(new Color(rd,gr,bl));

}			else {			tb.setBackground(new Color(rd,gr,bl));

}

p.setVisible(false); }

}

public void 	 adjustmentValueChanged( AdjustmentEvent ae) {		if(ae.getSource==r) {			rd=r.getValue; }		if(ae.getSource==g) {			gr=g.getValue;

}		if(ae.getSource==b) {			bl=b.getValue;

}

//System.out.println(rd +" "+ gr +"  "+ bl);

}	public static void main(String str[])throws Exception {

B_5_MyFrame r =new B_5_MyFrame;

} }

Q11. C_1 Construct a simple calculator using the GrigBagLayout with minimum functionality. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class C_1_Calculator extends JPanel { GridBagConstraints gbl=new GridBagConstraints; JLabel calci=new JLabel("3.1415"); C_1_Calculator { setLayout(new GridBagLayout); gbl.weightx=1.0; gbl.weighty=1.0; gbl.fill=GridBagConstraints.BOTH; gbl.gridx=0; gbl.gridy=0; add(calci,gbl);

JPanel jp1=new JPanel; gbl.gridwidth=4; gbl.gridx=0; gbl.gridy=1; jp1.setLayout(new GridLayout); add(jp1,gbl); gbl.gridwidth=1; gbl.gridx=0; gbl.gridy=0; jp1.add(new JButton("C"),gbl); gbl.gridx=1; gbl.gridy=0; jp1.add(new JButton("%"),gbl); gbl.weightx =1.0; gbl.gridx=2; gbl.gridy=0; jp1.add(new JButton("+"),gbl); gbl.gridx=0; gbl.gridy=2; add(new JButton("7"),gbl); gbl.gridx=1; gbl.gridy=2; add(new JButton("8"),gbl); gbl.gridx=2; gbl.gridy=2; add(new JButton("9"),gbl); gbl.gridx=3; gbl.gridy=2; add(new JButton("-"),gbl); gbl.gridx=0; gbl.gridy=3; add(new JButton("4"),gbl); gbl.gridx=1; gbl.gridy=3; add(new JButton("5"),gbl); gbl.gridx=2; gbl.gridy=3; add(new JButton("6"),gbl); gbl.gridx=3; gbl.gridy=3; add(new JButton("*"),gbl); gbl.gridx=0; gbl.gridy=4; add(new JButton("1"),gbl); gbl.gridx=1; gbl.gridy=4; add(new JButton("2"),gbl); gbl.gridx=2; gbl.gridy=4; add(new JButton("3"),gbl); gbl.gridx=3; gbl.gridy=4; add(new JButton("/"),gbl); //**************************** JPanel jp2=new JPanel; jp2.setLayout(new GridLayout); gbl.gridwidth=4; gbl.gridx=0; gbl.gridy=5; add(jp2,gbl); gbl.gridwidth=1; gbl.gridx=0; gbl.gridy=0; jp2.add(new JButton("0"),gbl); gbl.gridx=1; gbl.gridy=0; jp2.add(new JButton("."),gbl); gbl.weightx =1.0; gbl.gridx=2; gbl.gridy=0; jp2.add(new JButton("="),gbl); }//const public static void main(String args[]) { JFrame fr=new JFrame("Practical 6"); fr.setContentPane(new C_1_Calculator); fr.setSize(200,250); fr.setLocation(250,250); fr.setVisible(true); }//main }//Prac630 //compile and run this program