Sunday, May 26, 2013

Change Background Color In Java

/*Program for background color changing using scrollbar*/

import java.awt.*;
import java.awt.event.*;

class sroll extends Frame implements AdjustmentListener,ActionListener
   {
        Button b;
        Scrollbar sr,sg,sb;
        Label lr,lg,lb;

public sroll()
  {
           b=new Button("EXIT:");
                       lr=new Label("RED:");
          lg=new Label("GREEN:");
          lb=new Label("BLUE:");

                       sr=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sg=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sb=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);

           b.addActionListener(this);
           sr.addAdjustmentListener(this);
          sg.addAdjustmentListener(this);
          sb.addAdjustmentListener(this);

                      setLayout(new FlowLayout());

          add(lr);  add(sr);  add(lg);  add(sg);  add(lb);  add(sb);
add(b);

}


    public void actionPerformed(ActionEvent a1)
      {

System.exit(0);

       }



                 public void adjustmentValueChanged(AdjustmentEvent ae)
      {

 int r=sr.getValue();
 int g=sg.getValue();
 int b=sb.getValue();

Color c=new Color(r,g,b);

   setBackground(c);
   }

public static void main(String args[])
 {


sroll a=new sroll();

a.setSize(200,200);

a.setVisible(true);
}

    }

Convert Checkbox To Radio Button In Java

/*Program to convert checkboxes into radio buttons*/

import java.awt.*;

class elec extends Frame
  {
     Label l1,l2;
     Checkbox a,b,c,d,e,f;
     CheckboxGroup x,y;

        public elec()
{
  setTitle("UNIVERSITY FORM:");
               l1=new Label("OPTION 1:");
  l2=new Label("OPTION 2:");
               x=new CheckboxGroup();
               y=new CheckboxGroup();
              a=new Checkbox("maths",false,x);
              b=new Checkbox("chem",false,x);
              c=new Checkbox("physics",false,x);
              d=new Checkbox("biology",false,y);
              e=new Checkbox("english",false,y);
              f=new Checkbox("hindi",false,y);

          setLayout(new FlowLayout());

          add(l1); add(a);  add(b);  add(c);
          add(l2); add(d);  add(e);  add(f);
  }

    public static void main(String args[])throws Exception
       {
 
           elec r=new elec();

           
            r.setSize(200,300);
            r.setVisible(true);
       }
 
 } 

File Writing In Java

/*Program For File Writing In Java*/

import java.io.*;


class simple
  {
      public static void main(String args[])throws Exception
        {
                File f=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc");
            String fnames[]=f.list();
             

    System.out.println("NAME"+"  "+"LENGTH"+"  "+"STATUS"+"  "+"TYPE");

            for(int i=0;i<fnames.length;i++)
{
  File f1=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc",fnames[i]);
       
               System.out.print(" "+f1.getName());
   System.out.print(" "+f1.length());
if(f1.canRead()==true)
{
  System.out.print(" "+"R");
}
if(f1.canWrite()==true)
 {
   System.out.print(" "+"w");
 }
if(f1.isFile()==true)
{
  System.out.print(" "+"FILE");
}
if(f1.isDirectory()==true)
{
 System.out.print(" "+"DIR");
}
                        System.out.println("");
       }
      }
 }

Number Conversion in Java

/*Program for conversion of a decimal no into binary,octal,hexadecimal*/

import java.string.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;


class dec extends Frame implements ActionListener
 {
     int n,d,a,no,i;
     int ans[]=new int[20];
     String s;
      String d="";
     Button b1,b2,b3,b4,b5,b6;
     TextField t1,t2,t3,t4;
 
         public dec()
              {
        b1=new Button("ENTER NUMBER:");
        b2=new Button("BINARY NUMBER:");
        b3=new Button("OCTAL NUMBER:");
        b4=new Button("HEXADECIMAL NUMBER:");
        b5=new Button("RESET:");
        b6=new Button("EXIT:");
        t1=new TextField(20);
        t2=new TextField(20);
        t3=new TextField(20);
        t4=new TextField(20);

                  setLayout(new FlowLayout());

                  add(b1); add(t1);  add(b2);  add(t2);  add(b3);  add(t3);  add(b4);  add(t4);
      add(b5);  add(b6);

                    b1.addActionListener(this);
       b2.addActionListener(this);
       b3.addActionListener(this);
       b4.addActionListener(this);
       b5.addActionListener(this);
       b6.addActionListener(this);
  }

    public void actionPerformed(ActionEvent ae)
{
 
  if(ae.getSource()==b2)
     {
       s=t1.getText();
         a=Integer.parseInt(s);

                         no=a;                      
                     for(i=1;no!=0;i++,no/=2)
                           ans[i]=no%2;
                       for(i--;i>0;i--)
                            d+=ans[i];  
                       if(t2.getText().equals(""))
                             t2.setText(d);
     }

if(ae.getSource()==b3)
     {
       s=t1.getText();

no=a;
for(d="",no=a,i=1;no!=0;i++,no/=8)  
                        ans[i]=no%8;
                       for(i--;i>0;i--)
                            d+=ans[i];
                         if(t3.getText().equals(""))
                             t3.setText(d);
   }


if(ae.getSource()==b3)
     {
       s=t1.getText();

no=a;
for(d="",no=a,i=1;no!=0;i++,no/=16)
                           ans[i]=no%16;
                     for(i--;i>0;i--)
                          {
                              switch(ans[i])
                                  {
                                       case 10:d+="A";  break;
                                       case 11:d+="B";  break;
                                       case 12:d+="C";  break;
                                       case 13:d+="D";  break;
                                        case 14:d+="E";  break;
                                      case 15:d+="F";  break;
                                      default:d+=ans[i];
                                  }
                        }
                              if(t4.getText().equals(""))
                                    t4.setText(d);
                  }

if(ae.getSource()==b5)
     {
            t1.setText("");
     }

if(ae.getSource()==b6)
     {

System.exit(0);
     }
}

      public static void main(String args[])throws Exception
{
    dec de=new dec();

     de.setSize(200,400);
     de.setVisible(true);
}
    }




Pattern in Java

/**Program For Design pattern in java**/

import java.io.*;

class loop
  {
       int i,j,n;

      public void getdata()throws Exception
         {
             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               System.out.println("ENTER THE NUMBER OF LINES TO BE PRINTED:");
                 n=Integer.parseInt(br.readLine());
         }
     public void display()
         {
              for(i=0;i<n;i++)
                {
                  for(j=i;j<n;j++)
                    {
                        System.out.print("*");
                    }
                        System.out.println(" ");        
                 }
                           
                   
          }
                   
             
        public static void main(String args[])throws Exception
         {
            loop l=new loop();
   
             l.getdata();
             l.display();
         }
  }
 

Graphics in Java

/**Graphics In Java Example **/

import java.awt.*;
import java.awt.event.*;

class line extends Frame implements MouseListener,ActionListener
 {

     int x1,x2,y1,y2;
     Button b;
   
public line()
 {
 
    addMouseListener(this);
                 b=new Button("EXIT");

     setLayout(new FlowLayout());
                    add(b);
                       b.addActionListener(this);
 }

public void actionPerformed(ActionEvent ae)
 {

     System.exit(0);
 }


public void mousePressed(MouseEvent me)
 {
    x1=me.getX();
    y1=me.getY();
 }

public void mouseReleased(MouseEvent me)
 {
    x2=me.getX();
    y2=me.getY();
 
    Graphics g=getGraphics();

   g.drawLine(x1,y1,x2,y2);
}

public void mouseClicked(MouseEvent me)
 {
 }

public void mouseEntered(MouseEvent me)
 {
 }

public void mouseExited(MouseEvent me)
 {
 }

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

     line l=new line();

    l.setSize(600,600);
    l.setVisible(true);
}

    }

Inheritance in Java

/**Program For Inheritance in Java**/


import java.lang.*;

class xyz
  {
       int x;
     
public void getdata()
  {
    x=5;
  }
public void putdata()
 {
   System.out.println(x);
 }
  }

class abc extends xyz
  {
       int a;

public void getdata()
{
  a=7;
}
public void putdata()
{
  System.out.println(a);
}

public static void main(String args[])throws Exception
    {  
abc a=new abc();
xyz x=new xyz();

         

x.getdata();
a.getdata();
x.putdata();
a.putdata();
 }
  }

Frame in Java

/**PROGRAM FOR FRAME (WINDOW)**/

import java.lang.*;
import java.awt.*;

class demo extends Frame
  {
   

     public static void main(String args[])throws Exception
          {
                 demo d=new demo();

    d.setSize(1600,800);

               d.setVisible(true);
          }
  }

                 

Editor in Java

/**EDITOR PROGRAM IN JAVA**/


import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;


public class editor extends Frame implements ActionListener
   {
      MenuBar mb;
      Menu m1,m2,m3,m4,m5;
      MenuItem a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,c1,c2,d1,e1,e2;
      TextArea t;

        editor()
          {
            t=new TextArea();
            mb=new MenuBar();
            m1=new Menu("FILE");
            a1=new MenuItem("NEW");
a2=new MenuItem("OPEN");
a3=new MenuItem("SAVE");
a4=new MenuItem("SAVE AS");
a5=new MenuItem("PRINT");
a6=new MenuItem("CLOSE");

         a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);

         

m2=new Menu("EDIT");
b1=new MenuItem("UNDO");
b2=new MenuItem("CUT");
b3=new MenuItem("COPY");
b4=new MenuItem("PASTE");
b5=new MenuItem("SELECT");
b6=new MenuItem("TIME");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);

m3=new Menu("FORMAT");
c1=new MenuItem("WORDWRAP");
c2=new MenuItem("FONT");

c1.addActionListener(this);
c2.addActionListener(this);

m4=new Menu("VIEW");
d1=new MenuItem("STATUS BAR");

d1.addActionListener(this);

m5=new Menu("HELP");
e1=new MenuItem("HELP TOPICS");
e2=new MenuItem("ABOUT NOTEPAD");

e1.addActionListener(this);
e2.addActionListener(this);

             setLayout(new GridLayout(1,1));

           add(t);
           mb.add(m1); mb.add(m2);  mb.add(m3); mb.add(m4);  mb.add(m5);
           m1.add(a1); m1.add(a2); m1.add(a3); m1.add(a4); m1.add(a5);m1.add(a6);

          m2.add(b1); m2.add(b2); m2.add(b3); m2.add(b4); m2.add(b5);m2.add(b6);

          m3.add(c1); m3.add(c2);

          m4.add(d1);  m5.add(e1);  m5.add(e2);

       

setMenuBar(mb);  
       
        }

    public void actionPerformed(ActionEvent ae)
            {
  if(ae.getSource()==a6)
{
   System.exit(0);
}


         }




public static void main(String args[])
  {
    editor h= new editor();
    h.setTitle("Notepad");
    h.setVisible(true);
    h.setSize(500,500);
  }

         
    }

         


Mouse handling in Java

/**PROGRAM OF MOUSE HANDLING**/

import java.lang.*;
import java.awt.*;
import java.awt.event.*;


class date1 extends Frame implements ActionListener,AdjustmentListener
  {

     Button a,b;
     Label l1,l2,l3,l4;
     TextField  t1,t2,t3,t4;
     Scrollbar s1,s2,s3;
     String g1,g2,g3;
      int da,mo,ye;
       int z,x,y;
               

     public date1()
{
  a=new Button("OK");
  b=new Button("EXIT");
  l1=new Label("DD");
  l2=new Label("MM");
  l3=new Label("YY");
  l4=new Label("DATE");

 t1=new TextField(5);
 t2=new TextField(10);
 t3=new TextField(5);
 t4=new TextField(20);

 s1=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,31);
 s2=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,12);
 s3=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1900,2100);

            setLayout(new FlowLayout());

              add(l1);  add(s1);  add(t1);  add(l2);  add(s2);  add(t2);
  add(l3);  add(s3);  add(t3);
               add(a);  add(l4);  add(t4);  add(b);

 a.addActionListener(this);
 b.addActionListener(this);

 s1.addAdjustmentListener(this);
 s2.addAdjustmentListener(this);
 s3.addAdjustmentListener(this);
         
          }

      public void actionPerformed(ActionEvent ae)
           {
   
    if(ae.getSource()==a)
                  {
                   
          g1=t1.getText();
         
         
         g2=t2.getText();  

        g3=t3.getText();
       
                     t4.setText(g1+"/"+g2+"/"+g3);
     }

  if(ae.getSource()==b)
     {
        System.exit(0);
     }
}

         public void adjustmentValueChanged(AdjustmentEvent be)
{  
 
     da=s1.getValue();
      t1.setText(" "+da);

    mo=s2.getValue();
    t2.setText(" "+mo);

     ye=s3.getValue();
     t3.setText(" "+ye);

}

public static void main(String args[])throws Exception
 {
                 date1 q=new date1();

    q.setSize(400,400);
    q.setVisible(true);
}

      }
         



MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...