Tuesday, 13 September 2011

Login form in java

Here is the code of NextPage.java

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

class NextPage extends JFrame
{
NextPage()
  {
  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  setTitle("Welcome");
       setSize(400, 200);
   
}
 }
Here is the code of LoginDemo.java

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

class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
Login()
{
   label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);


label2 = new JLabel();
label2.setText("Password:");
   text2 = new JPasswordField(15);
   //this.setLayout(new BorderLayout());

SUBMIT=new JButton("SUBMIT");

        panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
   add(panel,BorderLayout.CENTER);
        SUBMIT.addActionListener(this);
        setTitle("LOGIN FORM");
}
   public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
        if (value1.equals("roseindia") && value2.equals("roseindia")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
        page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
 class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
output:






Bar Chart in Java


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

  public class SimpleBarChart extends JPanel {
  private double[] value;
  private String[] languages;
  private String title;

  public SimpleBarChart(double[] val, String[] lang, String t) {
  languages = lang;
  value = val;
  title = t;
  }
  public void paintComponent(Graphics graphics) {
  super.paintComponent(graphics);
  if (value == null || value.length == 0)
  return;
  double minValue = 0;
  double maxValue = 0;
  for (int i = 0; i < value.length; i++) {
  if (minValue > value[i])
  minValue = value[i];
  if (maxValue < value[i])
  maxValue = value[i];
  }
  Dimension dim = getSize();
  int clientWidth = dim.width;
  int clientHeight = dim.height;
  int barWidth = clientWidth / value.length;
  Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
  FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
  Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
  FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
  int titleWidth = titleFontMetrics.stringWidth(title);
  int q = titleFontMetrics.getAscent();
  int p = (clientWidth - titleWidth) / 2;
  graphics.setFont(titleFont);
  graphics.drawString(title, p, q);
  int top = titleFontMetrics.getHeight();
  int bottom = labelFontMetrics.getHeight();
  if (maxValue == minValue)
  return;
  double scale = (clientHeight - top - bottom) / (maxValue - minValue);
  q = clientHeight - labelFontMetrics.getDescent();
  graphics.setFont(labelFont);
  for (int j = 0; j < value.length; j++) {
  int valueP = j * barWidth + 1;
  int valueQ = top;
  int height = (int) (value[j] * scale);
  if (value[j] >= 0)
  valueQ += (int) ((maxValue - value[j]) * scale);
  else {
  valueQ += (int) (maxValue * scale);
  height = -height;
  }
  graphics.setColor(Color.blue);
  graphics.fillRect(valueP, valueQ, barWidth - 2, height);
  graphics.setColor(Color.black);
  graphics.drawRect(valueP, valueQ, barWidth - 2, height);
  int labelWidth = labelFontMetrics.stringWidth(languages[j]);
  p = j * barWidth + (barWidth - labelWidth) / 2;
  graphics.drawString(languages[j], p, q);
  }
  }
 public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setSize(350, 300);
  double[] value= new double[5];
  String[] languages = new String[5];
  value[0] = 1;
  languages[0] = "Visual Basic";

  value[1] = 2;
  languages[1] = "PHP";

  value[2] = 3;
  languages[2] = "C++";

  value[3] = 4;
  languages[3] = "C";

  value[4] = 5;
  languages[4] = "Java";
  frame.getContentPane().add(new SimpleBarChart(value, languages,
    "Programming Languages"));

  WindowListener winListener = new WindowAdapter() {
  public void windowClosing(WindowEvent event) {
  System.exit(0);
  }
  };
  frame.addWindowListener(winListener);
  frame.setVisible(true);
  }
}
output:

Chess Application In Java Swing


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

public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public ChessGameDemo(){
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for this this application
         layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);
        layeredPane.addMouseListener(this);
        layeredPane.addMouseMotionListener(this);

        //Add a chess board to the Layered Pane

        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
            else
                square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
        }

        //Add a few pieces to the board

        JLabel piece = new JLabel( new ImageIcon("/home/vinod/amarexamples/chess.jpg") );
        JPanel panel = (JPanel)chessBoard.getComponent(0);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg"));
        panel = (JPanel)chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/king.jpg"));
        panel = (JPanel)chessBoard.getComponent(16);
        panel.add(piece);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/camel.jpg"));
        panel = (JPanel)chessBoard.getComponent(20);
        panel.add(piece);

    }

    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel)
  return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }
 
    //Move the chess piece around
   
    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
     }
   
  //Drop the chess piece back onto the chess board

    public void mouseReleased(MouseEvent e) {
        if(chessPiece == null) return;

        chessPiece.setVisible(false);
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JLabel){
            Container parent = c.getParent();
            parent.remove(0);
            parent.add( chessPiece );
        }
        else {
            Container parent = (Container)c;
            parent.add( chessPiece );
        }

        chessPiece.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
 
    }
    public void mouseMoved(MouseEvent e) {
   }
    public void mouseEntered(MouseEvent e){
 
    }
    public void mouseExited(MouseEvent e) {
 
    }

    public static void main(String[] args) {
        JFrame frame = new ChessGameDemo();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}
 output:

Crate a Popup Menu in Java




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


public class PopUpMenu{
JPopupMenu Pmenu;
JMenuItem menuItem;
public static void main(String[] args) {
PopUpMenu p = new PopUpMenu();
}


public PopUpMenu(){
JFrame frame = new JFrame("Creating a Popup Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pmenu = new JPopupMenu();
menuItem = new JMenuItem("Cut");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Copy");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Paste");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Delete");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Undo");
Pmenu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){}
});
frame.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
if(me.isPopupTrigger()){
Pmenu.show(me.getComponent(), me.getX(), me.getY());
}
}
public void mouseReleased(MouseEvent Me){
if(Me.isPopupTrigger()){
Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
}
}
});
frame.setSize(400,400);
frame.setVisible(true);
}
}
output:


Create a ToolBar in Java


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

public class CreateToolbar{
public static void main(String[] args) {
JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste");
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
toolbar.add(cutbutton);
JButton copybutton = new JButton(new ImageIcon("copy.gif"));
toolbar.add(copybutton);
JButton pastebutton = new JButton(new ImageIcon("paste.gif"));
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
}
}