Showing posts with label Java awt. Show all posts
Showing posts with label Java awt. Show all posts

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 $


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 $


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 $


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 $


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 $


Prime Number Generation - Java AWT Example - Internet & Web Programming Lab

Program:
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();
 }
}


Output:

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:


nn@linuxmint ~/Desktop/java 7 $ javac ex2.java
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 $


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 $



Related Posts Plugin for WordPress, Blogger...