Friday, 24 January 2014

Simple Layout Demo Program using Applet

import java.awt.*;
import java.applet.*;

public class layoutDemo extends Applet
{
    Button b1, b2, b3;
    public void init()
    {
        b1 = new Button("One");
        b2 = new Button("Two");
        b3 = new Button("Three");
        
        setLayout(new GridLayout(2,2));
        setLayout(new BorderLayout());
        setLayout(null);
        add(b1);      
        add(b2);
        add(b3);
        
        b1.setBounds(100,100, 100, 30);
        b2.setBounds(200,100, 100, 30);
        b3.setBounds(100,200, 100, 30);
        
    }
        
}

/*
<applet code="layoutDemo" width="500" height="500"></applet>
*/

OUTPUT



Simple Create a Frame using JAVA

import java.awt.*;

class frameDemo extends Frame
{
    Button b = new Button("Click Me!");
    
    frameDemo()
    {
        setVisible(true);
        setSize(400,400);
        setTitle("My First App");
        setLayout(new FlowLayout());
        add(b);
    }
    
    public static void main(String[] s)
    {
        new frameDemo();      
    } 
}


OUTPUT


Event Handling Mouse Motion Program

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

public class eventDemo extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    Button b = new Button("+");
    Button b2 = new Button("-");
    TextField t1 = new TextField(20);
    TextField t2 = new TextField(20);
    Label ans = new Label("Ans goes here");
    public void init()
    {
        b.addActionListener(this);
        b2.addActionListener(this);
        ans.addMouseListener(this);
        this.addMouseMotionListener(this);
        add(t1);
        add(t2);
        add(b);
        add(b2);
        add(ans);
    }
    public void actionPerformed(ActionEvent e)
    {
        int a = Integer.parseInt(t1.getText());
        int bt = Integer.parseInt(t2.getText());
        if(e.getSource()==b)
        {
            int an = a+bt;
            ans.setText(an+"");
        }
        else if(e.getSource()==b2)
        {
            int an = a-bt;
            ans.setText(an+"");
            
        }
        
    }
    
    public void mouseClicked(MouseEvent e)
    { 
        t1.setText("Clicked");
    }
    public void mouseEntered(MouseEvent e)
    {
        t1.setText("Entered");
    }
    public void mouseExited(MouseEvent e)
    {
        t1.setText("Exited");
    }
    public void mousePressed(MouseEvent e)
    {
        t1.setText("Pressed");
    }
    public void mouseReleased(MouseEvent e)
    {
        t1.setText("Released");
    }
    
    public void mouseMoved(MouseEvent e)
    {
        t2.setText(e.getX() + "," + e.getY());
    }
    public void mouseDragged(MouseEvent e)
    {
        
    }
}

/*<applet code="eventDemo" width="400" height="400"></applet>*/

OUTPUT


Write a program to make Calculator in User Interface

import java.awt.*;
import java.applet.*;

public class calc extends Applet
{
    Button[] b = new Button[16];
    TextField t = new TextField(20);
    Panel p = new Panel();
    String[] lbl= {"1","2","3","+","4","5","6","-","7","8","9","*","CE","0","/","="};
    public void init()
    {
        setLayout(new BorderLayout());
        add(t, BorderLayout.NORTH);
        
        p.setLayout(new GridLayout(4,4));
        for(int i=0;i<=15;i++)
        {
            b[i] = new Button(lbl[i]);
            p.add(b[i]);  
        }
        add(p, BorderLayout.CENTER);
        
    } 
}


/*<applet code="calc" width="200" height="400"></applet>*/


OUTPUT



















Write a Program to make AX-Ball Game

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class game extends Applet implements MouseMotionListener, Runnable
{
    int x = 0;
    int boxx = 0;
    int boxy = 0;
    Thread t;
    public void init()
    {
        t = new Thread(this);
        t.start();
        this.addMouseMotionListener(this);
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(x, 380, 100, 20);  
        g.setColor(Color.BLACK);
        g.fillRect(boxx, boxy, 50, 50);

    }
    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        repaint();
    }
    public void mouseDragged(MouseEvent e){}
    public void run()
    {
        try
        {
            while(true)
            {
                boxx = new Random().nextInt(350);                 
                for(int i=0;i<=330;i+=5)
                {
                    boxy = i; 
                    repaint();
                    t.sleep(10);
                }
                if((x<boxx && x+100 > boxx) || (boxx<x && boxx+50>x) || (x<boxx && x+100 >boxx))
                {
                    System.out.println("Yippiee");
                }
                                           
                else
                {
                    setBackground(Color.red);
                    break;
                }
            }
        }
        catch(Exception e){System.out.println(e);}
    }
}
 

/*<applet code="game" width="400" height="400"></applet>*/


OUTPUT



Simple Random Color Program using Applets in JAVA

import java.awt.*;
import java.applet.*;
import java.util.*;

public class abc extends Applet implements Runnable
{
    Thread t;
    Button btn;
    Checkbox cb, cb1;
    CheckboxGroup cbg;
    TextField tf = new TextField("Ankit",30);
    public void run()
    {
        try
        {
            while(true)
            {
                Random rnd = new Random();
                int r = rnd.nextInt(254);
                int g = rnd.nextInt(254);
                int b = rnd.nextInt(254);
                setBackground(new Color(r,g,b));
                t.sleep(1000);
            } 
        }
catch(Exception e)
        {
            System.out.println(e);
        }
    }
    
    public void init()
    {
        t = new Thread(this);
        t.start();
        cbg = new CheckboxGroup();
        btn = new Button("Click Me!");
        cb = new Checkbox("Hindi", false, cbg);
        cb1 = new Checkbox("English", false, cbg);
        
        setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        add(btn);
        add(cb);
        add(cb1);
        add(new Checkbox("French",true, cbg));
        add(tf);
    }
        
      public void paint(Graphics g)
    {
      Color redc = new Color(255, 0, 0);
        g.fillRect(100,200,100,100);  
        g.setColor(Color.blue);

        g.fillRect(300,300,50,50);
        g.drawOval(0, 0, 200, 100);
    g.fillOval(255,255,255,255);
    }
}

/*
<applet code="abc" width="500" height="500"></applet>


*/

 OutPut













Friday, 13 September 2013

Write a Program Merge Sort in C++


Merge Sort: Merge Sort algorithm is based on Divide and Conquer theory. In this methodology, unsorted array list is divided into small sub-arrays. The sub-array elements are sorted and then merge into a single sorted array. Dividing of array into sub-array is done with the help of merge-sort method and sorting sub-array element and merging it to a single sorted array is done with the help of merge method. Time complexity of merge sort algorithm is O(n) where n=r-p+1. Have a look at below diagram, it is the buttom-up view of merge sort algorithm.




 Merge Sort:Algorithm:

                        MERGE-SORT (A, p, r)
                                 1.     IF p < r                            // Check for base case
                                 2.         THEN q = FLOOR[(p + r)/2]                 // Divide step
                                 3.                 MERGE (A, p, q)                          // Conquer step.
                                 4.                 MERGE (A, q + 1, r)                     // Conquer step.
                                 5.                 MERGE (A, p, q, r)                       // Conquer step.



Program:
 






















































 Output:



Followers