survey management
TRANSCRIPT
-
8/9/2019 Survey Management
1/25
import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.*;
import java.awt.image.*;import java.text.*;import javax.swing.*;import java.sql.*;import java.io.*;
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);
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){int i;
String value1=text1.getText();String value2=text2.getText();if (value1.equals("admin") && value2.equals("torres")) {
NextPage page=new NextPage();page.setVisible(true);
JLabel label = new JLabel("WELCOME TO SURVEY MANAGEMENT SYSTEM");page.getContentPane().add(label);
TabbedPaneDemo tp= new TabbedPaneDemo();
-
8/9/2019 Survey Management
2/25
}else
{System.out.println("enter the valid username and password");JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);}
}}class Survey{public static void main(String arg[]){int i;try{Login frame=new Login();frame.setSize(300,100);frame.setVisible(true);
}catch(Exception e){JOptionPane.showMessageDialog(null, e.getMessage());}
}}
class TabbedPaneDemo extends JFrame{
TabbedPaneDemo(){
JFrame frame = new JFrame("Survey Management System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JTabbedPane tab = new JTabbedPane();frame.add(tab, BorderLayout.CENTER);
tab.add("Agriculture",new AgriculturePanel());
tab.add("Education",new EducationPanel());
tab.add("Hospital",new HospitalPanel());
tab.add("Population",new PopulationPanel());frame.setSize(800,800);frame.setVisible(true);}
}
class NextPage extends JFrame{NextPage(){setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);setTitle("WELCOME ");setSize(400, 200);
}}
-
8/9/2019 Survey Management
3/25
class AgriculturePanel extends JPanel implements ActionListener
{ //int i,j,k,r;JTextField agt1,agt2,agt3;JLabel ag1,ag2,ag3;JButton abu,abv,abd;Connection conn;
Statement stmt;//Image img;public AgriculturePanel(){
// MediaTracker mt = new MediaTracker(this);//img = Toolkit.getDefaultToolkit().getImage("1.jpg");// mt.addImage(img,0);
/*final ImageIcon R1=new ImageIcon("D:\\torres\\1.jpg");
JPanel panel = new JPanel()
{ protected void paintComponent(Graphics g){ System.out.println("paint component");
g.drawImage(R1.getImage(), 0, 0,null);super.paintComponent(g);
}};
panel.setOpaque( false );panel.setPreferredSize(new Dimension(582,622));panel.setBounds(0,0,572,622);add(panel);panel.setLayout(null);*/
ag1 = new JLabel("Enter the amount of RICE");ag2 = new JLabel("Enter the amount of WHEAT");ag3 = new JLabel("Enter the amount of BARLEY");agt1= new JTextField(15);agt2= new JTextField(15);agt3= new JTextField(15);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){}
abd=new JButton("Download");
-
8/9/2019 Survey Management
4/25
abu=new JButton("Update");abv=new JButton("View");
setBackground(Color.green);setLayout(null);
//PImage ai = new PImage();
add(ag1);add(ag2);add(ag3);add(agt1);add(agt2);add(agt3);add(abu);add(abv);add(abd);
abd.addActionListener(this);
abu.addActionListener(this);abv.addActionListener(this);
ag1.setBounds(100,100,300,25);ag2.setBounds(100,200,300,25);ag3.setBounds(100,300,300,25);
agt1.setBounds(420,100,100,25);agt2.setBounds(420,200,100,25);agt3.setBounds(420,300,100,25);
abu.setBounds(75,450,150,30);
abv.setBounds(300,450,150,30);abd.setBounds(500,450,150,30);
}
public void actionPerformed(ActionEvent ae){
int i,j,k,r;String s1=ae.getActionCommand();if(s1.equals("Update")){
//System.out.println("the button is pressed");
i=Integer.parseInt(agt1.getText());j=Integer.parseInt(agt2.getText());k=Integer.parseInt(agt3.getText());
try{
String str1="update agri set amount="+i+" where crop='Rice';";
r= stmt.executeUpdate(str1);
String str2="update agri set amount="+j+" where crop='Wheat';";
-
8/9/2019 Survey Management
5/25
r= stmt.executeUpdate(str2);
String str3="update agri set amount="+k+" where crop='Barley';";
r= stmt.executeUpdate(str3);JOptionPane.showMessageDialog(this,"UPDA
TE SUCCESSFULL!!",
"SUCCESS",JOptionPane.INFORMATION_MESSAGE);}
catch(Exception e){}
}
else if(s1.equals("View")){
JFrame f = new JFrame("Pie Chart");
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.getContentPane().add(new PieChartPanel());f.setSize(350,300);f.setLocation(300,300);f.setVisible(true);
}else if(s1.equals("Download")){
System.out.println("button pressed");String strd="";int l,m,n;String str5="select * from agri";
//System.out.println(str5);
try{
ResultSet rst2=stmt.executeQuery(str5);rst2.next();l=Integer.parseInt(rst2.getString(3));rst2.next();
m=Integer.parseInt(rst2.getString(3));
rst2.next();n=Integer.parseInt(rst2.getString(3));
File f=new File("CROPS.txt");FileOutputStream fop=new FileOutputStrea
m(f);if(f.exists()){
strd="The amount of rice is: "+l+" ";fop.write(strd.getBytes());
-
8/9/2019 Survey Management
6/25
strd="The amount of wheat is: "+m+" ";
fop.write(strd.getBytes());
strd="The amount of barley is: "+n+"";
fop.write(strd.getBytes());
fop.flush();fop.close();System.out.println("The data has been wr
itten");}
elseSystem.out.println("This file is
not exist");JOptionPane.showMessageDialog(this,"DOWN
LOAD SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);}catch(Exception e){}
}
}
}
class EducationPanel extends JPanel implements ActionListener{
JLabel ae1,ae2,ae3;JButton aebu,aebv,aebd;JTextField aet1,aet2,aet3;Connection conn;Statement stmt;public EducationPanel(){
ae1 = new JLabel("Enter the number of pre primary schools");ae2 = new JLabel("Enter the number of primary schools");ae3 = new JLabel("Enter the number of higher secondary schools")
;aet1= new JTextField(15);aet2= new JTextField(15);aet3= new JTextField(15);aebu=new JButton("Update");aebv=new JButton("View");aebd=new JButton("Download");
setBackground(Color.yellow);
-
8/9/2019 Survey Management
7/25
setLayout(null);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){}
add(ae1);add(ae2);add(ae3);add(aet1);add(aet2);add(aet3);add(aebu);add(aebv);add(aebd);
aebu.addActionListener(this);aebv.addActionListener(this);
aebd.addActionListener(this);
ae1.setBounds(100,100,300,25);ae2.setBounds(100,200,300,25);ae3.setBounds(100,300,300,25);
aet1.setBounds(420,100,100,25);aet2.setBounds(420,200,100,25);aet3.setBounds(420,300,100,25);
aebu.setBounds(75,450,150,30);aebv.setBounds(300,450,150,30);aebd.setBounds(500,450,150,30);
}public void actionPerformed(ActionEvent ae)
{int i,j,k,r;String s1=ae.getActionCommand();if(s1.equals("Update")){
System.out.println("the button is pressed");
i=Integer.parseInt(aet1.getText());
j=Integer.parseInt(aet2.getText());k=Integer.parseInt(aet3.getText());
-
8/9/2019 Survey Management
8/25
//System.out.println(i);//System.out.println(j);//System.out.println(k);try{
String str1="update edu set Number1="+i+" where Type='PrePrimary';";
r= stmt.executeUpdate(str1);
String str2="update edu set Number1="+j+" where Type='Primary';";
r= stmt.executeUpdate(str2);
String str3="update edu set Number1="+k+" where Type='HigherSecondary';";
r= stmt.executeUpdate(str3);JOptionPane.showMessageDialog(this,"UPDA
TE SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);}
catch(Exception e){}
}else if(s1.equals("View")){
JFrame f = new JFrame("Pie Chart");//f.setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);f.getContentPane().add(new PieChartPanel1());f.setSize(350,300);f.setLocation(300,300);f.setVisible(true);
}else if(s1.equals("Download")){
System.out.println("button pressed");String strd="";int l,m,n;String str5="select * from edu";//System.out.println(str5);
try{
ResultSet rst2=stmt.executeQuery(str5);rst2.next();l=Integer.parseInt(rst2.getString(3));rst2.next();
m=Integer.parseInt(rst2.getString(3));
rst2.next();n=Integer.parseInt(rst2.getString(3));
-
8/9/2019 Survey Management
9/25
File f=new File("SCHOOLS.txt");FileOutputStream fop=new FileOutputStrea
m(f);
if(f.exists()){strd="The number of Pre Primary schools
is: "+l+" ";fop.write(strd.getBytes());
strd="The number of Primary schools is:"+m+" ";
fop.write(strd.getBytes());
strd="The number of Higher Secondary sch
ools is: "+n+" "; fop.write(strd.getBytes());
fop.flush();fop.close();System.out.println("The data has been wr
itten");}
elseSystem.out.println("This file is
not exist");JOptionPane.showMessageDialog(this,"DOWN
LOAD SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){}
}
}
}
class HospitalPanel extends JPanel implements ActionListener{
JTextField aht1,aht2,aht3;JButton ahbu,ahbv,ahbd;JLabel ah1,ah2,ah3;Connection conn;Statement stmt;public HospitalPanel(){
ah1 = new JLabel("Enter the number of Clinics");ah2 = new JLabel("Enter the number of Vetenary Hospitals");ah3 = new JLabel("Enter the number of Medical Colleges");
-
8/9/2019 Survey Management
10/25
aht1= new JTextField(15);aht2= new JTextField(15);aht3= new JTextField(15);ahbu=new JButton("Update");ahbv=new JButton("View");ahbd=new JButton("Download");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");
stmt=conn.createStatement();}catch(Exception e){}
setBackground(Color.cyan);setLayout(null);
add(ah1);add(ah2);
add(ah3);add(aht1);add(aht2);add(aht3);add(ahbu);add(ahbv);add(ahbd);
ahbu.addActionListener(this);ahbv.addActionListener(this);ahbd.addActionListener(this);
ah1.setBounds(100,100,300,25);ah2.setBounds(100,200,300,25);ah3.setBounds(100,300,300,25);
aht1.setBounds(420,100,100,25);aht2.setBounds(420,200,100,25);aht3.setBounds(420,300,100,25);
ahbu.setBounds(75,450,150,30);ahbv.setBounds(300,450,150,30);ahbd.setBounds(500,450,150,30);
}public void actionPerformed(ActionEvent ae){
-
8/9/2019 Survey Management
11/25
int i,j,k,r;String s1=ae.getActionCommand();if(s1.equals("Update")){
//System.out.println("the button is pressed");
i=Integer.parseInt(aht1.getText());
j=Integer.parseInt(aht2.getText());k=Integer.parseInt(aht3.getText());
//System.out.println(i);//System.out.println(j);//System.out.println(k);try{
String str1="update hospital set Number1
="+i+" where Type='Clinics';";
r= stmt.executeUpdate(str1);
String str2="update hospital set Number1="+j+" where Type='VetHospital';";
r= stmt.executeUpdate(str2);
String str3="update hospital set Number1="+k+" where Type='MedicalCollege';";
r= stmt.executeUpdate(str3);JOptionPane.showMessageDialog(this,"UPDA
TE SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e){}
}
else if(s1.equals("View")){
JFrame f = new JFrame("Pie Chart");//f.setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);f.getContentPane().add(new PieChartPanel2());f.setSize(350,300);f.setLocation(300,300);f.setVisible(true);
}else if(s1.equals("Download")){
System.out.println("button pressed");String strd="";int l,m,n;String str5="select * from hospital";//System.out.println(str5);
try{
-
8/9/2019 Survey Management
12/25
ResultSet rst2=stmt.executeQuery(str5);rst2.next();l=Integer.parseInt(rst2.getString(3));rst2.next();
m=Integer.parseInt(rst2.getString(3));
rst2.next();n=Integer.parseInt(rst2.getString(3));
File f=new File("HospitalS.txt");FileOutputStream fop=new FileOutputStrea
m(f);if(f.exists())
{strd="The number of Clinics is: "+l+"
";fop.write(strd.getBytes());
strd="The number of Vetinary Hospitalsis: "+m+" ";
fop.write(strd.getBytes());
strd="The number of Medical Colleges is:"+n+" ";
fop.write(strd.getBytes());
fop.flush();fop.close();System.out.println("The data has been wr
itten");}
elseSystem.out.println("This file is
not exist");JOptionPane.showMessageDialog(this,"DOWN
LOAD SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){}
}
}
}class PopulationPanel extends JPanel implements ActionListener{
Connection conn;
Statement stmt;JLabel ap1,ap2,ap3;JTextField apt1,apt2;
-
8/9/2019 Survey Management
13/25
JButton apbu,apbv,apbd;public PopulationPanel(){
ap1 = new JLabel("Enter the number of Males");ap2 = new JLabel("Enter the number of Females");apt1= new JTextField(15);apt2= new JTextField(15);
apbu= new JButton("Update");apbv= new JButton("View");apbd =new JButton("Download");try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e){}
try
{ conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){}
setBackground(Color.cyan);setLayout(null);
setBackground(Color.pink);setLayout(null);
add(ap1);add(ap2);
add(apt1);add(apt2);add(apbu);add(apbv);add(apbd);
apbu.addActionListener(this);apbv.addActionListener(this);apbd.addActionListener(this);
ap1.setBounds(100,100,300,25);ap2.setBounds(100,200,300,25);
apt1.setBounds(420,100,100,25);apt2.setBounds(420,200,100,25);
-
8/9/2019 Survey Management
14/25
apbu.setBounds(75,450,150,30);apbv.setBounds(300,450,150,30);apbd.setBounds(500,450,150,30);
}public void actionPerformed(ActionEvent ae)
{int i,j,r;
String s1=ae.getActionCommand();if(s1.equals("Update")){
//System.out.println("the button is pressed");
i=Integer.parseInt(apt1.getText());j=Integer.parseInt(apt2.getText());//k=Integer.parseInt(apt3.getText());
//System.out.println(i);//System.out.println(j);//System.out.println(k);
try{
String str1="update population set Numbe
r1="+i+" where Gender='Male';";r= stmt.executeUpdate(str1);
String str2="update population set Number1="+j+" where Gender='Female';";
r= stmt.executeUpdate(str2);
JOptionPane.showMessageDialog(this,"UPDA
TE SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e){}
}else if(s1.equals("View")){
JFrame f = new JFrame("Pie Chart");//f.setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);f.getContentPane().add(new PieChartPanel3());f.setSize(350,300);f.setLocation(300,300);f.setVisible(true);
}else if(s1.equals("Download")){
System.out.println("button pressed");String strd="";int l,m,n;String str5="select * from population";
//System.out.println(str5);
try
-
8/9/2019 Survey Management
15/25
{
ResultSet rst2=stmt.executeQuery(str5);rst2.next();l=Integer.parseInt(rst2.getString(3));rst2.next();
m=Integer.parseInt(rst2.getString(3));
n=l+m;//rst2.next();// n=Integer.parseInt(rst2.getString(3));
File f=new File("POPULATION.txt");
FileOutputStream fop=new FileOutputStream(f);if(f.exists()){strd="The number of Males is: "+l+" "
;fop.write(strd.getBytes());
strd="The number of Females is: "+m+"";
fop.write(strd.getBytes());
strd="The total Population is: "+n+"";
fop.write(strd.getBytes());
fop.flush();fop.close();System.out.println("The data has been wr
itten");}
elseSystem.out.println("This file is
not exist");JOptionPane.showMessageDialog(this,"DOWN
LOAD SUCCESSFULL!!","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){}
}
}
}
-
8/9/2019 Survey Management
16/25
class PieChartPanel extends JPanel{
Connection conn;Statement stmt;BufferedImage image;final int PAD = 30;
Font font;NumberFormat numberFormat;
public PieChartPanel(){
font = new Font("Book Antiqua", Font.BOLD, 20);numberFormat = NumberFormat.getPercentInstance();addMouseListener(new Visibility(this));addComponentListener(new ComponentAdapter(){});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){
}
}protected void paintComponent(Graphics graphics){
super.paintComponent(graphics);Graphics2D graphics2d = (Graphics2D)graphics;graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIAS
ING,RenderingHints.VALUE_ANTIALIAS_ON);
createChartImage();graphics2d.drawImage(image, 0, 0, this);
}private void createChartImage(){
int i,j=0;String str1="select * from agri; ";//String str2="select * from edu ;";//String str3="select * from edu ";int marks[]=new int[3];try{ResultSet rst1=stmt.executeQuery(str1);//ResultSet rst2=stmt.executeQuery(str2);
while(rst1.next())
{
-
8/9/2019 Survey Management
17/25
i=Integer.parseInt(rst1.getString(3));
marks[j]=i;j++;
}
int width = getWidth();int height = getHeight();int cp = width/2;int cq = height/2;image = new BufferedImage(width, height, BufferedImage.T
YPE_INT_RGB);Graphics2D g2 = image.createGraphics();g2.setPaint(Color.blue);g2.fillRect(0, 0, width, height);g2.setPaint(Color.black);int pie = Math.min(width,height) - 2*PAD;g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pi
e, pie)); double total = 0;for(int k = 0; k < 3; k++)total += marks[k];double theta = 0, phi = 0;double p, q;for(int l = 0; l < 3; l++){
p = cp + (pie/2) * Math.cos(theta);q = cq + (pie/2) * Math.sin(theta);g2.draw(new Line2D.Double(cp, cq,p , q));phi = (marks[l]/total) * 2 * Math.PI;p = cp + (9*pie/24) * Math.cos(theta + phi/2);
q = cq + (9*pie/24) * Math.sin(theta + phi/2);g2.setFont(font);String st = String.valueOf(marks[l]);FontRenderContext frc = g2.getFontRenderContext
();float textWidth = (float)font.getStringBounds(st
, frc).getWidth();LineMetrics lm = font.getLineMetrics(st, frc);float sp = (float)(p - textWidth/2);float sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + ph
i/2);q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi
/2);st = numberFormat.format(marks[l]/total);textWidth = (float)font.getStringBounds(st, frc)
.getWidth();lm = font.getLineMetrics(st, frc);sp = (float)(p - textWidth/2);sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);theta += phi;
}g2.dispose();
}catch(SQLException se){
-
8/9/2019 Survey Management
18/25
System.out.println(se);}
}public void toggleVisibility(){
repaint();}
}
class Visibility extends MouseAdapter{
PieChartPanel piechart;public Visibility(PieChartPanel pc){
piechart = pc;}public void mousePressed(MouseEvent event){
if(event.getClickCount() > 1)piechart.toggleVisibility();}
}
class PieChartPanel1 extends JPanel{
Connection conn;Statement stmt;BufferedImage image;final int PAD = 30;Font font;
NumberFormat numberFormat;
public PieChartPanel1(){
font = new Font("Book Antiqua", Font.BOLD, 20);numberFormat = NumberFormat.getPercentInstance();addMouseListener(new Visibility1(this));addComponentListener(new ComponentAdapter(){});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){}
}protected void paintComponent(Graphics graphics)
-
8/9/2019 Survey Management
19/25
{super.paintComponent(graphics);Graphics2D graphics2d = (Graphics2D)graphics;graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIAS
ING,RenderingHints.VALUE_ANTIALIAS_ON);
createChartImage();
graphics2d.drawImage(image, 0, 0, this);}private void createChartImage(){
int i,j=0;//String str1="select * from agri; ";String str2="select * from edu ;";//String str3="select * from edu ";int marks[]=new int[3];try{//ResultSet rst1=stmt.executeQuery(str1);
ResultSet rst2=stmt.executeQuery(str2);
while(rst2.next())
{i=Integer.parseInt(rst2.getString(3));
marks[j]=i;j++;
}
int width = getWidth();
int height = getHeight();int cp = width/2;int cq = height/2;image = new BufferedImage(width, height, BufferedImage.T
YPE_INT_RGB);Graphics2D g2 = image.createGraphics();g2.setPaint(Color.blue);g2.fillRect(0, 0, width, height);g2.setPaint(Color.black);int pie = Math.min(width,height) - 2*PAD;g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pi
e, pie));double total = 0;for(int k = 0; k < 3; k++)total += marks[k];double theta = 0, phi = 0;double p, q;for(int l = 0; l < 3; l++){
p = cp + (pie/2) * Math.cos(theta);q = cq + (pie/2) * Math.sin(theta);g2.draw(new Line2D.Double(cp, cq, p, q));phi = (marks[l]/total) * 2 * Math.PI;p = cp + (9*pie/24) * Math.cos(theta + phi/2);q = cq + (9*pie/24) * Math.sin(theta + phi/2);
g2.setFont(font);String st = String.valueOf(marks[l]);FontRenderContext frc = g2.getFontRenderContext
-
8/9/2019 Survey Management
20/25
();float textWidth = (float)font.getStringBounds(st
, frc).getWidth();LineMetrics lm = font.getLineMetrics(st, frc);float sp = (float)(p - textWidth/2);float sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);
p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
st = numberFormat.format(marks[l]/total);textWidth = (float)font.getStringBounds(st, frc)
.getWidth();lm = font.getLineMetrics(st, frc);sp = (float)(p - textWidth/2);sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);theta += phi;
}g2.dispose();}catch(SQLException se){
System.out.println(se);}
}public void toggleVisibility(){
repaint();}
}
class Visibility1 extends MouseAdapter{
PieChartPanel1 piechart;public Visibility1(PieChartPanel1 pc){
piechart = pc;}public void mousePressed(MouseEvent event){
if(event.getClickCount() > 1)piechart.toggleVisibility();
}}class PieChartPanel2 extends JPanel
{
Connection conn;Statement stmt;BufferedImage image;final int PAD = 30;Font font;NumberFormat numberFormat;
public PieChartPanel2(){
font = new Font("Book Antiqua", Font.BOLD, 20);
-
8/9/2019 Survey Management
21/25
numberFormat = NumberFormat.getPercentInstance();addMouseListener(new Visibility2(this));addComponentListener(new ComponentAdapter(){});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (Exception e){}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){
}
}protected void paintComponent(Graphics graphics){
super.paintComponent(graphics);Graphics2D graphics2d = (Graphics2D)graphics;graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIAS
ING,RenderingHints.VALUE_ANTIALIAS_ON);
createChartImage();graphics2d.drawImage(image, 0, 0, this);
}
private void createChartImage(){
int i,j=0;//String str1="select * from agri; ";//String str2="select * from edu ;";String str3="select * from hospital; ";int marks[]=new int[3];try{//ResultSet rst1=stmt.executeQuery(str1);ResultSet rst3=stmt.executeQuery(str3);
while(rst3.next())
{i=Integer.parseInt(rst3.getString(3));
marks[j]=i;j++;
}
int width = getWidth();int height = getHeight();int cp = width/2;
int cq = height/2;image = new BufferedImage(width, height, BufferedImage.T
YPE_INT_RGB);
-
8/9/2019 Survey Management
22/25
Graphics2D g2 = image.createGraphics();g2.setPaint(Color.blue);g2.fillRect(0, 0, width, height);g2.setPaint(Color.black);int pie = Math.min(width,height) - 2*PAD;g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pi
e, pie));
double total = 0;for(int k = 0; k < 3; k++)total += marks[k];double theta = 0, phi = 0;double p, q;for(int l = 0; l < 3; l++){
p = cp + (pie/2) * Math.cos(theta);q = cq + (pie/2) * Math.sin(theta);g2.draw(new Line2D.Double(cp, cq, p, q));phi = (marks[l]/total) * 2 * Math.PI;p = cp + (9*pie/24) * Math.cos(theta + phi/2);
q = cq + (9*pie/24) * Math.sin(theta + phi/2);g2.setFont(font);String st = String.valueOf(marks[l]);FontRenderContext frc = g2.getFontRenderContext
();float textWidth = (float)font.getStringBounds(st
, frc).getWidth();LineMetrics lm = font.getLineMetrics(st, frc);float sp = (float)(p - textWidth/2);float sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + ph
i/2);
q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
st = numberFormat.format(marks[l]/total);textWidth = (float)font.getStringBounds(st, frc)
.getWidth();lm = font.getLineMetrics(st, frc);sp = (float)(p - textWidth/2);sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);theta += phi;
}g2.dispose();}catch(SQLException se){
System.out.println(se);}
}public void toggleVisibility(){
repaint();}
}
class Visibility2 extends MouseAdapter{
PieChartPanel2 piechart;
-
8/9/2019 Survey Management
23/25
public Visibility2(PieChartPanel2 pc){
piechart = pc;}public void mousePressed(MouseEvent event){
if(event.getClickCount() > 1)
piechart.toggleVisibility();}
}class PieChartPanel3 extends JPanel
{
Connection conn;Statement stmt;BufferedImage image;final int PAD = 30;Font font;NumberFormat numberFormat;
public PieChartPanel3(){
font = new Font("Book Antiqua", Font.BOLD, 20);numberFormat = NumberFormat.getPercentInstance();addMouseListener(new Visibility3(this));addComponentListener(new ComponentAdapter(){});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch (Exception e)
{}
try{
conn=DriverManager.getConnection("jdbc:odbc:sms");stmt=conn.createStatement();
}catch(Exception e){}
}protected void paintComponent(Graphics graphics){
super.paintComponent(graphics);Graphics2D graphics2d = (Graphics2D)graphics;graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIAS
ING,RenderingHints.VALUE_ANTIALIAS_ON);
createChartImage();graphics2d.drawImage(image, 0, 0, this);
}private void createChartImage(){
int i,j=0;//String str1="select * from agri; ";String str2="select * from population ;";
-
8/9/2019 Survey Management
24/25
//String str3="select * from edu ";int marks[]=new int[3];try{//ResultSet rst1=stmt.executeQuery(str1);ResultSet rst2=stmt.executeQuery(str2);
while(rst2.next())
{i=Integer.parseInt(rst2.getString(3));
marks[j]=i;j++;
}
int width = getWidth();int height = getHeight();
int cp = width/2;int cq = height/2;image = new BufferedImage(width, height, BufferedImage.T
YPE_INT_RGB);Graphics2D g2 = image.createGraphics();g2.setPaint(Color.blue);g2.fillRect(0, 0, width, height);g2.setPaint(Color.black);int pie = Math.min(width,height) - 2*PAD;g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pi
e, pie));double total = 0;for(int k = 0; k < 2; k++)
total += marks[k];double theta = 0, phi = 0;double p, q;for(int l = 0; l < 2; l++){
p = cp + (pie/2) * Math.cos(theta);q = cq + (pie/2) * Math.sin(theta);g2.draw(new Line2D.Double(cp, cq, p, q));phi = (marks[l]/total) * 2 * Math.PI;p = cp + (9*pie/24) * Math.cos(theta + phi/2);q = cq + (9*pie/24) * Math.sin(theta + phi/2);g2.setFont(font);String st = String.valueOf(marks[l]);FontRenderContext frc = g2.getFontRenderContext
();float textWidth = (float)font.getStringBounds(st
, frc).getWidth();LineMetrics lm = font.getLineMetrics(st, frc);float sp = (float)(p - textWidth/2);float sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + ph
i/2);q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi
/2);
st = numberFormat.format(marks[l]/total);textWidth = (float)font.getStringBounds(st, frc)
.getWidth();
-
8/9/2019 Survey Management
25/25
lm = font.getLineMetrics(st, frc);sp = (float)(p - textWidth/2);sq = (float)(q + lm.getAscent()/2);g2.drawString(st, sp, sq);theta += phi;
}g2.dispose();
}catch(SQLException se){
System.out.println(se);}
}public void toggleVisibility(){
repaint();}
}
class Visibility3 extends MouseAdapter{
PieChartPanel3 piechart;public Visibility3(PieChartPanel3 pc){
piechart = pc;}public void mousePressed(MouseEvent event){
if(event.getClickCount() > 1)piechart.toggleVisibility();
}
}