Monday 23 May 2011

Layout in java


/*
   This applet demonstrates various layout managers.
   The applet itself uses a border layout with a JPanel in
   the center, a JComboBox menu to the North, and a JLabel
   to the south. The center panel uses a CardLayout.
   Each card in the card layout contains a number of
   buttons and uses a different layout manager.  The
   JComboBox menu is used to select among these cards.
   The JLabel reports events as they occur.
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutDemo extends JApplet
                          implements ActionListener, ItemListener {

   CardLayout cards;      // the layout manager for the center panel
   JPanel cardPanel;      // the center panel
   JComboBox panelChoice; // menu for selecting which card to show
   JLabel message;        // a message shown at the bottom of the applet
  
   public void init() {
  
      panelChoice = new JComboBox();  // Set up the menu
      panelChoice.setBackground(Color.white);
      panelChoice.addItem("FlowLayout");   // Add in the names of the cards.
      panelChoice.addItem("FlowLayout with Big Hgap");
      panelChoice.addItem("Vertical BoxLayout");
      panelChoice.addItem("Horizontal BoxLayout with Struts");
      panelChoice.addItem("BorderLayout");
      panelChoice.addItem("GridLayout(3,2)");
      panelChoice.addItem("GridLayout(1,0)");
      panelChoice.addItem("GridLayout(0,1)");
      panelChoice.addItemListener(this);
     
      message = new JLabel("Layout Demo", JLabel.CENTER);  // Set up the mesage
      message.setBackground(Color.white);
      message.setOpaque(true);  // so background color will show
      message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0));
      message.setForeground(Color.red);
     
      cardPanel = new JPanel();               // Set up the center panel
      cardPanel.setBackground(Color.white);
      cards = new CardLayout();
      cardPanel.setLayout(cards);
     
      setBackground(Color.blue);
      getContentPane().setBackground(Color.blue); 
      getContentPane().setLayout(new BorderLayout(3,3));   
      getContentPane().add("Center",cardPanel);
      getContentPane().add("North",panelChoice);
      getContentPane().add("South",message);
     
      JPanel panel;  // Will represent various cards to be added to the center panel.
      Box box;       // For the cards that use a BoxLayout.

      // Set up each "card" in the center panel to have its own layout
      // manager and to contain a variety of buttons.
     
      panel = new JPanel();
      // use default FlowLayout for panel
      panel.setBackground(Color.white);
      cardPanel.add(panel, "FlowLayout");
      addButton(panel,"First Button");  // ( addButton is a untility method, defined below )
      addButton(panel,"Second Button");
      addButton(panel,"Third Button");
      addButton(panel,"Fourth Button");
      addButton(panel,"Fifth Button");
      addButton(panel,"Sixth Button");
      addButton(panel,"Seventh Button");
           
      panel = new JPanel(); 
      panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5));
      panel.setBackground(Color.white);
      cardPanel.add(panel,"FlowLayout with Big Hgap");
      addButton(panel," A Button");
      addButton(panel,"Another Button");
      addButton(panel,"A Third Button");
      addButton(panel,"A Fourth Button");
      addButton(panel,"A Final Button");

      box = Box.createVerticalBox(); 
      box.setBackground(Color.white);
      cardPanel.add(box,"Vertical BoxLayout");
      addButton(box,"Button One");
      addButton(box,"Button Two");
      addButton(box,"Button Three");
      addButton(box,"Button Four");
      addButton(box,"Button Five");
      addButton(box,"Button Six");

      box = Box.createHorizontalBox(); 
      box.setBackground(Color.white);
      cardPanel.add(box,"Horizontal BoxLayout with Struts");
      addButton(box,"1st");
      addButton(box,"2nd");
      box.add( Box.createHorizontalStrut(10) );
      addButton(box,"3rd");
      addButton(box,"4th");
      box.add( Box.createHorizontalStrut(10) );
      addButton(box,"5th");

      panel = new JPanel(); 
      panel.setLayout(new BorderLayout());
      panel.setBackground(Color.white);
      cardPanel.add(panel,"BorderLayout");
      addButton(panel,"Center Button", BorderLayout.CENTER);
      addButton(panel,"North Button", BorderLayout.NORTH);
      addButton(panel,"South Button", BorderLayout.SOUTH);
      addButton(panel,"East Button", BorderLayout.EAST);
      addButton(panel,"West Button", BorderLayout.WEST);
                 
      panel = new JPanel(); 
      panel.setLayout(new GridLayout(3,2));
      panel.setBackground(Color.white);
      cardPanel.add(panel,"GridLayout(3,2)");
      addButton(panel,"Button 1");                 
      addButton(panel,"Button 2");                 
      addButton(panel,"Button 3");                 
      addButton(panel,"Button 4");                 
      addButton(panel,"Button 5");                 
      addButton(panel,"Button 6");                 
     
      panel = new JPanel(); 
      panel.setLayout(new GridLayout(1,0));
      panel.setBackground(Color.white);
      cardPanel.add(panel,"GridLayout(1,0)");
      addButton(panel,"Button 1");                 
      addButton(panel,"Button 2");                 
      addButton(panel,"Button 3");                 
      addButton(panel,"Button 4");                 
     
      panel = new JPanel(); 
      panel.setLayout(new GridLayout(0,1));
      panel.setBackground(Color.white);
      cardPanel.add(panel,"GridLayout(0,1)");
      addButton(panel,"Button 1");                 
      addButton(panel,"Button 2");                 
      addButton(panel,"Button 3");                 
      addButton(panel,"Button 4");                 
      addButton(panel,"Button 5");                 
      addButton(panel,"Button 6");                 
     
   } // end init()


   public Insets getInsets() {
        // specify borders around the edges of the applet
      return new Insets(3,3,3,3);
   }
  
  
   void addButton(Container p, String name) {
          // Create a button with the given name and add it
          // to the given panel.  Set up the button to send
          // events to the applet.
      JButton b = new JButton(name);
      p.add(b);
      b.addActionListener(this);
   }
     

   void addButton(JPanel p, String name, Object option) {
          // Same as above, but use the "option" object
          // as an additional parameter in the add method.
      JButton b = new JButton(name);
      p.add(b, option);
      b.addActionListener(this);
   }
     

   public void actionPerformed(ActionEvent evt) {
         // A button was pressed.  Report the name
         // of the button by setting the message text.
      String buttonName = evt.getActionCommand();
      message.setText("Button \"" + buttonName + "\" was pressed.");
   }
  
   public void itemStateChanged(ItemEvent evt) {
         // The user has selected an item from the JComboBox.
         // Change the displayed card to match.
      String panelName = (String)panelChoice.getSelectedItem();
      cards.show(cardPanel, panelName);
      message.setText("Panel \"" + panelName + "\" was selected.");
   }

  
} // end class LayoutDemo

Simple Dialog Demo java program


/*
     This applet demonstrates four easy-to-use routines for
     showing a dialog box and, in three cases, getting back
     some information from the user.  The methods are:
    
         JOptionPane.showMessageDialog
         JOptionPane.showConfirmDialog
         JOptionPane.showInputDialog
         JColorChooser.showDialog
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleDialogDemo extends JApplet implements ActionListener {

   JLabel message;  // A label for giving some feedback to the user.
                    // It appears at the top of the applet.

   Color selectedColor = Color.gray; // This color will be used as the
                                     // initial color in the color chooser.
                                     // It is used to rememeber the user's
                                     // color choice, so that the color
                                     // chooser can show the same color,
                                     // if it is opened twice.
                                  

   public void init() {
          // Set up the applet with a message label and four buttons.
          // Each button will open a different type of dialog.

       setBackground(Color.gray);
       getContentPane().setBackground(Color.gray);
       getContentPane().setLayout( new GridLayout(3,1,3,3) );
       message = new JLabel("Click a button to open a dialog", JLabel.CENTER);
       message.setForeground(new Color(180,0,0));
       message.setBackground(Color.white);
       message.setOpaque(true);
       getContentPane().add(message);

       JPanel buttonBar;
       JButton button;

       buttonBar = new JPanel();
       buttonBar.setLayout(new GridLayout(1,2,3,3));
       buttonBar.setBackground(Color.gray);
       getContentPane().add(buttonBar);
       button = new JButton("Message Dialog");
       button.addActionListener(this);
       buttonBar.add(button);
       button = new JButton("Confirm Dialog");
       button.addActionListener(this);
       buttonBar.add(button);

       buttonBar = new JPanel();
       buttonBar.setLayout(new GridLayout(1,2,3,3));
       buttonBar.setBackground(Color.gray);
       getContentPane().add(buttonBar);
       button = new JButton("Input Dialog");
       button.addActionListener(this);
       buttonBar.add(button);
       button = new JButton("Color Chooser");
       button.addActionListener(this);
       buttonBar.add(button);

   } // end init()
  

   public Insets getInsets() {
         // Leave a gray border around the applet.
      return new Insets(3,3,3,3);
   }
  

   public void actionPerformed(ActionEvent evt) {
          // Respond to a button click by showing a dialog
          // and setting the message label to describe the
          // user's response.

      String command = evt.getActionCommand();

      if (command.equals("Message Dialog")) {
         message.setText("Displaying message dialog.");
         JOptionPane.showMessageDialog(null,
             "This is an example of JOptionPane.showMessageDialog.");
         message.setText("You closed the message dialog.");
      }

      else if (command.equals("Confirm Dialog")) {
         message.setText("Displaying confirm dialog.");
         int response = JOptionPane.showConfirmDialog(null,
             "This is an example of JOptioPane.showConfirmDialog.\n"
               + "Click any button to indicate your response.");
         switch(response) {
            case JOptionPane.YES_OPTION:
               message.setText("You clicked \"Yes\".");
               break;
            case JOptionPane.NO_OPTION:
               message.setText("You clicked \"No\".");
               break;
            case JOptionPane.CANCEL_OPTION:
               message.setText("You clicked \"Cancel\".");
               break;
            case JOptionPane.CLOSED_OPTION:
               message.setText("You closed the box without making a selection.");
         }
      }

      else if (command.equals("Input Dialog")) {
         message.setText("Displaying input dialog.");
         String response = JOptionPane.showInputDialog(null,
             "This is an example of JOptioPane.showInputDialog.\n"
               + "Type your response, and click a button.");
         if (response == null)
            message.setText("You canceled the input.");
         else if (response.trim().length() == 0)
            message.setText("You left the input box empty.");
         else
            message.setText("You entered \"" + response + "\".");
      }

      else if (command.equals("Color Chooser")) {
         message.setText("Displaying color chooser dialog.");
         Color c = JColorChooser.showDialog(null,"Select a Color",selectedColor);
         if (c == null)
            message.setText("You canceled without selecting a color.");
         else {
            selectedColor = c;  // Remember selected color for next time.
            int r = c.getRed();
            int g = c.getGreen();
            int b = c.getBlue();
            message.setText("You selected RGB = (" + r + "," + g + "," + b + ").");
         }
      }

   } // end actionPerformed()


} // end class SimpleDialogDemo

Shape drawing menu example in java


/*
    The ShapeDrawWithMenus applet lets the user add colored shapes to
    a drawing area and then drag them around.  The shapes are rectangles,
    ovals, and roundrects.  The user adds a shape to the canvas using
    an "Add" menu.  The shape is added at the upper left corner
    of the canvas.  The color of the shape is given by the current
    setting of the "Color" menu.  The shapes come in two sizes,
    and they come with or without black borders.  These two options
    are controlled by items in the "Options" menu.  The "Options"
    menu also has an item for clearing all the shapes, and it has
    a sub-menu for changing the background color.  Finally, there
    is a pop-up menu that will appear if the user clicks on a shape
    in a certain way.  (The exact action depends on the look-and-feel.
    It might require right-clicking or control-clicking, for example.)
    The pop-up menu contains commands for editing the shape, for
    deleting it and for bringing it to the front of all the other
    shapes.  (The shape will also be moved to the front if the
    user shift-clicks on it.)
   
    This file defines the applet class plus several other classes used
    by the applet, namely:  ShapeCanvas, Shape, RectShape, OvalShape,
    and RoundRectShape.  These classes are nested inside the main class,
    so the compiled class files for these classes will have named like
    ShapeDraw$ShapeCanvas.class.  All the class files are necessary to
    run the applet.
   
    This applet requires Java version 1.2 or higher.
   
    David Eck
    June 2002
    Based on ShapeDraw.java
   
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;


public class ShapeDrawWithMenus extends JApplet {

   // The following variables are declared as instance variables
   // since the objects are created in the init() method but are
   // used in the ShapeCanvas class.

   JCheckBoxMenuItem addLargeShapes;  // Controls whether newly added
                                      // shapes will be large or small.
  
   JCheckBoxMenuItem addBorderedShapes;  // Controls whether newly added
                                         // will have a black border.
  
   JRadioButtonMenuItem red, green, blue,       // Control the color
                        cyan, magenta, yellow,  //    of newly added shapes.
                        black, gray, white;
  
   JPopupMenu popup;  // The pop-up menu, which is used for editing shapes.
  

   public void init() { 
        // Set up the applet's GUI.  It consists of a canvas, or drawing area
        // and a menu bar.  The pop-up menu is also created here.
  
      setBackground(Color.gray);  // Will appear in the Insets area, around
                                  // the edges of the JApplet.
     

      /* Create the "canvas" which displays the shapes and serves as
         the content pane of the applet. */
     
      ShapeCanvas canvas = new ShapeCanvas();
      setContentPane(canvas);
     

      /* Create the menu bar and the menus */
     
      JMenuBar menubar = new JMenuBar();
      setJMenuBar(menubar);
     
      JMenu addShapeMenu = new JMenu("Add");
      addShapeMenu.setMnemonic('A');
      menubar.add(addShapeMenu);
      JMenu shapeColorMenu = new JMenu("Color");
      shapeColorMenu.setMnemonic('C');
      menubar.add(shapeColorMenu);
      JMenu optionsMenu = new JMenu("Options");
      optionsMenu.setMnemonic('O');
      menubar.add(optionsMenu);


      /* Create menu items for adding shapes to the canvas,
         and add them to the "Add" menu.  The canvas serves
         as ActionListener for these menu items. */     
     
      JMenuItem rect = new JMenuItem("Rectangle");
      rect.setAccelerator( KeyStroke.getKeyStroke("ctrl R") );
      addShapeMenu.add(rect);
      rect.addActionListener(canvas);
      JMenuItem oval = new JMenuItem("Oval");
      oval.setAccelerator( KeyStroke.getKeyStroke("ctrl O") );
      addShapeMenu.add(oval);
      oval.addActionListener(canvas);
      JMenuItem roundrect = new JMenuItem("Round Rect");
      roundrect.setAccelerator( KeyStroke.getKeyStroke("ctrl D") );
      addShapeMenu.add(roundrect);
      roundrect.addActionListener(canvas);
     

      /* Create the JRadioButtonMenuItems that control the color
         of a newly added shape, and add them to the "Color"
         menu.  There is no ActionListener for these menu items.
         The canvas checks for the currently selected color when
         it adds a shape to the canvas.  A ButtonGroup is used
         to make sure that only one color is selected. */
     
      ButtonGroup colorGroup = new ButtonGroup();
      red = new JRadioButtonMenuItem("Red");
      shapeColorMenu.add(red);
      colorGroup.add(red);
      red.setSelected(true);
      green = new JRadioButtonMenuItem("Green");
      shapeColorMenu.add(green);
      colorGroup.add(green);
      blue = new JRadioButtonMenuItem("Blue");
      shapeColorMenu.add(blue);
      colorGroup.add(blue);
      cyan = new JRadioButtonMenuItem("Cyan");
      shapeColorMenu.add(cyan);
      colorGroup.add(cyan);
      magenta = new JRadioButtonMenuItem("Magenta");
      shapeColorMenu.add(magenta);
      colorGroup.add(magenta);
      yellow = new JRadioButtonMenuItem("Yellow");
      shapeColorMenu.add(yellow);
      colorGroup.add(yellow);
      black = new JRadioButtonMenuItem("Black");
      shapeColorMenu.add(black);
      colorGroup.add(black);
      gray = new JRadioButtonMenuItem("Gray");
      shapeColorMenu.add(gray);
      colorGroup.add(gray);
      white = new JRadioButtonMenuItem("White");
      shapeColorMenu.add(white);
      colorGroup.add(white);
     
     
      /* Create the "Clear" menu item, and add it to the
         "Options" menu.  The canvas will listen for events
         from this menu item. */

      JMenuItem clear = new JMenuItem("Clear");
      clear.setAccelerator( KeyStroke.getKeyStroke("ctrl C") );
      clear.addActionListener(canvas);
      optionsMenu.add(clear);
      optionsMenu.addSeparator();  // Add a separating line to the menu.
     
     
      /* Create the JCheckBoxMenuItems and add them to the Options
         menu.  There is no ActionListener for these items because
         the canvas class will check their state when it adds a
         new shape. */
     
      addLargeShapes = new JCheckBoxMenuItem("Add Large Shapes");
      addLargeShapes.setSelected(true);
      optionsMenu.add(addLargeShapes);
      addBorderedShapes = new JCheckBoxMenuItem("Add Shapes with Border");
      addBorderedShapes.setSelected(true);
      optionsMenu.add(addBorderedShapes);
      optionsMenu.addSeparator();
     
     
      /* Create a menu for background colors, and add it to the
         "Options" menu.  It will show up as a hierarchical sub-menu. */
     
      JMenu background = new JMenu("Background Color");
      optionsMenu.add(background);
      background.add("Red").addActionListener(canvas);
      background.add("Green").addActionListener(canvas);
      background.add("Blue").addActionListener(canvas);
      background.add("Cyan").addActionListener(canvas);
      background.add("Magenta").addActionListener(canvas);
      background.add("Yellow").addActionListener(canvas);
      background.add("Black").addActionListener(canvas);
      background.add("Gray").addActionListener(canvas);
      background.add("White").addActionListener(canvas);
     

      /* Create the pop-up menu and add commands for editing a
         shape.  This menu is not used until the user performs
         the pop-up trigger mouse gesture on a shape. */
        
      popup = new JPopupMenu();
      popup.add("Delete Shape").addActionListener(canvas);
      popup.add("Bring to Front").addActionListener(canvas);
      popup.addSeparator();
      popup.add("Make Large").addActionListener(canvas);
      popup.add("Make Small").addActionListener(canvas);
      popup.addSeparator();
      popup.add("Add Black Border").addActionListener(canvas);
      popup.add("Remove Black Border").addActionListener(canvas);
      popup.addSeparator();
      popup.add("Set Color to Red").addActionListener(canvas);
      popup.add("Set Color to Green").addActionListener(canvas);
      popup.add("Set Color to Blue").addActionListener(canvas);
      popup.add("Set Color to Cyan").addActionListener(canvas);
      popup.add("Set Color to Magenta").addActionListener(canvas);
      popup.add("Set Color to Yellow").addActionListener(canvas);
      popup.add("Set Color to Black").addActionListener(canvas);
      popup.add("Set Color to Gray").addActionListener(canvas);
      popup.add("Set Color to White").addActionListener(canvas);
     
   } // end init()
  
   public Insets getInsets() {
         // Allows 3 pixels of extra space around the edges of the applet
         // that will appear in the background color.
      return new Insets(3,3,3,3);
   } // end getInsets()
  
  
   //---- Nested class definitions ---
   //
   // The remainder of the ShapeDraw class consists of static nested class definitions.
   // These are just like regular classes, except that they are defined inside
   // another class (and hence have full names, when used outside this class, such
   // as ShapeDraw.ShapeCanvas).

   class ShapeCanvas extends JPanel
                     implements ActionListener, MouseListener, MouseMotionListener {

         // This class represents a "canvas" that can display colored shapes and
         // let the user drag them around.  It uses an off-screen images to
         // make the dragging look as smooth as possible.

      ArrayList shapes = new ArrayList();
           // holds a list of the shapes that are displayed on the canvas


      ShapeCanvas() {
           // Constructor: set background color to white
           // set up listeners to respond to mouse actions
         setBackground(Color.white);
         addMouseListener(this);
         addMouseMotionListener(this);
      }  

      public void paintComponent(Graphics g) {
           // In the paint method, all the shapes in ArrayList are
           // copied onto the canvas.
         super.paintComponent(g);  // First, fill with background color.
         int top = shapes.size();
         for (int i = 0; i < top; i++) {
            Shape s = (Shape)shapes.get(i);
            s.draw(g);
         }
      }  

      public void actionPerformed(ActionEvent evt) {
             // Called to respond to action events from the
             // menus or pop-up menu.
         String command = evt.getActionCommand();
         if (command.equals("Clear")) {
            shapes.clear(); // Remove all items from the ArrayList
            repaint();
         }
         else if (command.equals("Rectangle"))
            addShape(new RectShape());
         else if (command.equals("Oval"))
            addShape(new OvalShape());
         else if (command.equals("Round Rect"))
            addShape(new RoundRectShape());
         else if (command.equals("Red"))
            setBackground(Color.red);
         else if (command.equals("Green"))
            setBackground(Color.green);
         else if (command.equals("Blue"))
            setBackground(Color.blue);
         else if (command.equals("Cyan"))
            setBackground(Color.cyan);
         else if (command.equals("Magenta"))
            setBackground(Color.magenta);
         else if (command.equals("Yellow"))
            setBackground(Color.yellow);
         else if (command.equals("Black"))
            setBackground(Color.black);
         else if (command.equals("Gray"))
            setBackground(Color.gray);
         else if (command.equals("White"))
            setBackground(Color.white);
         else if (clickedShape != null) {
                // Process a command from the pop-up menu.
            if (command.equals("Delete Shape"))
               shapes.remove(clickedShape);
            else if (command.equals("Bring to Front")) {
               shapes.remove(clickedShape);
               shapes.add(clickedShape); 
            }
            else if (command.equals("Make Large"))
               clickedShape.setSize(100,60);
            else if (command.equals("Make Small"))
               clickedShape.setSize(50,30);
            else if (command.equals("Add Black Border"))
               clickedShape.setDrawOutline(true);
            else if (command.equals("Remove Black Border"))
               clickedShape.setDrawOutline(false);
            else if (command.equals("Set Color to Red"))
               clickedShape.setColor(Color.red);
            else if (command.equals("Set Color to Green"))
               clickedShape.setColor(Color.green);
            else if (command.equals("Set Color to Blue"))
               clickedShape.setColor(Color.blue);
            else if (command.equals("Set Color to Cyan"))
               clickedShape.setColor(Color.cyan);
            else if (command.equals("Set Color to Magenta"))
               clickedShape.setColor(Color.magenta);
            else if (command.equals("Set Color to Yellow"))
               clickedShape.setColor(Color.yellow);
            else if (command.equals("Set Color to Black"))
               clickedShape.setColor(Color.black);
            else if (command.equals("Set Color to Gray"))
               clickedShape.setColor(Color.gray);
            else if (command.equals("Set Color to White"))
               clickedShape.setColor(Color.white);
            repaint();
         }
      } // end actionPerformed()


      void addShape(Shape shape) {
             // Add the shape to the canvas, and set its size, color
             // and whether or not it should have a black border.  These
             // properties are determined by looking at the states of
             // various menu items.  The shape is added at the top-left
             // corner of the canvas.
         if (red.isSelected())
            shape.setColor(Color.red);
         else if (blue.isSelected())
            shape.setColor(Color.blue);
         else if (green.isSelected())
            shape.setColor(Color.green);
         else if (cyan.isSelected())
            shape.setColor(Color.cyan);
         else if (magenta.isSelected())
            shape.setColor(Color.magenta);
         else if (yellow.isSelected())
            shape.setColor(Color.yellow);
         else if (black.isSelected())
            shape.setColor(Color.black);
         else if (white.isSelected())
            shape.setColor(Color.white);
         else
            shape.setColor(Color.gray);
         shape.setDrawOutline( addBorderedShapes.isSelected() );
         if (addLargeShapes.isSelected())
            shape.reshape(3,3,100,60);
         else
            shape.reshape(3,3,50,30);
         shapes.add(shape);
         repaint();
      } // end addShape()


      // -------------------- This rest of this class implements dragging ----------------------
     
      Shape clickedShape = null;  // This is the shape that the user clicks on.
                                  // It becomes the draggedShape is the user is
                                  // dragging, unless the user is invoking a
                                  // pop-up menu.  This variable is used in
                                  // actionPerformed() when a command from the
                                  // pop-up menu is processed.

      Shape draggedShape = null;  // This is null unless a shape is being dragged.
                                  // A non-null value is used as a signal that dragging
                                  // is in progress, as well as indicating which shape
                                  // is being dragged.

      int prevDragX;  // During dragging, these record the x and y coordinates of the
      int prevDragY;  //    previous position of the mouse.
     
      public void mousePressed(MouseEvent evt) {
            // User has pressed the mouse.  Find the shape that the user has clicked on, if
            // any.  If there is no shape at the position when the mouse was clicked, then
            // ignore this event.  If there is then one of three things will happen:
            // If the event is a pop-up trigger, then the pop-up menu is displayed, and
            // the user can select from the pop-up menu to edit the shape.  If the user was
            // holding down the shift key, then bring the clicked shape to the front, in
            // front of all the other shapes.  Otherwise, start dragging the shape.
         if (draggedShape != null) {
              // A drag operation is already in progress, so ignore this click.
              // This might happen if the user clicks a second mouse button before
              // releasing the first one(?).
            return;
         }
         int x = evt.getX();  // x-coordinate of point where mouse was clicked
         int y = evt.getY();  // y-coordinate of point
         clickedShape = null;  // This will be set to the clicked shape, if any.
         for ( int i = shapes.size() - 1; i >= 0; i-- ) { 
                // Check shapes from front to back.
            Shape s = (Shape)shapes.get(i);
            if (s.containsPoint(x,y)) {
               clickedShape = s;
               break;
            }
         }
         if (clickedShape == null) {
               // The user did not click on a shape.
            return;
         }
         else if (evt.isPopupTrigger()) {
              // The user wants to see the pop-up menu
            popup.show(this,x-10,y-2);
         }
         else if (evt.isShiftDown()) {
              // Bring the clicked shape to the front
            shapes.remove(clickedShape);
            shapes.add(clickedShape);
            repaint();
         }
         else {
              // Start dragging the shape.
            draggedShape = clickedShape;
            prevDragX = x;
            prevDragY = y;
         }
      }

      public void mouseDragged(MouseEvent evt) {
             // User has moved the mouse.  Move the dragged shape by the same amount.
         if (draggedShape == null) {
                // User did not click a shape.  There is nothing to do.
            return;
         }
         int x = evt.getX();
         int y = evt.getY();
         draggedShape.moveBy(x - prevDragX, y - prevDragY);
         prevDragX = x;
         prevDragY = y;
         repaint();      // redraw canvas to show shape in new position
      }

      public void mouseReleased(MouseEvent evt) {
             // User has released the mouse.  Move the dragged shape, and set
             // draggedShape to null to indicate that dragging is over.
             // If the shape lies completely outside the canvas, remove it
             // from the list of shapes (since there is no way to ever move
             // it back on screen).  However, if the event is a popup trigger
             // event, then show the popup menu instead.
         if (draggedShape == null) {
               // User did not click on a shape. There is nothing to do.
            return;
         }
         int x = evt.getX();
         int y = evt.getY();
         if (evt.isPopupTrigger()) {
               // Check whether the user is trying to pop up a menu.
               // (This should be checked in both the mousePressed() and
               // mouseReleased() methods.)
            popup.show(this,x-10,y-2);
         }
         else {
            draggedShape.moveBy(x - prevDragX, y - prevDragY);
            if ( draggedShape.left >= getSize().width || draggedShape.top >= getSize().height ||
                    draggedShape.left + draggedShape.width < 0 ||
                    draggedShape.top + draggedShape.height < 0 ) {  // shape is off-screen
               shapes.remove(draggedShape);  // remove shape from list of shapes
            }
            repaint();
         }
         draggedShape = null;  // Dragging is finished.
      }

      public void mouseEntered(MouseEvent evt) { }   // Other methods required for MouseListener and
      public void mouseExited(MouseEvent evt) { }    //              MouseMotionListener interfaces.
      public void mouseMoved(MouseEvent evt) { }
      public void mouseClicked(MouseEvent evt) { }

   }  // end class ShapeCanvas


   // ------- Nested class definitions for the abstract Shape class and three -----
   // -------------------- concrete subclasses of Shape. --------------------------


   static abstract class Shape {

         // A class representing shapes that can be displayed on a ShapeCanvas.
         // The subclasses of this class represent particular types of shapes.
         // When a shape is first constructed, it has height and width zero
         // and a default color of white.

      int left, top;      // Position of top left corner of rectangle that bounds this shape.
      int width, height;  // Size of the bounding rectangle.
      Color color = Color.white;  // Color of this shape.
      boolean drawOutline;  // If true, a black border is drawn on the shape

      void reshape(int left, int top, int width, int height) {
            // Set the position and size of this shape.
         this.left = left;
         this.top = top;
         this.width = width;
         this.height = height;
      }
     
      void setSize(int width, int height) {
            // Set the size of this shape
         this.width = width;
         this.height = height;
      }

      void moveBy(int dx, int dy) {
             // Move the shape by dx pixels horizontally and dy pixels vertically
             // (by changing the position of the top-left corner of the shape).
         left += dx;
         top += dy;
      }

      void setColor(Color color) {
             // Set the color of this shape
         this.color = color;
      }
     
      void setDrawOutline(boolean draw) {
             // If true, a black outline is drawn around this shape.
         drawOutline = draw;
      }

      boolean containsPoint(int x, int y) {
            // Check whether the shape contains the point (x,y).
            // By default, this just checks whether (x,y) is inside the
            // rectangle that bounds the shape.  This method should be
            // overridden by a subclass if the default behavior is not
            // appropriate for the subclass.
         if (x >= left && x < left+width && y >= top && y < top+height)
            return true;
         else
            return false;
      }

      abstract void draw(Graphics g); 
            // Draw the shape in the graphics context g.
            // This must be overridden in any concrete subclass.

   }  // end of class Shape



   static class RectShape extends Shape {
         // This class represents rectangle shapes.
      void draw(Graphics g) {
         g.setColor(color);
         g.fillRect(left,top,width,height);
         if (drawOutline) {
            g.setColor(Color.black);
            g.drawRect(left,top,width,height);
         }
      }
   }


   static class OvalShape extends Shape {
          // This class represents oval shapes.
      void draw(Graphics g) {
         g.setColor(color);
         g.fillOval(left,top,width,height);
         if (drawOutline) {
            g.setColor(Color.black);
            g.drawOval(left,top,width,height);
         }
      }
      boolean containsPoint(int x, int y) {
            // Check whether (x,y) is inside this oval, using the
            // mathematical equation of an ellipse.
         double rx = width/2.0;   // horizontal radius of ellipse
         double ry = height/2.0;  // vertical radius of ellipse
         double cx = left + rx;   // x-coord of center of ellipse
         double cy = top + ry;    // y-coord of center of ellipse
         if ( (ry*(x-cx))*(ry*(x-cx)) + (rx*(y-cy))*(rx*(y-cy)) <= rx*rx*ry*ry )
            return true;
         else
           return false;
      }
   }


   static class RoundRectShape extends Shape {
          // This class represents rectangle shapes with rounded corners.
          // (Note that it uses the inherited version of the
          // containsPoint(x,y) method, even though that is not perfectly
          // accurate when (x,y) is near one of the corners.)
      void draw(Graphics g) {
         g.setColor(color);
         g.fillRoundRect(left,top,width,height,width/3,height/3);
         if (drawOutline) {
            g.setColor(Color.black);
            g.drawRoundRect(left,top,width,height,width/3,height/3);
         }
      }
   }


}  // end class ShapeDrawWithMenus

Creation of Multiple Threads in java

class MyThread1 implements Runnable
{
    Thread t;
    MyThread1(String s)
    {

        t=new Thread(this,s);
        t.start();

    }
   
    public void run()
    {
        for(int i=0;i<5;i++)
        {
            System.out.println("Thread Name  :"+Thread.currentThread().getName());
            try
            {
            Thread.sleep(1000);
            }catch(Exception e){}
        }
    }
}

public class RunnableThread1
{
    public static void main(String args[])
    {
        System.out.println("Thread Name :"+Thread.currentThread().getName());  
        MyThread1 m1=new MyThread1("My Thread 1");
        MyThread1 m2=new MyThread1("My Thread 2");

    }

}

animation in java

/*
   This applet draws a representation of the famous Mandelbrot
   set.  It's real purpose, however, is to demonstrate the
   use of a separated thread to do long computations.  The
   thread is not started until the user clicks a "Start" button.
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mandelbrot extends JApplet {

   Display canvas;  // The drawing surface on which the Mandelbrot
                    // set is displayed.
                   
   JButton stopButton, startButton;  // Computation will start when
                                     // Start button is pressed and will
                                     // continue until it finishes or the
                                     // user presses the "Stop" button.

   public void init() {
          // Initialize the applet by creating the canvas
          // and buttons and adding them to the applet's
          // content pane.
     
      setBackground(Color.gray);
     
      canvas = new Display();
      getContentPane().add(canvas, BorderLayout.CENTER);
     
      JPanel bottom = new JPanel();
      bottom.setBackground(Color.gray);
      startButton = new JButton("Start");
      startButton.addActionListener(canvas);
      bottom.add(startButton);
      stopButton = new JButton("Stop");
      stopButton.addActionListener(canvas);
      bottom.add(stopButton);
      stopButton.setEnabled(false);
     
      getContentPane().add(bottom, BorderLayout.SOUTH);
     
   } // end init();
  
  
   public Insets getInsets() {
         // Leave space around the applet that will show in the
         // background color, gray.
      return new Insets(2,2,2,2);
   }
  
   public void stop() {
         // This method is called by the system when the applet
         // is about to be temporarily or permanently stopped.
         // To the canvas to stop the computation thread, if
         // it is running.
      canvas.stopRunning();
   }


   // --- The following nested class represents the drawing surface
   // --- of the applet and also does all the work.
  
   class Display extends JPanel implements ActionListener, Runnable {
  
      Image OSI;    // An off-screen images that holds the picture
                    //    of the Mandelbrot set.  This is copied onto
                    //    the drawing surface, if it exists.  It is
                    //    created by the computational thread.
                   
      Graphics OSG; // A graphics context for drawing on OSI.

      Thread runner;    // A thread to do the computation.
      boolean running;  // This is set to true when the thread is running.
     
      double xmin = -2.5;   // The ranges of x and y coordinates that
      double xmax = 1;      //    are represented by this drawing surface
      double ymin = -1.25;
      double ymax = 1.25;
  
      public void paintComponent(Graphics g) {
            // Called by the system to paint the drawing surface.
            // This copies the off-screen image onto the screen,
            // if the off-screen image exists.  If not, it just
            // fills the drawing surface with black.
         if (OSI == null) {
            g.setColor(Color.black);
            g.fillRect(0,0,getWidth(),getHeight());
         }
         else {
            g.drawImage(OSI,0,0,null);
         }
      }
     
      public void actionPerformed(ActionEvent evt) {
            // This will be called when the user clicks on
            // the "Start" or "Stop" button.  It responds
            // by starting or stopping the animation.
         String command = evt.getActionCommand();
         if (command.equals("Start"))
            startRunning();
         else if (command.equals("Stop"))
            stopRunning();
      }
     
      void startRunning() {
           // A simple method that starts the computational thread,
           // unless it is already running.  (This should be
           // impossible since this method is only called when
           // the user clicks the "Start" button, and that button
           // is disabled when the thread is running.)
         if (running)
            return;
         runner = new Thread(this);
              // Creates a thread that will execute the run()
              // method in this Display class.
         running = true;
         runner.start();
      }
     
      void stopRunning() {
           // A simple method that is called to stop the computational
           // thread.  This is done by setting the value of the
           // variable, running.  The thread checks this value
           // regularly and will terminate when running becomes false.
         running = false;
      }
     
      int countIterations(double x, double y) {
            // The Mandelbrot set is represented by coloring
            // each point (x,y) according to the number of
            // iterations it takes before the while loop in
            // this method ends.  For points that are actually
            // in the Mandelbrot set, or very close to it, the
            // count will reach the maximum value, 80.  These
            // points will be colored purple.  All other colors
            // represent points that are definitely NOT in the set.
         int count = 0;
         double zx = x;
         double zy = y;
         while (count < 80 && Math.abs(x) < 100 && Math.abs(zy) < 100) {
            double new_zx = zx*zx - zy*zy + x;
            zy = 2*zx*zy + y;
            zx = new_zx;
            count++;
         }
         return count;
      }
     
      int i,j;   // The center pixel of a square that needs to be
                 //    drawing.  These variables are set in the
                 //    run() method of the Display class and are
                 //    used in the run() method of the painter object.
                 //    The same is true for the next two variables.

      int size;  // The size of the square that needs to be drawn.

      int colorIndex;  // A number between 1 and 80 that is used
                       //    to decide on the color of the square.
     
      Runnable painter = new Runnable() {
               // A Runnable object whose job is to paint a
               // square onto the off-screen canvas, and then
               // copy that square onto the screen.  It will do
               // this when its run method is called.  The data
               // for the square are given by the preceding four
               // variables.
            public void run() {
               int left = i - size/2;
               int top = j - size/2;
               OSG.setColor( Color.getHSBColor(colorIndex/100.0F,1F,1F) );
               OSG.fillRect(left,top,size,size);
               paintImmediately(left,top,size,size);
            }
        };
     

      public void run() {
            // This is the run method that is executed by the
            // computational thread.  It draws the Mandelbrot
            // set in a series of passes of increasing resolution.
            // In each pass, it fills the applet with squares
            // that are colored to represent the Mandelbrot set.
            // The size of the squares is cut in half on each pass.

         startButton.setEnabled(false);  // Disable "Start" button
         stopButton.setEnabled(true);    //    and enable "Stop" button
                                         //    while thread is running.

         int width = getWidth();   // Current size of this canvas.
         int height = getHeight();

         OSI = createImage(getWidth(),getHeight());
             // Create the off-screen image where the picture will
             // be stored, and fill it with black to start.
         OSG = OSI.getGraphics();
         OSG.setColor(Color.black);
         OSG.fillRect(0,0,width,height);
        
         for (size = 64; size >= 1 && running; size = size/2) {
               // Outer for loop performs one pass, filling
               // the image with squares of the given size.
               // The size here is given in terms of pixels.
               // Note that all loops end immediately if running
               // becomes false.
            double dx,dy;  // Size of square in real coordinates.
            dx = (xmax - xmin)/width * size;
            dy = (ymax - ymin)/height * size;
            double x = xmin + dx/2;  // x-coord of center of square.
            for (i = size/2; i < width+size/2 && running; i += size) {
                  // First nested for loop draws one column of squares.
               double y = ymax - dy/2; // y-coord of center of square
               for (j = size/2; j < height+size/2 && running; j += size) {
                      // Innermost for loop draws one square, by
                      // counting iterations to determine what
                      // color it should be, and then invoking the
                      // "painter" object to actually draw the square.
                   colorIndex = countIterations(x,y);
                   try {
                      SwingUtilities.invokeAndWait(painter);
                   }
                   catch (Exception e) {
                   }
                   y -= dy;
               }
               x += dx;
               Thread.yield();  // Give other threads a chance to run.
            }
         }

         running = false;  // The thread is about to end, either
                           // because the computation is finished
                           // or because running has been set to
                           // false elsewhere.  In the former case,
                           // we have to set running = false here
                           // to indicate that the thread is no
                           // longer running.

         startButton.setEnabled(true);  // Reset states of buttons.
         stopButton.setEnabled(false);

      } // end run()


   } // end nested class Display
  

} // end class Mandelbrot