You are on page 1of 17

Programme Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
class OnlinePaper extends MouseAdapter implements
ActionListener
{
JFrame f;
JTextField tf1,tf2;
JTextArea ta1;
JLabel
l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l21,l22,l23,l24;
JCheckBox cb1,cb2,cb3,cb4,cb5,cb6;
JRadioButton rb1,rb2,rb3,rb4;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b21,b22;
ButtonGroup bg1,bg2;
JPanel p1,p2,p5,p6,p7,jp,p11,p12;
Box bx1,bx3,bx4,bx5;
JPasswordField pf;
Font font;
Connection con;
Statement stmt;
ResultSet rs;
String ch,q1,ds1,ds2,ds3,us1,us2,ps1,ps2;
int
i=1,j=0,k=0,arr[]={0,1,2,3,4,5,6,7,8,9,10},a1,b11,c1,d1,marks=0,fl=1,i
1=1;
boolean a,b,c,d,flag=true,flag1=false;
Color clr;

Date dt;
Thread t;
JDialog d2;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public OnlinePaper()
{
loginDialog();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con =
DriverManager.getConnection("jdbc:odbc:paper");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("SELECT * FROM Question");
while(rs.next())
{
rs.updateInt("ua",0);
rs.updateInt("ub",0);
rs.updateInt("uc",0);
rs.updateInt("ud",0);
rs.updateRow();
}
rs.first();
}
catch(Exception e1)
{
}
}
//------------------------------------------------------------------------------private void loginDialog()
{
d2=new JDialog(new JFrame(),"Authentication",false);

d2.setSize(250,150);d2.setLocation (screenSize.width / 2 d2.getWidth() / 2, screenSize.height / 2 - d2.getHeight() / 2);


d2.getContentPane().setLayout( new FlowLayout());
d2.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE
);
l21=new JLabel("User Name ");
tf1=new JTextField(10);
p11=new JPanel();
p11.add(l21); p11.add(tf1);
d2.getContentPane().add(p11);
l22=new JLabel("Passward ");
pf=new JPasswordField(10);
p12=new JPanel();
p12.add(l22); p12.add(pf);
d2.getContentPane().add(p12);
b21=new JButton("OK");
d2.getContentPane().add(b21);
b21.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent
a)
{
String uname=tf1.getText();
String psw=String.valueOf(pf.getPassword());
boolean flag=false;
Statement stmt1=null;
ResultSet rs1=null;
try

stmt1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
E,ResultSet.CONCUR_UPDATABLE);
rs1 = stmt1.executeQuery("SELECT uname,psw FROM user
WHERE uname='"+uname+"' AND psw='"+psw+"'");
while(rs1.next())
{
flag=true;}}
catch(Exception e){}
if(flag)
{
d2.setVisible(false);
d2.dispose();
startExam();
f.setVisible(true);
}
else
{
tf1.setText("");
pf.setText("");
JOptionPane.showMessageDialog(null,"User Name or Password not
match");}}});
b22=new JButton("Cancel");
d2.getContentPane().add(b22);b22.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent a)
{
System.exit(1);
}
});
d2.setVisible(true);
}//-----------------------------------------------------------------------------private void startExam(){
f=new JFrame("Online Exam");

f.setLocation (screenSize.width / 2 - f.getWidth() / 2,


screenSize.height / 2 - f.getHeight() / 2);
f.setSize(775,400);
f.setResizable(false);
f.setBackground(Color.yellow);
f.setBounds(10,100,775,400);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
p1=new JPanel();
p1.setLayout(new BorderLayout());
clr=new Color(204,204,204);
//f.setBackground(clr);
p6=new JPanel();
p6.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
l5=new JLabel("Q");
l6=new JLabel();
p7=new JPanel();
ta1=new JTextArea(6,45);
ta1.setBackground(Color.yellow);
ta1.setEditable(false);
ta1.setLineWrap(true);
ta1.setFont(new Font("Arial",Font.PLAIN,12));
p6.add(l5);

p6.add(l6);

p7.add(ta1);

p1.add(p6,"West"); p1.add(p7);
bx1= Box.createVerticalBox();
l1=new JLabel("A.");
l3=new JLabel("C.");
l4=new JLabel("D.");

l2=new JLabel("B.");

bx1.add(l1);
bx1.add(Box.createVerticalStrut(35));
bx1.add(l2);
bx1.add(Box.createVerticalStrut(35));
bx1.add(l3);
bx1.add(Box.createVerticalStrut(35));
bx1.add(l4);
bx3= Box.createVerticalBox();
cb1=new JCheckBox(); cb2=new JCheckBox();
cb3=new JCheckBox();
cb4=new JCheckBox();
cb1.addMouseListener(this);
cb2.addMouseListener(this);
cb3.addMouseListener(this);
cb4.addMouseListener(this);
cb1.addActionListener(this);
bx3.add(Box.createVerticalStrut(33));
bx3.add(cb1);
bx3.add(Box.createVerticalStrut(30));
bx3.add(cb2);
bx3.add(Box.createVerticalStrut(30));
bx3.add(cb3);
bx3.add(Box.createVerticalStrut(30));
bx3.add(cb4);
bx3.add(Box.createVerticalStrut(30));
bx4= Box.createVerticalBox();
l8=new JLabel();
l9=new JLabel();
JLabel();
l11=new JLabel();
bx4.add(Box.createVerticalStrut(33));
bx4.add(l8);
bx4.add(Box.createVerticalStrut(35));

l10=new

bx4.add(l9);
bx4.add(Box.createVerticalStrut(35));
bx4.add(l10);
bx4.add(Box.createVerticalStrut(35));
bx4.add(l11);
bx4.add(Box.createVerticalStrut(35));
bx5= Box.createHorizontalBox();
bx4.add(Box.createHorizontalStrut(135));
bx5.add(bx1);
bx5.add(Box.createHorizontalStrut(5));
bx5.add(bx3);
bx5.add(Box.createHorizontalStrut(5));
bx5.add(bx4);
bx5.add(Box.createHorizontalStrut(5));
p2=new JPanel();
p2.setLayout(new
FlowLayout(FlowLayout.LEFT,5,5));
p2.setBackground(Color.red);
p2.add(bx5);
p5=new JPanel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT,35,5));
b1=new JButton("Start");
b2=new
JButton("Previous");
b3=new JButton("Next");
b4=new
JButton("Finish");
b5=new JButton("Preview"); b7=new JButton("Exit");
l12=new JLabel("Total marks obtain ");
l12.setVisible(false);
l13=new JLabel(); l13.setVisible(false);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b7.addActionListener(this);
p5.add(b1);
p5.add(b2);
p5.add(b3);
p5.add(b4);
p5.add(l12);p5.add(l13);
p5.add(b5);
p5.add(b7);
f.getContentPane().add(p1,"North");
f.getContentPane().add(p2,"Center");
f.getContentPane().add(p5,"South");
b5.setVisible(false);
f.setVisible(true);
l23=new JLabel(new ImageIcon("offline.gif"));
p5.add(l23,"East");
l24=new JLabel(new ImageIcon("CF.gif"));
b2.setVisible(false); b3.setVisible(false);
b4.setVisible(false);
b7.setVisible(false);
p1.setVisible(false);
bx1.setVisible(false); bx3.setVisible(false);
bx4.setVisible(false);
l12.setForeground(Color.blue);
while(i1<10)
{
j=(int)(Math.random()*100);
k=0;
while(k<=i1){if(arr[k]==j || j==0 || j>30)fl=0;

k++;}
if(fl==1)
{arr[i1]=j;i1++;}
fl=1;}
dt=new Date();
t=Thread.currentThread();
ds1=""+dt;
jp=new JPanel();
l14=new JLabel();
jp.add(l14);
p1.add(jp,"East");
ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
l14.setText(""+new java.util.Date());
l24.setIcon(new ImageIcon("CF.gif"));}};
new Timer(1000, taskPerformer).start();}
//==================================================
public void mouseReleased(MouseEvent me){
try{
rs.moveToInsertRow();
a=cb1.isSelected();
if(a==true){
rs.updateInt("ua",1);}
else
rs.updateInt("ua",0);
b=cb2.isSelected();
if(b==true)
rs.updateInt("ub",1);
else
rs.updateInt("ub",0);
c=cb3.isSelected();
if(c==true)
rs.updateInt("uc",1);

else
rs.updateInt("uc",0);
d=cb4.isSelected();
if(d==true)
rs.updateInt("ud",1);
else
rs.updateInt("ud",0);
rs.updateRow();
con.setAutoCommit(true);
}catch(Exception e)
{}}//==============================================
public void actionPerformed(ActionEvent ae){
String act=ae.getActionCommand();if(act.equals("Start"))
{p1.setVisible(true);
bx1.setVisible(true); bx3.setVisible(true);
bx4.setVisible(true);
l23.setVisible(false);
l23=new JLabel(new ImageIcon("online.gif"));
jp.add(l23);
p5.add(l24);
if(i<10)
b4.setVisible(false);
else
b4.setVisible(true);
try{
i=1;
rs.absolute(arr[i]);
l6.setText(""+i);
ta1.setText(rs.getString("Q"));
l8.setText(rs.getString("A"));
l9.setText(rs.getString("B"));
l10.setText(rs.getString("C"));
l11.setText(rs.getString("D"));
b1.setVisible(false);}

catch(Exception e2){}
b2.setVisible(true); b3.setVisible(true);}
//-------------------------------------------------------------------------------------if(act.equals("Next")){
try{i++;rs.first();if(i<11){if(i<10)
b4.setVisible(false);else{
b3.setEnabled(false);
b4.setVisible(true);}rs.absolute(arr[i]);a1=rs.getInt("ua");
if(a1==1)
cb1.setSelected(true);
else
cb1.setSelected(false);
b11=rs.getInt("ub");
if(b11==1)
cb2.setSelected(true);
else
cb2.setSelected(false);
c1=rs.getInt("uc");
if(c1==1)
cb3.setSelected(true);
else
cb3.setSelected(false);
d1=rs.getInt("ud");
if(d1==1)
cb4.setSelected(true);
else
cb4.setSelected(false);
l6.setText(""+i);
ta1.setText(rs.getString("Q"));
l8.setText(rs.getString("A"));
l9.setText(rs.getString("B"));
l10.setText(rs.getString("C"));
l11.setText(rs.getString("D"));
}

b2.setEnabled(true);
}
catch(Exception e3)
{
}
if(flag1)
{
try
{
if(rs.getInt("aa")==1)
l8.setForeground(Color.red);
else
l8.setForeground(Color.black);
if(rs.getInt("ab")==1)
l9.setForeground(Color.red);
else
l9.setForeground(Color.black);
if(rs.getInt("ac")==1)
l10.setForeground(Color.red);
else
l10.setForeground(Color.black);
if(rs.getInt("ad")==1)
l11.setForeground(Color.red);
else
l11.setForeground(Color.black);
}
catch(Exception e6)
{
}
}
}

//-----------------------------------------------------------------------------------------if(act.equals("Previous"))
{
try
{
i--;
rs.first();
if(i>0)
{
b3.setEnabled(true);
if(i<10)
b4.setVisible(false);
else
b4.setVisible(true);
rs.absolute(arr[i]);
//System.out.println("Row No ="+rs.getRow()
+" Arr[i] ="+arr[i]);
a1=rs.getInt("ua");
if(a1==1)
cb1.setSelected(true);
else
cb1.setSelected(false);
b11=rs.getInt("ub");
if(b11==1)
cb2.setSelected(true);
else
cb2.setSelected(false);
c1=rs.getInt("uc");
if(c1==1)
cb3.setSelected(true);
else

cb3.setSelected(false);
d1=rs.getInt("ud");
if(d1==1)
cb4.setSelected(true);
else
cb4.setSelected(false);
l6.setText(""+i);
ta1.setText(rs.getString("Q"));
l8.setText(rs.getString("A"));
l9.setText(rs.getString("B"));
l10.setText(rs.getString("C"));
l11.setText(rs.getString("D"));
}
if(i==1)
b2.setEnabled(false);
}
catch(Exception e4)
{
}
}
if(flag1)
{
try
{
if(rs.getInt("aa")==1)
l8.setForeground(Color.red);
else
l8.setForeground(Color.black);
if(rs.getInt("ab")==1)
l9.setForeground(Color.red);
else
l9.setForeground(Color.black);

if(rs.getInt("ac")==1)
l10.setForeground(Color.red);
else
l10.setForeground(Color.black);
if(rs.getInt("ad")==1)
l11.setForeground(Color.red);
else
l11.setForeground(Color.black);
}
catch(Exception e7)
{
}
}
if(i>=10)
{
b2.setEnabled(true); b3.setEnabled(false);
}
if(i==0)
{
b3.setEnabled(true); b2.setEnabled(false);
i=1;
}
//-----------------------------------------------------------------------------------------if(act.equals("Finish"))
{
b4.setEnabled(false);
b5.setVisible(true);
b7.setVisible(true);
try
{

rs.beforeFirst();
while(rs.next())
{
if(rs.getInt("ua") != rs.getInt("aa"))
flag=false;
if(rs.getInt("ub") != rs.getInt("ab"))
flag=false;
if(rs.getInt("uc") != rs.getInt("ac"))
flag=false;
if(rs.getInt("ud") != rs.getInt("ad"))
flag=false;
if(flag)
marks++;flag=true;}}catch(Exception e5){}
l13.setText(""+marks+" Marks");
if(marks<=4) l13.setForeground(Color.red);
if(4< marks && marks <=6)
l13.setForeground(Color.blue);
if(6< marks && marks <11)
l13.setForeground(Color.yellow);
l12.setVisible(true);
l13.setVisible(true);
cb1.setEnabled(false);
cb2.setEnabled(false);
cb3.setEnabled(false);
cb4.setEnabled(false);}
//-------------------------------------------------------------------------------------if(act.equals("Preview"))
{
b5.setVisible(false);
flag1=true;}
//-------------------------------------------------------------------------------------if(act.equals("Exit"))
{try{stmt.close();
con.close();}
catch(Exception e8)
{}
f.setVisible(false);

f.dispose();
System.exit(1);}}
//-------------------------------------------------------------------------------------public static void main(String arg[])
{
new OnlinePaper();}}