Thursday, October 21, 2010

CHESSBOARD INTERFACE


Introduction
This is a description of a progam that displays a chess board and its pieces in 2 dimension.
Interface
To display the interface, the source code imported some classes from the SWING package and the AWT package, of the java API (Application Programming Interface). The two packages provided the required classes and methods for the achievement of the program’s intended purpose.

Ø  Chessboard 
Figure 1: Chessboard

The graphics context object above was created from a class called Graphics2D. The code that displayed it was placed in the paint method. The paint method received the graphics context for the component as a parameter. The paint method of this code was called by Swing whenever a component needs to be repainted for any reason.
After creating the chessboard, pieces were placed on it as shown in figure 2.

Ø  Chessboard and pieces
Figure 2: chessboard and pieces

A folder containing image icons of the chesspieces was stored in a disk c. Thus, image icon constructor was used to insert the pieces on the chessboard by using the location of the image icon folder as a parameter in the image icon constructor. 

CHESSBOARD SOURCE CODE

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

public class JazzaChess extends JFrame {

    private JPanel mainPanel;
    private DrawingPanel drawPanel;
     static SamChess myChessBoard;
     private int x = 100, y = 60;

    public JazzaChess() {
   
           super("CHESS BOARD AND CHESS PIECES");
   
        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.setBackground(Color.RED);

        drawPanel= new DrawingPanel();
        drawPanel.setBounds(mainPanel.getWidth() / 2, mainPanel.getHeight() / 2, 1000, 1000);
        drawPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        mainPanel.add(drawPanel, BorderLayout.CENTER);
         setContentPane(mainPanel);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000,700);
        setLocationRelativeTo(null);
        setVisible(true);

      

    }

    public static void main(String[] args) {
         myChessBoard = new  SamChess();
           }


     class DrawingPanel extends JPanel {
        private int x = 100, y = 60;   

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            drawingMethod(g2d);
        }

       private void drawingMethod(Graphics2D g2d) {
          
            for (int i = 0; i <= 8; ++i) {
                g2d.drawLine(x, (y * i) + 60, x + 800, (y * i) + 60);
                // g2d.drawLine((x * j) + 100, y, (x * j) + 100, y + 480);
                }
           
         for (int j = 0; j <= 8; ++j) {
                    g2d.drawLine((x * j) + 100, y, (x * j) + 100, y + 480);
               }
      
          g2d.setColor(Color.RED);
          g2d.fillRect(200, 60, 100, 60);         
       
          ImageIcon myImage3 = new ImageIcon("c:\\Chesspieces\\chess2.png");
          Image picImage3 = myImage3.getImage();
          g2d.drawImage(picImage3,130,65, this);
         
          ImageIcon myImage = new ImageIcon("c:\\Chesspieces\\chess1.png");
          Image picImage = myImage.getImage();
          g2d.drawImage(picImage, 230,65, this);
         
          ImageIcon myImage6 = new ImageIcon("c:\\Chesspieces\\chess3.png");
          Image picImage6 = myImage6.getImage();
          g2d.drawImage(picImage6, 330,65, this);
         
         g2d.fillRect(400, 60, 100, 60);
          ImageIcon myImage8 = new ImageIcon("c:\\Chesspieces\\chess5.png");
          Image picImage8 = myImage8.getImage();
          g2d.drawImage(picImage8, 430,61, this);         
                  
         
          ImageIcon myImage9 = new ImageIcon("c:\\Chesspieces\\chess2.png");
          Image picImage9 = myImage9.getImage();
         
          g2d.fillRect(100, 120, 100, 60);
          g2d.fillRect(300, 120, 100, 60);
          g2d.fillRect(500, 120, 100, 60);
         g2d.fillRect(700, 120, 100, 60);

          g2d.drawImage(picImage9, 130,125, this);         
          g2d.drawImage(picImage9, 230,125, this);         
          g2d.drawImage(picImage9, 330,125, this);         
          g2d.drawImage(picImage9, 430,125, this);         
          g2d.drawImage(picImage9, 530,125, this);       
          g2d.drawImage(picImage9, 630,125, this);         
          g2d.drawImage(picImage9, 730,125, this);
          g2d.drawImage(picImage9, 830,125, this);           

          g2d.fillRect(200, 180, 100, 60);
          g2d.fillRect(400, 180, 100, 60);
          g2d.fillRect(600, 180, 100, 60);
          g2d.fillRect(800, 180, 100, 60);           
       
          g2d.fillRect(100, 240, 100, 60);
          g2d.fillRect(300, 240, 100, 60);
          g2d.fillRect(500, 240, 100, 60);
          g2d.fillRect(700, 240, 100, 60);
           
          g2d.fillRect(200, 300, 100, 60);
          g2d.fillRect(400, 300, 100, 60);
          g2d.fillRect(600, 300, 100, 60);
          g2d.fillRect(800, 300, 100, 60);
           
          g2d.fillRect(100, 360, 100, 60);
          g2d.fillRect(300, 360, 100, 60);
          g2d.fillRect(500, 360, 100, 60);
          g2d.fillRect(700, 360, 100, 60);

         g2d.fillRect(800, 60, 100, 60);
           
          ImageIcon myImage4 = new ImageIcon("c:\\Chesspieces\\chess2.png");
          Image picImage4 = myImage4.getImage();
          g2d.drawImage(picImage4, 830,65, this);
         
          ImageIcon myImage2 = new ImageIcon("c:\\Chesspieces\\chess1.png");
          Image picImage2 = myImage2.getImage();
          g2d.drawImage(picImage2,730,65, this);
         
          g2d.fillRect(600, 60, 100, 60);
          ImageIcon myImage5 = new ImageIcon("c:\\Chesspieces\\chess3.png");
          Image picImage5 = myImage5.getImage();
          g2d.drawImage(picImage5, 630,65, this);
         
          ImageIcon myImage7 = new ImageIcon("c:\\Chesspieces\\chess4.png");
          Image picImage7 = myImage7.getImage();
          g2d.drawImage(picImage7, 530,61, this);


                   
          ImageIcon myImage17 = new ImageIcon("c:\\Chesspieces\\chess6.png");
          Image picImage17 = myImage17.getImage();
          g2d.drawImage(picImage17,130,428, this);
         
          g2d.fillRect(200, 420, 100, 60);
          g2d.fillRect(400, 420, 100, 60);
          g2d.fillRect(600, 420, 100, 60);
          g2d.fillRect(800, 420, 100, 60);
          g2d.fillRect(100, 480, 100, 60);
             
          g2d.drawImage(picImage17,230,428, this);
          g2d.drawImage(picImage17,330,428, this);
          g2d.drawImage(picImage17,430,428, this);       
          g2d.drawImage(picImage17,530,428, this);
          g2d.drawImage(picImage17,630,428, this);
          g2d.drawImage(picImage17,730,428, this);
          g2d.drawImage(picImage17,830,428, this);
          g2d.drawImage(picImage17,130,486, this);
         
       
          ImageIcon myImage28 = new ImageIcon("c:\\Chesspieces\\chess7.png");
          Image picImage28 = myImage28.getImage();
          g2d.drawImage(picImage28,230,486, this);
         
          g2d.fillRect(300, 480, 100, 60);
          ImageIcon myImage29 = new ImageIcon("c:\\Chesspieces\\chess8.png");
          Image picImage29 = myImage29.getImage();
          g2d.drawImage(picImage29,330,486, this);
         
          ImageIcon myImage25 = new ImageIcon("c:\\Chesspieces\\chess6.png");
          Image picImage25 = myImage25.getImage();
          g2d.drawImage(picImage25,830,486, this);
       
          g2d.fillRect(700, 480, 100, 60);
          ImageIcon myImage27 = new ImageIcon("c:\\Chesspieces\\chess7.png");
          Image picImage27 = myImage27.getImage();
          g2d.drawImage(picImage27,730,486, this);
         
          ImageIcon myImage30 = new ImageIcon("c:\\Chesspieces\\chess8.png");
          Image picImage30 = myImage30.getImage();
          g2d.drawImage(picImage30,630,486, this);
         
          g2d.fillRect(500, 480, 100, 60);
          ImageIcon myImage31 = new ImageIcon("c:\\Chesspieces\\chess9.png");
          Image picImage31 = myImage31.getImage();
          g2d.drawImage(picImage31,530,480, this);
         
          ImageIcon myImage32 = new ImageIcon("c:\\Chesspieces\\chess10.png");
          Image picImage32 = myImage32.getImage();
          g2d.drawImage(picImage32,430,480, this);
             
             }
         }
    }

Friday, October 8, 2010

JAVA SOURCE CODE FOR PATIENT REGISTRATION INTERFACE

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

    public class Patient extends JFrame implements ActionListener
  
   {
       public static void main(String [] args)
      {
       
         new Patient();
      }
  
      JTextField pName, doBirth, hAddress,hospital,diagnosis;
      JComboBox sex;
      JRadioButton medical, dentistry, optical,oPatient,admitted;
      JCheckBox died, discharged;
      JButton Apply, clearButton, exitButton;
      String[] theBox= {"male","female"};
       public Patient()
     
      {
         this.setTitle("PATIENT REGISTRATION FORM");
         this.setDefaultCloseOperation(
            JFrame.EXIT_ON_CLOSE);
     
         JPanel panel1 = new JPanel();
         panel1.setLayout(new GridBagLayout());         
         addItem(panel1, new JLabel("FULL NAME OF PATIENT:"), 
            0, 0, 1, 1, GridBagConstraints.EAST);
         addItem(panel1, new JLabel("DATE OF BIRTH:"),
            0, 1, 1, 1, GridBagConstraints.EAST);
         addItem(panel1, new JLabel("HOME ADDRESS ( Vge / TA / District):"),
            0, 2, 1, 1, GridBagConstraints.EAST);
         addItem(panel1, new JLabel("SEX:"),
            0, 3, 1, 1, GridBagConstraints.EAST);
         addItem(panel1, new JLabel("HOSPITAL:"),
           
            0, 4, 1, 1, GridBagConstraints.EAST);
         addItem(panel1, new JLabel("DIAGNOSIS:"),
            0, 5, 1, 1, GridBagConstraints.EAST);
     
     
         pName = new JTextField(20);
         doBirth = new JTextField(20);
         hAddress = new JTextField(20);
     
         sex = new JComboBox(theBox);
         hospital = new JTextField(20);
         diagnosis= new JTextField(20);
     
     
         addItem(panel1, pName, 1, 0, 2, 1,                
            GridBagConstraints.WEST);
         addItem(panel1, doBirth, 1, 1, 1, 1,
            GridBagConstraints.WEST);
         addItem(panel1, hAddress, 1, 2, 2, 1,
            GridBagConstraints.WEST);
         
         addItem(panel1, sex, 1, 3, 2, 1,
            GridBagConstraints.WEST);
         addItem(panel1,hospital, 1,4,2,1,
            GridBagConstraints.WEST);
     
         addItem(panel1, diagnosis, 1,5,2,1,
            GridBagConstraints.WEST);
     
     
         Box sizeBox = Box.createVerticalBox();          
         medical = new JRadioButton("medical");
         dentistry = new JRadioButton("dentistry");
         optical = new JRadioButton("optical");
         
         ButtonGroup sizeGroup = new ButtonGroup();
         sizeGroup.add(medical);
         sizeGroup.add(dentistry);
         sizeGroup.add(optical);
         sizeBox.add(medical);
         sizeBox.add(dentistry);
         sizeBox.add(optical);
         
         sizeBox.setBorder(
            BorderFactory.createTitledBorder("TREATMENT"));
         addItem(panel1, sizeBox, 0,6, 1, 1,
            GridBagConstraints.NORTH);
     
         Box styleBox = Box.createVerticalBox();          
         oPatient = new JRadioButton("Out Patient");
         admitted = new JRadioButton("Admitted");
         ButtonGroup styleGroup = new ButtonGroup();
       
         styleGroup.add(oPatient);
         styleGroup.add(admitted);
         styleBox.add(oPatient);
         styleBox.add(admitted);
         styleBox.setBorder(BorderFactory.
           
            createTitledBorder("RHESUS FACTOR"));
         addItem(panel1, styleBox, 1,6, 1, 1,
            GridBagConstraints.NORTH);
     
         Box topBox = Box.createVerticalBox();            
         discharged = new JCheckBox("yes");
         died = new JCheckBox("no");
      
         ButtonGroup topGroup = new ButtonGroup();
         topGroup.add(discharged);
         topGroup.add(died);
       
         topBox.add(discharged);
         topBox.add(died);
         topBox.setBorder(BorderFactory.
           
            createTitledBorder("APPOINTMENT"));
         addItem(panel1, topBox, 2, 6, 1, 1,
            GridBagConstraints.NORTH);
     
         Box buttonBox = Box.createHorizontalBox();       
         Apply = new JButton("Apply");
         clearButton = new JButton("Clear");
         exitButton = new JButton("Exit");
         
         buttonBox.add(Apply); 
         buttonBox.add(Box.createHorizontalStrut(25));
         buttonBox.add(clearButton);
         clearButton.addActionListener(this);
         buttonBox.add(Box.createHorizontalStrut(25));
         buttonBox.add(exitButton);
         exitButton.addActionListener(this);
         addItem(panel1, buttonBox, 0, 7, 3, 1, GridBagConstraints.CENTER);
     
         pName.setText("  ");
         doBirth.setText("  ");
         hAddress.setText(" ");
         hospital.setText("  ");
         diagnosis.setText("  ");
     
         oPatient.setText("positive        ");
         oPatient.setSelected(true);
     
         admitted.setText("negative       ");
         admitted.setSelected(true);
     
         medical.setText("medical");
         medical.setSelected(true);
     
         dentistry.setText("dentistry");
         dentistry.setSelected(true);
     
         optical.setText("optical");
         optical.setSelected(true);
           
         discharged.setText("yes                 ");
         discharged.setSelected(true);
     
         died.setText("no             ");
         died.setSelected(true);
     
         Apply.setText("Apply");
         Apply.addActionListener(
                new ActionListener(){
                   public void actionPerformed(ActionEvent e){
                     JTextArea textarea=new JTextArea();
                     JScrollPane scroll=new JScrollPane( textarea,
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,  
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                     
                     JFrame frame= new JFrame();
                 
                     if (e.getSource()==Apply){
                        String name= pName.getText();
                        textarea.append("\n\n");
                        textarea.append("    FULL NAME OF PATIENT: "+name+"\n\n");
                    
                        String name1= doBirth.getText();
                        textarea.append("    DATE OF BIRTH: "+name1+"\n\n");
                    
                        String name3 =  hAddress.getText();
                        textarea.append("    HOME ADDRESS (Vge / TA / District): "+ name3+"\n\n");
                        String enteredText = (String)sex.getSelectedItem();
                        textarea.append("    SEX: "+enteredText+"\n\n");
                    
                        String name4=hospital.getText();
                        textarea.append("    HOSPITAL: "+name4+"\n\n");
                                                           
                        String name5=diagnosis.getText();
                        textarea.append("    DIAGNOSIS: "+name5+"\n\n");
                    
                        if (oPatient.isSelected())
                           textarea.append("    RHESUS FACTOR:  positive\n\n");
                        if (admitted.isSelected())
                           textarea.append("    RHESUS FACTOR:  negative\n\n");
                    
                        if (medical.isSelected())
                           textarea.append("    TREATMENT:  Medical\n\n");
                        if (dentistry.isSelected())
                           textarea.append("    TREATMENT:  Dentistry\n\n");   
                        if (optical.isSelected())
                           textarea.append("    TREATMENT:  Optical\n\n");
                    
                        if (discharged.isSelected())
                           textarea.append("    APPOINTMENT:  Yes\n\n");
                        if (died.isSelected())
                           textarea.append("    APPOINTMENT:  No\n\n");
                                                            
                        textarea.setEditable(false);
                     }   
                     frame.add(scroll);
                     frame.setTitle("    DETAILS OF REGISTERED PATIENT");
                     frame.setSize(500,500);
                     frame.setLocation(350,50);
                     frame.setVisible(true);
                 
                  }
               });
     
         this.add(panel1);
         this.pack();
         this.setVisible(true);
      }
  
       public void actionPerformed(ActionEvent evnt){
         if(evnt.getSource()==exitButton){
            System.exit(0);
         }
         else if (evnt.getSource()==clearButton){
            cancel1();
         }       
      }
  
       public void cancel1() {
         pName.setText("");
     
      }
      
       private void addItem(JPanel p, JComponent c,
        int x, int y, int width, int height,
        int align)
      {
         GridBagConstraints gc =
            new GridBagConstraints();
         gc.gridx = x;
         gc.gridy = y;
         gc.gridwidth = width;
         gc.gridheight = height;
         gc.weightx = 100.0;
         gc.weighty = 100.0;
         gc.insets = new Insets(20, 20, 20, 20);
         gc.anchor = align;
         gc.fill = GridBagConstraints.NONE;
         p.add(c, gc);
      }
   }

Thursday, October 7, 2010

PATIENT REGISTRATION INTERFACE


INTRODUCTION
The objects from the standard java.awt packages are called Graphical User Interface (GUI) objects, and they are used to handle the user interface aspect of programs. The style of programming used with the GUI objects is called event driven programming. An event occurs when a user interacts with a GUI object. For example, when a button is clicked, an event occurs. In event driven programs, objects are programmed to respond to the events by defining event handling methods. In the development of patient registration system interface
 as shown in the four figures below, the following packages were used; javax.swing, java.awt, and java.awt.event.

INTERFACE
The figure 1 below is a patient registration form which is used to register a patient. The user of the registration form is a data  entry clerk at a hospital.

Figure 1: A sample of non filled form  



The development of the Java source code which displays the figure 1 has exposed me in discovering much about JButtons, JRadioButtons, JLabels, JTextFields, JComboBoxe, layout managers; especially GridBagLayout and handling event methods.


LABEL
It is a displayed line of text on an interface. For example, the label; Name of patient in the diagram 1 above is a label. Labels are used for a variety of purposes: to display captions for other controls such as text fields or combo boxes, to display informational messages, or to show the results of a calculation or a database lookup. Name of patient was constructed by using a JLabel constructor, specifying the coordinates of the label as (1,1), setting the GridBagConstraints to the EAST of the mainPanel> JLabel constructors  and methods were used. Similar procedures were done to all the remaining labels (date of birth, home address, sex, hospital, diagnosis, treatment, Rhesus factor and appointment) but each label had a different cordinate.

TEXT FIELDS
A text field is a box that the user can type text in. The input boxes of the labels, for instance, text field for the label; name of patient was constructed by using JTextField constructor  and methods. Length and cordinates of the field were specified. The GridBagConstraints were anchored to the WEST of the mainPanel and the text field was added to the mainPanel by using add method. Similar procedures were applied to the other text fields of the remaining labels but the difference was on the cordinates of each text field.

Figure 2 showing combo box with list o values


COMBO BOX
A combo box is a combination of a text field and a drop-down list from which the user can choose a value. The combo box with a label sex has male and female as values from which user can select value. The user must pick one of the values from the list as shown in figure 3. The JComboBox class was used to create combo boxes. Constructors and methods of this class were used.

Figure 3: A sample of filled form


After entering the details of patient were entered in the text fields and clicking the appropriate buttons.

RADIO BUTTONS
Buttons under Treatment and Rhesus Factor
Radio buttons travel in groups, and a user can select only one radio button in each group at a time. When a radio button is clicked to select it, whatever radio button was previously selected is automatically deselected. Compare radio buttons under treatment in figures 1 and 2.
To come up with radio buttons, two classes were used. Radio buttons were created with the JRadioButton class, by constructors and methods. Then a group for the buttons was created with the ButtonGroup class. After that, the radio buttons were added to a panel and to a button group.

The position of the radio buttons were set by using cordinates and the GridBagConstraints to the EAST of the main panel.

APPOINTMENT CHECKBOX
A check box is a control that the user can click to either check or uncheck. Check box in the interface above was used to let the user specify Yes or No to an appointment of a patient with a doctor.

JBUTTONS
The three JButtons namely save, clear and exit button were created by using JButton constructor. The buttons had the different x-values but the same y-values (cordinates) for positioning the buttons and GridBagLayout constraints were set to the west of the mainPanel.

  • CLEAR BUTTON
This button is used to make corrections when data is entered incorrectly. It was constructed by JButton constructor. When this button is clicked, an event is sent to a listener that is registered with the button as an ActionListener. In particular, evt.getActionCommand() returns a string, giving the command associated with the button.

  • EXIT BUTTON
When this button is clicked, the patient registration interface disappears. It was constructed by JButton constructor. When this button is clicked, an event is sent to a listener that is registered with the button as an ActionListener. In particular, evt.getActionCommand() returns a string, giving the command associated with the button.

  • APPLY BUTTON
This button is clicked whenever the details that have been typed are to be kept in a database. Figure 3 below shows the results after clicking the save button in the diagram 2 above. The save button was constructed by JButton constructor. When the user clicked on the save button, the button generated an event of type ActionEvent. ActionListener has one method, “public void actionPerformed(ActionEvent evt)”, which was called to notify the object of an action event. When actionPerformed(evt) is called by the button, the parameter, evt, contains information about the event. In particular, evt.getActionCommand() returns a String giving the command associated with the save button.
Finally details of the patient are stored in a database known as details of registered patient as shown in figure 4 below.

Figure 4: Sample of saved details of a patient





Reference

Bailey, D.A., & Bailey, D.W. (2000). Java elements: Principles of programming in java.
             New York: McGraw Hill-Companies, Inc.

Java All-In One Desk Reference for Dummies (2nd Ed.), John Willey & Sons

Thomas, C. (2001). An introduction to object oriented programming with Java (2nd ed.). 
New York: Thomas Casson.