Monday, 22 December 2014

AdjustmentListener in Java

//save as AdjustmentListenerDemo.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AdjustmentListenerDemo extends Applet implements AdjustmentListener
{
    Scrollbar slider;
    int x = 0;

    public void init()
    {             
        setLayout(null);

        slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 300);

        slider.setBounds(10,100,150,25);

        add(slider);
        slider.addAdjustmentListener(this);
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        x = ae.getValue();
        repaint();
    }

    public void paint(Graphics g)
    {
        g.drawString("Current Value: " + x,10,180);
    }
}


/*


*/
Output

Friday, 19 December 2014

KeyListener in Java

//save as KeyListenerDemo.java 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class KeyListenerDemo extends Applet implements KeyListener
{
    String s = "";

    public void init()
    {
        addKeyListener(this);
        requestFocus();     //request input focus.
    }
  
    //Handle when a key has been pressed...
    public void keyPressed(KeyEvent ke)
    {
        showStatus("Key Pressed...");
        repaint();
    }

    //Handle when a key has been released...
    public void keyReleased(KeyEvent ke)
    {
        showStatus("Key Released...");
        repaint();
    }
  
    //Handle when a key has been pressed...
    public void keyTyped(KeyEvent ke)
    {
        s += ke.getKeyChar();
        repaint();
    }
  
    //display keystrokes.
    public void paint(Graphics g)
    {
        g.drawString(s,x,y);
    }
}

/*


*/
Output

Tuesday, 16 December 2014

ItemListener in Java

//save as ItemListenerJavaDemo.java 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ItemListenerJavaDemo extends Applet implements ItemListener
{
    Checkbox radio1;
    Checkbox radio2;
    Checkbox radio3;
    Checkbox radio4;
    String msg = "";
  
    public void init()
    {
        radio1 = new Checkbox("Window7");
        radio2 = new Checkbox("Window8");
        radio3 = new Checkbox("MacOS");
        radio4 = new Checkbox("Vista");
      
        add(radio1);
        radio1.addItemListener(this);
        add(radio2);
        radio2.addItemListener(this);
        add(radio3);
        radio3.addItemListener(this);
        add(radio4);
        radio4.addItemListener(this);
    }
  
    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
  
    public void paint(Graphics g)
    {
        msg = "Current State: ";
        g.drawString(msg,6,100);
      
        msg = "Windows7:  " + radio1.getState();
        g.drawString(msg,6,120);
      
        msg = "Windows8:  " + radio2.getState();
        g.drawString(msg,6,140);
      
        msg = "MacOS:  " + radio3.getState();
        g.drawString(msg,6,160);
      
        msg = "Vista:  " + radio4.getState();
        g.drawString(msg,6,180);
    }
}

/*


*/
Output

Tuesday, 9 December 2014

ItemListenerDemo in Java


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

public class ItemListenerDemo extends Applet implements ItemListener
{
     CheckboxGroup radioGroup;
     Checkbox radio1;
     Checkbox radio2;
     Checkbox radio3;

    public void init()
    {
          radioGroup = new CheckboxGroup();
          radio1 = new Checkbox("Red",radioGroup,false);
          radio2 = new Checkbox("Blue",radioGroup,false);
          radio3 = new Checkbox("Green",radioGroup,false);
         
          add(radio1);
          radio1.addItemListener(this);
          add(radio2);
          radio2.addItemListener(this);
          add(radio3);
          radio3.addItemListener(this);
     }

    public void paint(Graphics g)
    {
        int x = getSize().width;
        int y = getSize().height;

        x = x/2;
        y = y/2;

        if(radioGroup.getSelectedCheckbox() == radio1)
        {
            g.setColor(Color.red);
        }
        else if(radioGroup.getSelectedCheckbox() == radio2)
        {
            g.setColor(Color.blue);
        }
        else
        {
            g.setColor(Color.green);
        }

        g.drawString("Welcome to Java...",x,y);
    }

    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
}

/*


*/
Output