Image - Java AWT - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class cs extends Canvas
{
public void paint(Graphics g)
{
Toolkit t=getToolkit();
Image img=t.getImage("/home/nn/Desktop/1.jpg");
setSize(500,500);
g.drawImage(img, 0,0, this);
}
}
class ex10 extends Frame
{
cs t;
ScrollPane sp;
ex10()
{
super("Image Example");
setLayout(null);
setSize(500,500);
setVisible(true);
t=new cs();
sp=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
sp.add(t);
add(sp);
sp.setBounds(50,50,300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public static void main(String s[])
{
ex10 ob=new ex10();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex10.java
nn@linuxmint ~/Desktop/java 7 $ java ex10
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class cs extends Canvas
{
public void paint(Graphics g)
{
Toolkit t=getToolkit();
Image img=t.getImage("/home/nn/Desktop/1.jpg");
setSize(500,500);
g.drawImage(img, 0,0, this);
}
}
class ex10 extends Frame
{
cs t;
ScrollPane sp;
ex10()
{
super("Image Example");
setLayout(null);
setSize(500,500);
setVisible(true);
t=new cs();
sp=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
sp.add(t);
add(sp);
sp.setBounds(50,50,300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public static void main(String s[])
{
ex10 ob=new ex10();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex10.java
nn@linuxmint ~/Desktop/java 7 $ java ex10
nn@linuxmint ~/Desktop/java 7 $
Drawing - Mouse - Java AWT - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex9 extends Frame implements ActionListener
{
Canvas c;
Button b;
int x1,x2,y1,y2;
ex9()
{
super("Drawing");
setLayout(null);
setSize(500,500);
setVisible(true);
c=new Canvas();
c.setBackground(Color.white);
b=new Button("Clear");
c.setBounds(50,50,300,300);
add(c);
b.setBounds(200,400,50,20);
add(b);
b.addActionListener(this);
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ae)
{
x1=ae.getX();
y1=ae.getY();
c.getGraphics().drawLine(x1, y1, x1, y1);
}
}
);
c.addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent ae)
{
x2=ae.getX();
y2=ae.getY();
c.getGraphics().drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
c.repaint();
}
public static void main(String s[])
{
ex9 ob=new ex9();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex9.java
nn@linuxmint ~/Desktop/java 7 $ java ex9
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex9 extends Frame implements ActionListener
{
Canvas c;
Button b;
int x1,x2,y1,y2;
ex9()
{
super("Drawing");
setLayout(null);
setSize(500,500);
setVisible(true);
c=new Canvas();
c.setBackground(Color.white);
b=new Button("Clear");
c.setBounds(50,50,300,300);
add(c);
b.setBounds(200,400,50,20);
add(b);
b.addActionListener(this);
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ae)
{
x1=ae.getX();
y1=ae.getY();
c.getGraphics().drawLine(x1, y1, x1, y1);
}
}
);
c.addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent ae)
{
x2=ae.getX();
y2=ae.getY();
c.getGraphics().drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
c.repaint();
}
public static void main(String s[])
{
ex9 ob=new ex9();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex9.java
nn@linuxmint ~/Desktop/java 7 $ java ex9
nn@linuxmint ~/Desktop/java 7 $
Temperature Converter - Checkbox- Java AWT - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex8 extends Frame implements ItemListener,ActionListener
{
Label l1,l2;
TextField t;
Checkbox c1,c2;
Button b;
ex8()
{
super("Temperature Converter");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new Label();
l2=new Label();
t=new TextField();
b=new Button("Clear");
CheckboxGroup cg=new CheckboxGroup();
c1=new Checkbox("Celsius to Fahrenheit",cg,true);
c2=new Checkbox("Fahrenheit to Celsius",cg,false);
l1.setBounds(50,100,200,20);
add(l1);
l2.setBounds(200,130,100,20);
add(l2);
t.setBounds(260,100,50,20);
add(t);
c1.setBounds(50,300,250,20);
add(c1);
c2.setBounds(50,330,250,20);
add(c2);
b.setBounds(200,250,50,20);
add(b);
c1.addItemListener(this);
c2.addItemListener(this);
b.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ae)
{
float c,f;
if(c1.getState())
{
l1.setText("Enter temperature in Celsius");
if(t.getText()!=null)
{
c=Float.parseFloat(t.getText());
f=(9/(float)5)*c+32;
l2.setText(Float.toString(f)+"F");
}
}
if(c2.getState())
{
l1.setText("Enter temperature in Fahrenheit");
if(t.getText()!=null)
{
f=Float.parseFloat(t.getText());
c=5/(float)9*(f-32);
l2.setText(Float.toString(c)+"C");
}
}
}
public void actionPerformed(ActionEvent ae)
{
t.setText(null);
l2.setText(null);
t.requestFocus();
}
public static void main(String s[])
{
ex8 ob=new ex8();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex8.java
nn@linuxmint ~/Desktop/java 7 $ java ex8
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex8 extends Frame implements ItemListener,ActionListener
{
Label l1,l2;
TextField t;
Checkbox c1,c2;
Button b;
ex8()
{
super("Temperature Converter");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new Label();
l2=new Label();
t=new TextField();
b=new Button("Clear");
CheckboxGroup cg=new CheckboxGroup();
c1=new Checkbox("Celsius to Fahrenheit",cg,true);
c2=new Checkbox("Fahrenheit to Celsius",cg,false);
l1.setBounds(50,100,200,20);
add(l1);
l2.setBounds(200,130,100,20);
add(l2);
t.setBounds(260,100,50,20);
add(t);
c1.setBounds(50,300,250,20);
add(c1);
c2.setBounds(50,330,250,20);
add(c2);
b.setBounds(200,250,50,20);
add(b);
c1.addItemListener(this);
c2.addItemListener(this);
b.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ae)
{
float c,f;
if(c1.getState())
{
l1.setText("Enter temperature in Celsius");
if(t.getText()!=null)
{
c=Float.parseFloat(t.getText());
f=(9/(float)5)*c+32;
l2.setText(Float.toString(f)+"F");
}
}
if(c2.getState())
{
l1.setText("Enter temperature in Fahrenheit");
if(t.getText()!=null)
{
f=Float.parseFloat(t.getText());
c=5/(float)9*(f-32);
l2.setText(Float.toString(c)+"C");
}
}
}
public void actionPerformed(ActionEvent ae)
{
t.setText(null);
l2.setText(null);
t.requestFocus();
}
public static void main(String s[])
{
ex8 ob=new ex8();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex8.java
nn@linuxmint ~/Desktop/java 7 $ java ex8
nn@linuxmint ~/Desktop/java 7 $
Colour Generator - Scroll Bar - Java AWT Example - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex6 extends Frame implements AdjustmentListener
{
Scrollbar s1,s2,s3;
Label l1;
Panel p;
int r,g,b;
ex6()
{
super("Colour Generator:Scroll bar example");
setLayout(null);
setSize(700,400);
setVisible(true);
l1=new Label("www.2K8618.blogspot.com");
l1.setBackground(Color.black);
l1.setForeground(Color.blue);
s1=new Scrollbar();
s2=new Scrollbar();
s3=new Scrollbar();
s1.setMaximum(255);
s2.setMaximum(255);
s3.setMaximum(255);
s1.setBounds(50,50,20,100);
add(s1);
s2.setBounds(50,160,20,100);
add(s2);
s3.setBounds(50,270,20,100);
add(s3);
p=new Panel();
p.setBounds(100,100,500,200);
add(p);
p.add(l1);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
s3.addAdjustmentListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
if(s1==ae.getSource())
{
r=s1.getValue();
p.setBackground(new Color(r,g,b));
}
if(s2==ae.getSource())
{
g=s2.getValue();
p.setBackground(new Color(r,g,b));
}
if(s3==ae.getSource())
{
b=s3.getValue();
p.setBackground(new Color(r,g,b));
}
}
public static void main(String s[])
{
ex6 ob=new ex6();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex6.java
nn@linuxmint ~/Desktop/java 7 $ java ex6
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex6 extends Frame implements AdjustmentListener
{
Scrollbar s1,s2,s3;
Label l1;
Panel p;
int r,g,b;
ex6()
{
super("Colour Generator:Scroll bar example");
setLayout(null);
setSize(700,400);
setVisible(true);
l1=new Label("www.2K8618.blogspot.com");
l1.setBackground(Color.black);
l1.setForeground(Color.blue);
s1=new Scrollbar();
s2=new Scrollbar();
s3=new Scrollbar();
s1.setMaximum(255);
s2.setMaximum(255);
s3.setMaximum(255);
s1.setBounds(50,50,20,100);
add(s1);
s2.setBounds(50,160,20,100);
add(s2);
s3.setBounds(50,270,20,100);
add(s3);
p=new Panel();
p.setBounds(100,100,500,200);
add(p);
p.add(l1);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
s3.addAdjustmentListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
if(s1==ae.getSource())
{
r=s1.getValue();
p.setBackground(new Color(r,g,b));
}
if(s2==ae.getSource())
{
g=s2.getValue();
p.setBackground(new Color(r,g,b));
}
if(s3==ae.getSource())
{
b=s3.getValue();
p.setBackground(new Color(r,g,b));
}
}
public static void main(String s[])
{
ex6 ob=new ex6();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex6.java
nn@linuxmint ~/Desktop/java 7 $ java ex6
nn@linuxmint ~/Desktop/java 7 $
List Example - Java AWT Example - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex4 extends Frame implements ActionListener,FocusListener
{
List l1,l2;
Label lb;
Button b1,b2,b3,b4;
ex4()
{
super("List example");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new List();
l2=new List();
lb=new Label();
b1=new Button("<");
b2=new Button(">");
b3=new Button("<<");
b4=new Button(">>");
l1.setMultipleMode(true);
l2.setMultipleMode(true);
l1.add("Delhi");
l1.add("Pune");
l1.add("Mumbai");
l1.add("Chennai");
l1.setBounds(50,100,100,150);
add(l1);
l2.setBounds(300,100,100,150);
add(l2);
b1.setBounds(160,100,50,20);
add(b1);
b2.setBounds(160,130,50,20);
add(b2);
b3.setBounds(160,160,50,20);
add(b3);
b4.setBounds(160,190,50,20);
add(b4);
lb.setBounds(200,300,120,20);
add(lb);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b1.addFocusListener(this);
b2.addFocusListener(this);
Toolkit t=getToolkit();
Dimension d=t.getScreenSize();
int h=(int) d.getHeight();
int w=(int) d.getWidth();
setLocation(w/4, h/4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
int n;
if(ae.getSource()==b1)
{
String s[]=l2.getSelectedItems();
System.out.println(l2.getSelectedIndex());
if(s.length>1)
lb.setText("Please select only one item for this operation");
else
{
l1.add(s[0]);
l2.remove(s[0]);
}
}
if(ae.getSource()==b2)
{
String s[]=l1.getSelectedItems();
if(s.length>1)
lb.setText("Please select only one item for this operation");
else
{
l2.add(s[0]);
l1.remove(s[0]);
}
}
if(ae.getSource()==b3)
{
String s[]= l2.getSelectedItems();
for(int i=0;i<s.length;i++)
{
l1.add(s[i]);
l2.remove(s[i]);
}
}
if(ae.getSource()==b4)
{
String s[]= l1.getSelectedItems();
for(int i=0;i<s.length;i++)
{
l2.add(s[i]);
l1.remove(s[i]);
}
}
}
public void focusLost(FocusEvent fe)
{
if(fe.getSource()==b1)
lb.setText(null);
if(fe.getSource()==b2)
lb.setText(null);
}
public void focusGained(FocusEvent fe)
{}
public static void main(String s[])
{
ex4 ob=new ex4();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex4.java
nn@linuxmint ~/Desktop/java 7 $ java ex4
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex4 extends Frame implements ActionListener,FocusListener
{
List l1,l2;
Label lb;
Button b1,b2,b3,b4;
ex4()
{
super("List example");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new List();
l2=new List();
lb=new Label();
b1=new Button("<");
b2=new Button(">");
b3=new Button("<<");
b4=new Button(">>");
l1.setMultipleMode(true);
l2.setMultipleMode(true);
l1.add("Delhi");
l1.add("Pune");
l1.add("Mumbai");
l1.add("Chennai");
l1.setBounds(50,100,100,150);
add(l1);
l2.setBounds(300,100,100,150);
add(l2);
b1.setBounds(160,100,50,20);
add(b1);
b2.setBounds(160,130,50,20);
add(b2);
b3.setBounds(160,160,50,20);
add(b3);
b4.setBounds(160,190,50,20);
add(b4);
lb.setBounds(200,300,120,20);
add(lb);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b1.addFocusListener(this);
b2.addFocusListener(this);
Toolkit t=getToolkit();
Dimension d=t.getScreenSize();
int h=(int) d.getHeight();
int w=(int) d.getWidth();
setLocation(w/4, h/4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
int n;
if(ae.getSource()==b1)
{
String s[]=l2.getSelectedItems();
System.out.println(l2.getSelectedIndex());
if(s.length>1)
lb.setText("Please select only one item for this operation");
else
{
l1.add(s[0]);
l2.remove(s[0]);
}
}
if(ae.getSource()==b2)
{
String s[]=l1.getSelectedItems();
if(s.length>1)
lb.setText("Please select only one item for this operation");
else
{
l2.add(s[0]);
l1.remove(s[0]);
}
}
if(ae.getSource()==b3)
{
String s[]= l2.getSelectedItems();
for(int i=0;i<s.length;i++)
{
l1.add(s[i]);
l2.remove(s[i]);
}
}
if(ae.getSource()==b4)
{
String s[]= l1.getSelectedItems();
for(int i=0;i<s.length;i++)
{
l2.add(s[i]);
l1.remove(s[i]);
}
}
}
public void focusLost(FocusEvent fe)
{
if(fe.getSource()==b1)
lb.setText(null);
if(fe.getSource()==b2)
lb.setText(null);
}
public void focusGained(FocusEvent fe)
{}
public static void main(String s[])
{
ex4 ob=new ex4();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex4.java
nn@linuxmint ~/Desktop/java 7 $ java ex4
nn@linuxmint ~/Desktop/java 7 $
Prime Number Generation - Java AWT Example - Internet & Web Programming Lab
Program:
Output:import java.awt.*; import java.awt.event.*; class ex3 extends Frame implements ActionListener { Label l1; TextField t1; Button b1,b2; List lt; ex3() { super("Prime number generation"); setLayout(null); setSize(500,400); setVisible(true); l1=new Label("Enter the limit:"); t1=new TextField(); b1=new Button("Find"); b2=new Button("Clear"); lt=new List(); l1.setBounds(100,50,120,20); add(l1); t1.setBounds(250,50,50,20); add(t1); lt.setBounds(100,90,100,100); add(lt); b1.setBounds(200,150,50,20); add(b1); b2.setBounds(270,150,50,20); add(b2); b1.addActionListener(this); b2.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public void actionPerformed(ActionEvent ae) { int n; if(ae.getSource()==b1) { n=Integer.parseInt(t1.getText().trim()); int flag=0; for(int i=2;i<=n;i++) { flag=0; for(int j=2;j<=i/2;j++) { if(i%j==0) { flag=1; break; } } if(flag==0) lt.add(Integer.toString(i)); } } else { t1.setText(null); lt.removeAll(); t1.requestFocus(); } } public static void main(String s[]) { ex3 ob=new ex3(); } }
nn@linuxmint ~/Desktop/java 7 $ javac ex3.java nn@linuxmint ~/Desktop/java 7 $ java ex3 nn@linuxmint ~/Desktop/java 7 $
Factorial of a Number - Java AWT - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex2 extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1;
Button b1,b2;
ex2()
{
super("Factorial of a number");
setLayout(null);
setSize(500,400);
setVisible(true);
b1=new Button("Find");
b2=new Button("Clear");
l1=new Label("Enter the number:");
l2=new Label("The factorial is:");
l3=new Label(null);
t1=new TextField();
l1.setBounds(100,50,120,20);
add(l1);
t1.setBounds(240,50,50,20);
add(t1);
l2.setBounds(100,80,130,20);
add(l2);
l3.setBounds(240,80,50,20);
add(l3);
b1.setBounds(200,150,50,20);
add(b1);
b2.setBounds(270,150,50,20);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
Toolkit t=getToolkit();
Dimension d=t.getScreenSize();
int h=(int) d.getHeight();
int w=(int) d.getWidth();
setLocation(w/4, h/4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
int n;
if(ae.getSource()==b1)
{
n=Integer.parseInt(t1.getText().trim());
l3.setText(Integer.toString(fact(n)));
}
else
{
t1.setText(null);
l3.setText(null);
t1.requestFocus();
}
}
int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
public static void main(String s[])
{
ex2 ob=new ex2();
}
}
Output:import java.awt.event.*;
class ex2 extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1;
Button b1,b2;
ex2()
{
super("Factorial of a number");
setLayout(null);
setSize(500,400);
setVisible(true);
b1=new Button("Find");
b2=new Button("Clear");
l1=new Label("Enter the number:");
l2=new Label("The factorial is:");
l3=new Label(null);
t1=new TextField();
l1.setBounds(100,50,120,20);
add(l1);
t1.setBounds(240,50,50,20);
add(t1);
l2.setBounds(100,80,130,20);
add(l2);
l3.setBounds(240,80,50,20);
add(l3);
b1.setBounds(200,150,50,20);
add(b1);
b2.setBounds(270,150,50,20);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
Toolkit t=getToolkit();
Dimension d=t.getScreenSize();
int h=(int) d.getHeight();
int w=(int) d.getWidth();
setLocation(w/4, h/4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
int n;
if(ae.getSource()==b1)
{
n=Integer.parseInt(t1.getText().trim());
l3.setText(Integer.toString(fact(n)));
}
else
{
t1.setText(null);
l3.setText(null);
t1.requestFocus();
}
}
int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
public static void main(String s[])
{
ex2 ob=new ex2();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex2.java
nn@linuxmint ~/Desktop/java 7 $ java ex2
nn@linuxmint ~/Desktop/java 7 $
nn@linuxmint ~/Desktop/java 7 $ java ex2
nn@linuxmint ~/Desktop/java 7 $
Addition of Two Numbers - Java AWT Example - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class ex1 extends Frame implements ActionListener
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1,b2;
ex1()
{
super("Addition of two numbers");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new Label("Enter the first no:");
l2=new Label("Enter the second no:");
l3=new Label("The sum is:");
l4=new Label(null);
t1=new TextField();
t2=new TextField();
b1=new Button("ADD");
b2=new Button("Clear");
l1.setBounds(100,50,120,20);
add(l1);
t1.setBounds(240,50,50,20);
add(t1);
l2.setBounds(100,80,130,20);
add(l2);
t2.setBounds(240,80,50,20);
add(t2);
l3.setBounds(100,110,100,20);
add(l3);
l4.setBounds(210,110,60,20);
add(l4);
b1.setBounds(200,150,50,20);
add(b1);
b2.setBounds(270,150,50,20);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
float a,b,c;
if(ae.getSource()==b1)
{
a=Float.parseFloat(t1.getText().trim());
b=Float.parseFloat(t2.getText().trim());
c=a+b;
l4.setText(Float.toString(c));
}
else
{
t1.setText(null);
t2.setText(null);
l4.setText(null);
}
}
public static void main(String s[])
{
ex1 ob=new ex1();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex1.java
nn@linuxmint ~/Desktop/java 7 $ java ex1
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex1 extends Frame implements ActionListener
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1,b2;
ex1()
{
super("Addition of two numbers");
setLayout(null);
setSize(500,400);
setVisible(true);
l1=new Label("Enter the first no:");
l2=new Label("Enter the second no:");
l3=new Label("The sum is:");
l4=new Label(null);
t1=new TextField();
t2=new TextField();
b1=new Button("ADD");
b2=new Button("Clear");
l1.setBounds(100,50,120,20);
add(l1);
t1.setBounds(240,50,50,20);
add(t1);
l2.setBounds(100,80,130,20);
add(l2);
t2.setBounds(240,80,50,20);
add(t2);
l3.setBounds(100,110,100,20);
add(l3);
l4.setBounds(210,110,60,20);
add(l4);
b1.setBounds(200,150,50,20);
add(b1);
b2.setBounds(270,150,50,20);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
float a,b,c;
if(ae.getSource()==b1)
{
a=Float.parseFloat(t1.getText().trim());
b=Float.parseFloat(t2.getText().trim());
c=a+b;
l4.setText(Float.toString(c));
}
else
{
t1.setText(null);
t2.setText(null);
l4.setText(null);
}
}
public static void main(String s[])
{
ex1 ob=new ex1();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex1.java
nn@linuxmint ~/Desktop/java 7 $ java ex1
nn@linuxmint ~/Desktop/java 7 $
Matrix Multiplication - TCP - Client Server Program - Network-DBMS Lab - C Program
Program:
// Client Program: mc.c
Server Program: ms.c
Output:
Terminal 1: (Server)
// Client Program: mc.c
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> main() { struct sockaddr_in client,server; int a[10][10],b[10][10],c[10][10],mt[4]; int s,sock,r,i,j; client.sin_family=AF_INET; client.sin_port=3000; client.sin_addr.s_addr=inet_addr("127.0.0.1"); s=socket(AF_INET,SOCK_STREAM,0); connect(s,(struct sockaddr *)&client,sizeof(client)); printf("Enter order of matrix 1:"); scanf("%d %d",&mt[0],&mt[1]); printf("Enter order of matrix 2:"); scanf("%d %d",&mt[2],&mt[3]); if(mt[1]!=mt[2]) printf("Matrices cannot be multiplied"); else { send(s,&mt,sizeof(mt),0); printf("Enter Elements for matrix 1:\n"); for(i=0;i<mt[0];i++) { for(j=0;j<mt[1];j++) { scanf("%d",&a[i][j]); } } send(s,&a,sizeof(a),0); printf("Enter Elements for matrix 2:\n"); for(i=0;i<mt[2];i++) { for(j=0;j<mt[3];j++) { scanf("%d",&b[i][j]); } } send(s,&b,sizeof(b),0); recv(s,&c,sizeof(c),0); printf("\nResult:\n"); for(i=0;i<mt[0];i++) { printf("\n"); for(j=0;j<mt[3];j++) { printf(" %d ",c[i][j]); } } } printf("\n"); close(s); }
Server Program: ms.c
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> main() { struct sockaddr_in client,server; int a[10][10],b[10][10],c[10][10],mt[4]; int s,sock,r,i,j,sz,k; server.sin_family=AF_INET; server.sin_port=3000; server.sin_addr.s_addr=inet_addr("127.0.0.1"); s=socket(AF_INET,SOCK_STREAM,0); bind(s,(struct sockaddr *)&server,sizeof(server)); listen(s,1); sz=sizeof(server); sock=accept(s,(struct sockaddr *)&server,&sz); recv(sock,&mt,sizeof(mt),0); recv(sock,&a,sizeof(a),0); printf("\nReceived Matrix 1\n"); for(i=0;i<mt[0];i++) { printf("\n"); for(j=0;j<mt[1];j++) { printf(" %d ",a[i][j]); } } recv(sock,&b,sizeof(b),0); printf("\nReceived Matrix 2\n"); for(i=0;i<mt[2];i++) { printf("\n"); for(j=0;j<mt[3];j++) { printf(" %d ",b[i][j]); } } printf("\nMultiplying........\n"); for(i=0;i<mt[0];i++) for(j=0;j<mt[3];j++) { c[i][j]=0; for(k=0;k<mt[1];k++) c[i][j]+=a[i][k]*b[k][j]; } printf("\nResult:\n") ; for(i=0;i<mt[0];i++) { printf("\n"); for(j=0;j<mt[3];j++) { printf(" %d ",c[i][j]); } } printf("\n"); send(sock,&c,sizeof(c),0); close(sock); close(s); }
Output:
Terminal 1: (Server)
nn@linuxmint ~ $ gcc ms.c -o s nn@linuxmint ~ $ ./s Received Matrix 1 1 1 1 1 Received Matrix 2 2 2 2 2 Multiplying........ Result: 4 4 4 4 nn@linuxmint ~ $Terminal 2: (Client)
nn@linuxmint ~ $ gcc mc.c -o c nn@linuxmint ~ $ ./c Enter order of matrix 1:2 2 Enter order of matrix 2:2 2 Enter Elements for matrix 1: 1 1 1 1 Enter Elements for matrix 2: 2 2 2 2 Result: 4 4 4 4 nn@linuxmint ~ $
Number of Lines,Words and Characters - Lex Program - Compiler Design
Program:
// lwc.l
%{
int lines=0,wrds=0,characters=0;
%}
whitespace [ \n\t]*
%%
[ \t]{whitespace} {wrds++;characters++;}
\n{whitespace} {wrds++;characters++;lines++;}
. {characters++;}
%%
main()
{
yylex();
printf("\nlines: %d\nwords: %d\ncharacters: %d\n",lines,wrds,characters);
}
Output:
// lwc.l
%{
int lines=0,wrds=0,characters=0;
%}
whitespace [ \n\t]*
%%
[ \t]{whitespace} {wrds++;characters++;}
\n{whitespace} {wrds++;characters++;lines++;}
. {characters++;}
%%
main()
{
yylex();
printf("\nlines: %d\nwords: %d\ncharacters: %d\n",lines,wrds,characters);
}
Floating Point & Integers Lex Program Compiler Design
Program:
// frn.l
%{
%}
DIGIT [0-9]
%%
{DIGIT}* {ECHO;printf(" Integer");}
{DIGIT}*?\.{DIGIT}* {ECHO;printf(" Float ");}
%%
main()
{
yylex();
}
Output:
nn@linuxmint ~ $ lex frn.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<test1.txt
1 Integer
500 Integer
10.1 Float
.1 Float
1.0 Float
nn@linuxmint ~ $
// input : test1.txt
1
500
10.1
.1
1.0
// frn.l
%{
%}
DIGIT [0-9]
%%
{DIGIT}* {ECHO;printf(" Integer");}
{DIGIT}*?\.{DIGIT}* {ECHO;printf(" Float ");}
%%
main()
{
yylex();
}
nn@linuxmint ~ $ lex frn.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<test1.txt
1 Integer
500 Integer
10.1 Float
.1 Float
1.0 Float
nn@linuxmint ~ $
// input : test1.txt
1
500
10.1
.1
1.0
Parser for SQL Nested Queries - Compiler Design - Yacc Program
Program:
// Lex file: sq.l
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]
select return SELECT;
distinct return DISTINCT;
from return FROM;
where return WHERE;
like return LIKE;
desc return DESC;
asc return ASC;
"group by" return GROUP;
having return HAVING;
"order by" return ORDER;
or return OR;
and return AND;
in return IN;
{digit}+ return NUM;
{alpha}({alpha}|{digit})* return ID;
"<=" return LE;
">=" return GE;
"==" return EQ;
"!=" return NE;
. return yytext[0];
%%
// Yacc file: sq.y
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC IN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%%
S : ST1';' {printf("INPUT ACCEPTED...\n");exit(0);};
ST1 : SELECT attributeList FROM tableList ST2
| SELECT DISTINCT attributeList FROM tableList ST2
;
ST2 : WHERE COND ST3
| ST3
;
ST3 : GROUP attributeList ST4
| ST4
;
ST4 : HAVING COND ST5
| ST5
;
ST5 : ORDER attributeList ST6
|
;
ST6 : DESC
| ASC
|
;
attributeList : ID','attributeList
| '*'
| ID
;
tableList : ID',' tableList
| ID
;
COND : COND OR COND
| COND AND COND
| E
| ID IN '(' ST1 ')'
;
E : F'=' F
| F '<' F
| F '>' F
| F LE F
| F GE F
| F EQ F
| F NE F
| F OR F
| F AND F
| F LIKE F
;
F : ID
| NUM
;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
printf("Enter the query:");
yyparse();
}
Output:
nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select model from product where manufacture in ( select manuid from manufactures where manufacture = keltron);
INPUT ACCEPTED...
nn@linuxmint ~ $
// Lex file: sq.l
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]
select return SELECT;
distinct return DISTINCT;
from return FROM;
where return WHERE;
like return LIKE;
desc return DESC;
asc return ASC;
"group by" return GROUP;
having return HAVING;
"order by" return ORDER;
or return OR;
and return AND;
in return IN;
{digit}+ return NUM;
{alpha}({alpha}|{digit})* return ID;
"<=" return LE;
">=" return GE;
"==" return EQ;
"!=" return NE;
. return yytext[0];
%%
// Yacc file: sq.y
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC IN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%%
S : ST1';' {printf("INPUT ACCEPTED...\n");exit(0);};
ST1 : SELECT attributeList FROM tableList ST2
| SELECT DISTINCT attributeList FROM tableList ST2
;
ST2 : WHERE COND ST3
| ST3
;
ST3 : GROUP attributeList ST4
| ST4
;
ST4 : HAVING COND ST5
| ST5
;
ST5 : ORDER attributeList ST6
|
;
ST6 : DESC
| ASC
|
;
attributeList : ID','attributeList
| '*'
| ID
;
tableList : ID',' tableList
| ID
;
COND : COND OR COND
| COND AND COND
| E
| ID IN '(' ST1 ')'
;
E : F'=' F
| F '<' F
| F '>' F
| F LE F
| F GE F
| F EQ F
| F NE F
| F OR F
| F AND F
| F LIKE F
;
F : ID
| NUM
;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
printf("Enter the query:");
yyparse();
}
nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select model from product where manufacture in ( select manuid from manufactures where manufacture = keltron);
INPUT ACCEPTED...
nn@linuxmint ~ $
Java AWT Example - Font - Internet & Web Programming Lab
Program:
import java.awt.*;
import java.awt.event.*;
class font extends Frame implements ItemListener
{
Choice c1,c2,c3;
Label lb;
int a,b;
String c;
font()
{
setLayout(null);
setSize(500,400);
setVisible(true);
setTitle("Java Awt : Font");
c1=new Choice();
c2=new Choice();
c3=new Choice();
lb=new Label(null);
lb.setBackground(Color.white);
lb.setForeground(Color.blue);
lb.setAlignment(1);
c1.setBounds(50,100,100,20);
add(c1);
c2.setBounds(160,100,100,20);
add(c2);
c3.setBounds(270,100,100,20);
add(c3);
lb.setBounds(10,200,480,60);
add(lb);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
lb.setText("www.2k8618.blogspot.com");
c1.add("Normal");
c1.add("Bold");
c1.add("Italic");
for(int i=8;i<72;i+=2)
c2.add(Integer.toString(i));
GraphicsEnvironment
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String f[]=ge.getAvailableFontFamilyNames();
for(int i=0;i<f.length;i++)
c3.add(f[i]);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ae)
{
if(c1==ae.getSource())
{
if(c1.getSelectedIndex()==0)
a=Font.PLAIN;
else if(c1.getSelectedIndex()==1)
a=Font.BOLD;
else if(c1.getSelectedIndex()==2)
a=Font.ITALIC;
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
if(c2==ae.getSource())
{
b=Integer.parseInt(c2.getSelectedItem());
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
if(c3==ae.getSource())
{
c=c3.getSelectedItem();
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
}
public static void main(String s[])
{
font ob=new font();
}
}
Output:
nn@linuxmint ~/Desktop/java_all $ javac font.java
nn@linuxmint ~/Desktop/java_all $ java font
nn@linuxmint ~/Desktop/java_all $
import java.awt.*;
import java.awt.event.*;
class font extends Frame implements ItemListener
{
Choice c1,c2,c3;
Label lb;
int a,b;
String c;
font()
{
setLayout(null);
setSize(500,400);
setVisible(true);
setTitle("Java Awt : Font");
c1=new Choice();
c2=new Choice();
c3=new Choice();
lb=new Label(null);
lb.setBackground(Color.white);
lb.setForeground(Color.blue);
lb.setAlignment(1);
c1.setBounds(50,100,100,20);
add(c1);
c2.setBounds(160,100,100,20);
add(c2);
c3.setBounds(270,100,100,20);
add(c3);
lb.setBounds(10,200,480,60);
add(lb);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
lb.setText("www.2k8618.blogspot.com");
c1.add("Normal");
c1.add("Bold");
c1.add("Italic");
for(int i=8;i<72;i+=2)
c2.add(Integer.toString(i));
GraphicsEnvironment
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String f[]=ge.getAvailableFontFamilyNames();
for(int i=0;i<f.length;i++)
c3.add(f[i]);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ae)
{
if(c1==ae.getSource())
{
if(c1.getSelectedIndex()==0)
a=Font.PLAIN;
else if(c1.getSelectedIndex()==1)
a=Font.BOLD;
else if(c1.getSelectedIndex()==2)
a=Font.ITALIC;
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
if(c2==ae.getSource())
{
b=Integer.parseInt(c2.getSelectedItem());
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
if(c3==ae.getSource())
{
c=c3.getSelectedItem();
lb.setFont(new Font(c,a,b));
lb.setText(lb.getText());
}
}
public static void main(String s[])
{
font ob=new font();
}
}
Output:
nn@linuxmint ~/Desktop/java_all $ javac font.java
nn@linuxmint ~/Desktop/java_all $ java font
nn@linuxmint ~/Desktop/java_all $
Subscribe to:
Posts (Atom)
Blog Archive
-
▼
2011
(179)
-
▼
September
(18)
- Internet and Mobile Communication System Technolog...
- Design and Analysis of Algorithms - Previous Quest...
- Computer Graphics and Multimedia - Previous Questi...
- Internet and Web Programming with Java - Previous ...
- Advanced Database Systems - Previous Question Pape...
- Image - Java AWT - Internet & Web Programming Lab
- Drawing - Mouse - Java AWT - Internet & Web Progra...
- Temperature Converter - Checkbox- Java AWT - Inter...
- Colour Generator - Scroll Bar - Java AWT Example -...
- List Example - Java AWT Example - Internet & Web P...
- Prime Number Generation - Java AWT Example - Inter...
- Factorial of a Number - Java AWT - Internet & Web ...
- Addition of Two Numbers - Java AWT Example - Inter...
- Matrix Multiplication - TCP - Client Server Progra...
- Number of Lines,Words and Characters - Lex Program...
- Floating Point & Integers Lex Program Compiler Design
- Parser for SQL Nested Queries - Compiler Design - ...
- Java AWT Example - Font - Internet & Web Programmi...
-
▼
September
(18)
Recent Posts
Tags
- 2K8CSE (1)
- Advanced Database Systems (1)
- Aptitude Questions (11)
- Assembly Language (17)
- Baseconversion (3)
- C (62)
- C++ (28)
- CGM Video Tutorials (2)
- Client-Server (16)
- Compiler Design (44)
- Compiler Lab (39)
- Computer Graphics (6)
- Computer Graphics and Multimedia (8)
- Data Communication and Computer Networks (5)
- Datastructure (16)
- DBMS (3)
- Ebook Collection (16)
- File (5)
- File Transfer Protocol (3)
- Graph Theory and Combinatorics (9)
- Graphics and Multimedia Lab (23)
- Inheritance (3)
- Intermediate Code Generator (5)
- Internet and Web Programming Lab (22)
- Internet and Web Programming with Java (13)
- JAVA (56)
- Java awt (9)
- Java Swings (13)
- LEX (23)
- Lexicographic Sorting (1)
- Linearsearch (2)
- LinkedList (3)
- LISP (14)
- List (awt) (1)
- Logic Diagramming (1)
- MASM (17)
- Matrix (7)
- Miniproject (4)
- NetBeans (11)
- Network-DBMS Lab (17)
- Operating Systems (12)
- Oracle (11)
- OS (1)
- Parser (10)
- Presentation Slides (8)
- Previous Question Papers (49)
- Programming Environment Lab (22)
- Recruitment (12)
- Results (5)
- Search (2)
- Select Lines (1)
- Shared Memory (2)
- Software Engineering (3)
- Sort (13)
- Stack (3)
- String (10)
- Substring Search Replacement (4)
- Syllabus (5)
- Systems Lab (11)
- Taylor Series (4)
- TCP (10)
- UDP (6)
- YACC (21)