Friday, 20 January 2017

Practical 9: Create an application that display a frame with a menu bar.When a user selects any menu or menu Item,display that selection on a text area in the center of the frame.

Code:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.*;



/**
 *
 * @author Queue
 */
public class Prac9 extends JFrame implements ActionListener {
    
     private JMenuItem newOne;
    private JMenuItem save;
    private JMenuItem SaveAs;
    private JMenuItem exit;
    private JTextArea l;
    public Prac9()
    {
        FlowLayout f=new FlowLayout();
        
        JMenuBar menubar=new JMenuBar();
        JMenu menu=new JMenu("File");
         l=new JTextArea(null,1,5);
         
         JPanel p=new JPanel();
        add(p);
     
       l.setLayout(new FlowLayout());
        p.add(l);
        
        
        
         newOne=new JMenuItem("new");
        save=new JMenuItem("save");
         SaveAs=new JMenuItem("SaveAs");
         exit=new JMenuItem("Exit");
         
        
         newOne.setMnemonic('n');
         save.setMnemonic('s');
         SaveAs.setMnemonic('d');
         exit.setMnemonic('x');
         
         newOne.setAccelerator(KeyStroke.getKeyStroke('E',Event.CTRL_MASK));
         save.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
         SaveAs.setAccelerator(KeyStroke.getKeyStroke('D',Event.CTRL_MASK));
         exit.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK));
         
         
        menubar.add(menu);
        menu.add(newOne);
        menu.add(save);
        menu.add(SaveAs);
        menu.add(exit);
        
        newOne.addActionListener(this);
        save.addActionListener(this);
        SaveAs.addActionListener(this);
        exit.addActionListener(this);
        
        
        
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setJMenuBar(menubar);
        setSize(300,300);
        //setLayout(null);
        setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()== newOne)
        {
            l.setText("new");
        }
        else if(e.getSource()== save)
        {
            l.setText("save");
        }
        else if(e.getSource()== SaveAs)
        {
            l.setText("saveAS");
        }
        else if(e.getSource()== exit)
        {
            l.setText("exit");
        }
        
    }
            
public static void main(String args[])
{
    
    Prac9 p=new Prac9();
    
}
    
}

Output







Happy Coding 

Thanks ;)


Wednesday, 18 January 2017

Practical 8: Develop an Program that contrains Three Check boxes and 30*30 canvas.The three checkboxes should be laveles "RED" "GREEN" "BLUE" .The selection of the check boxes determine the color of the canvas.For Example , if the user select both "RED" and "BLUE" ,the canvas should be "MAGENTA".

CODE:



import java.awt.Canvas;
import java.awt.Color;
import static java.awt.Color.blue;
import static java.awt.Color.green;
import static java.awt.Color.red;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.plaf.basic.BasicComboBoxUI;



/**
 *
 * @author Queue
 */
public class JCheckbox extends JFrame {
    
    private JCheckBox c1;
    private JCheckBox c2;
    private JCheckBox c3;
    
    Canvas c;
    
    public JCheckbox()
    {
      super("hellow");
        c=new Canvas();
        c.setSize(30,30);
         c.setBackground(Color.black);
        
        add(c);
        setLayout(new FlowLayout());
        
        setSize(300,300);
        setVisible(true);
        
        c1=new JCheckBox("blue");
        c2=new JCheckBox("red");
        c3=new JCheckBox("green");
        
        add(c1);
        add(c2);
        add(c3);
        
        Handler h=new Handler();
        
        c1.addItemListener(h);
        c2.addItemListener(h);
        c3.addItemListener(h);
       
    }
   
    
private class Handler extends Canvas implements ItemListener
{

    Color color;
        @Override
        public void itemStateChanged(ItemEvent e) {
            
            
            if(c1.isSelected() && c2.isSelected())
            {
                color=Color.MAGENTA;
                update(color);
            }
            else if(c1.isSelected())
            {
                color=Color.BLUE;
                update(color);
            }
            else if(c3.isSelected())
            {
                color=Color.GREEN;
                update(color);
            }
            else if(c2.isSelected())
            {
                color=Color.red;
                update(color);
            }
            
        }
        
        public void update(Color ccc)
        {
                c.setBackground(ccc);
        }
    
}

         public static void main(String args[])
        {
              JCheckbox j=new JCheckbox();
     }



}

Output:






Happy Coding
  

Thanks ;)




Thursday, 5 January 2017

Practical 7:Develop a Programe that has only one button in the frame,clicking on the button cycle through the color RED>GREEN>BLUE ans so on.One color change per Click.(Use getBackGround() method to get the current color)

Code:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


/**
 *
 * @author Queue
 */

public class SimpleAwt extends Frame {

    public static void main(String args[]) {

        JFrame frame = new JFrame();
        JButton button = new JButton("click");
        Container c = frame.getContentPane();
        c.setBackground(Color.WHITE);
        FlowLayout ff = new FlowLayout();
        c.setLayout(ff);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeColor(c);
            }
        });

        frame.setSize(300, 300);
        frame.setVisible(true);

    }

    public static void changeColor(Container c) {
        Color cc = c.getBackground();

        if (cc.equals(Color.WHITE)) {
            c.setBackground(Color.RED);
        } else if (cc.equals(Color.RED)) {
            c.setBackground(Color.GREEN);
        } else if (cc.equals(Color.GREEN)) {
            c.setBackground(Color.BLUE);
        } else if (cc.equals(Color.BLUE)) {
            c.setBackground(Color.WHITE);
        }

    }






Output





Thanks 

Happy Coding ;)

practical 6: Develop an applet that uses the mouse listener,which overrides only two method which are mousePresses and mouseReleased.

Code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Queue
 */
public class mm extends Applet implements MouseListener{
    
    String msg="";
    public void init()
    {
        setSize(400,400);
        addMouseListener(this);
    }
    public void paint(Graphics s)
    {
        showStatus(msg);
        
        
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        msg="Mouse Pressed";
        repaint();
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
      msg="Mouse Released";
      repaint();
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
    

}


Output:



Thanks 
Happy Coding ;)


Practical 5: Develop an applet that contains one button initialize the label on the button to "Start",When the user presses the button,Which changes the label between these two values each time the button is presses

Code:


import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ClickEvent extends Applet implements ActionListener {

    Button b;
    public void init()
    {
        b=new Button();
        b.setLabel("Start");
        b.addActionListener(this);
        add(b);
        
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
     
      if(b.getLabel()== "Start")
        {
          b.setLabel("Stop");
      }
      else
      {
       b.setLabel("Start");
          
      }
        
        
    }
    

}

Output:

Thanks
Happy Coding ;)


Practical 4:Develop an applet that display the position of the mouse at the upper left corder of the appletwhen it is drag or move.Draw a 10*10 pixel rectangle filed with black at the current mouse position.

Code:

import java.applet.Applet;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


public class Drag extends Applet implements MouseListener,MouseMotionListener  {

String msg="";

int x,y;
public void init()
{
super.init();
setSize(400,400);
addMouseListener(this);
addMouseMotionListener(this);
        }

  public void paint(Graphics s)
{

showStatus(msg);
s.fillRect(x, y, 10, 10);
s.drawString("x: "+x+" Y:"+y, 20,20);
}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
            y=e.getY();
msg="X="+x+"Y="+y;
repaint();

}

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

x=e.getX();
    y=e.getY();

repaint();
msg="X="+x+"Y="+y;



}

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}


}


Output:

Thanks 
Happy Coding ;)

Practical 3:Built an applet that display a horizontal rectangle in it's center.Let the rectangle fill with color from left to right.

Code:


import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;


/**
 *
 * @author Queue
 */
public class practical3 extends Applet  {

    public void init()
    {
        super.init();
        setSize(350,350);
    }
    public void paint(Graphics s)
    {
        int x1=100,y1=100,y2=50;
        s.setColor(Color.green);
        s.drawRect(100, 100, 100, 50);
      
        for(x1=100;x1<300;x1=x1+5)
        {
            try
            {
                Thread.sleep(1000);
                s.fillRect(x1, y1, 5, y2);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    } 

}


Output:

Thanks

Happy Coding ;)

Practical 2:Draw ten red circle in a vertical column in the center of the applet


Code:


import java.applet.Applet;

import java.awt.Color;
import java.awt.Graphics;



/**

 *
 * @author Queue
 */

/*

<applet code="practical2" width="500" height="400">
</applet>
*/
public class Practical2 extends Applet{
    public void init()
    {
        super.init();
        setSize(350,350);
    }
    public void paint(Graphics s)
    {
        for(int i=0;i<10;i++)
        {
       s.setColor(Color.red);
       s.fillOval(100, i*50, 50, 50);
    }
        
    }
    

}



Output:



Thanks 

Happy Coding ;)





Practical 1:Develop an applet that draws a circle.The dimension of the applet should be 500 * 300 pixels the circle Should be centered the applet and have a radius of 100 pixels.Display your name centered in a circle(using drawOval method)


Code:


import java.applet.Applet;
import java.awt.Graphics;


/**
 *
 * @author Queue
 */


public class Practical1 extends Applet {

    
    public void init()
    {
        super.init();
        setSize(350,350);
    }
    
    public void paint(Graphics s)
    {
     s.drawOval(50, 50, 200, 200);
     s.drawString("Sherlock", 140, 150);
    }

}

Output:


Thanks ;)

Happy Coding