Package

Source Code of DataCollection

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;

public class DataCollection {
 
  private int currentNum;
  private JPanel pie;
  private JPanel jp;
  private JPanel c = new JPanel();
  private JComboBox box;
  JTextField flag[];
  private String[][] dataflag;
  private String[][] defaultflag;
  private int[][] data;
  private int[] dataAnsNum;
  private int QuestionNum;
  private int AnsNum;
  private GUI g;

  DataCollection(GUI g, int n, int a) {
    this.currentNum=0;
    this.g = g;
    this.QuestionNum = n;
    this.AnsNum = a;
    this.dataAnsNum = new int[n];
    this.dataflag = new String[n][a];
   
    data = new int[n][a];
   
   

    this.defaultflag=new String[5][];
    /*for(int i=0;i<5;i++){
      String[] temp=new String[i+1];
      this.defaultflag[i]=temp;
    }*/
    this.defaultflag[1]=new String[]{"1.是","2.否"};
    this.defaultflag[2]=new String[]{"1.太快","2.適中","3.太慢"};
    this.defaultflag[3]=new String[]{"1.非常滿意","2.很滿意","3.不滿意","4.非常不滿意"};
    this.defaultflag[4]=new String[]{"1.非常滿意","2.很滿意","3.滿意","4.不滿意","5.非常不滿意"};
   
    for (int i = 0; i < n; i++) {
      this.dataAnsNum[i] = a;
      for (int j = 0; j < a; j++) {
        this.data[i][j] = 0;
        //System.out.println(n+":"+this.data[i][j]);
        if(a<=this.defaultflag.length){
          //System.out.println(flag[i]);
          this.dataflag[i][j]=this.defaultflag[a-1][j];
        }else{
          this.dataflag[i][j]=String.valueOf(j+1);
        }
      }
    }
   
    this.setCountrol();
  }

  public void addData(String s) {
    char[] temp = s.toCharArray();
    if (temp.length > this.QuestionNum) {
      JOptionPane.showMessageDialog(
          g,
          "( ´_ゝ`):「你多打了"
              + Integer.valueOf(temp.length - this.QuestionNum)
              + "個答案……(嘆氣)」", "你手.殘.了", JOptionPane.ERROR_MESSAGE);
    } else if (temp.length < this.QuestionNum) {
      JOptionPane.showMessageDialog(
          g,
          "( ´_ゝ`):「你打少了"
              + Integer.valueOf(this.QuestionNum - temp.length)
              + "個答案……(嘆氣)」", "你手.殘.了", JOptionPane.ERROR_MESSAGE);
    } else {
      boolean chack = true;
      for (int i = 0; i < this.QuestionNum; i++) {
        if (Integer.valueOf(new Character(temp[i]).toString())< 1
            || Integer.valueOf(new Character(temp[i]).toString())  > this.dataAnsNum[i]) {
          chack = false;
          JOptionPane.showMessageDialog(g, "(#°Д°)σ:「你第" +
              String.valueOf(i+1) +
              "題打成"+temp[i]+"了!!!」", "你手.殘.了",
              JOptionPane.ERROR_MESSAGE);
          break;
        }
      }
      if (chack) {
        for (int i = 0; i < this.QuestionNum; i++) {
          this.data[i][Integer.valueOf(new Character(temp[i])
              .toString()) - 1]++;
        }
      }
    }
    // this.showData();

  }

  public JPanel getPie() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int i = 0; i < this.dataAnsNum[this.currentNum]; i++) {
      //System.out.println(this.currentNum);
      dataset.setValue(this.dataflag[this.currentNum][i], this.data[this.currentNum][i]);

    }
    JFreeChart chart = ChartFactory.createPieChart3D(null, dataset, // data
        true, // include legend
        true, true);

    // chart.getTitle().setFont(new Font("新細明體", Font.BOLD, 20));
    chart.getLegend().setItemFont(new Font("微軟正黑體", Font.PLAIN, 15));
    PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
    pieplot3d.setLabelFont(new Font("微軟正黑體", Font.PLAIN, 12));
    pieplot3d.setLabelGenerator(new   StandardPieSectionLabelGenerator( "{0}--{2}"));

    ChartPanel cp = new ChartPanel(chart);
    cp.setPreferredSize(new java.awt.Dimension(600, 300));

    return cp;
  }
  public JPanel getOption(){
    JPanel option=new JPanel()
    flag=new JTextField[this.dataAnsNum[this.currentNum]];
    for(int i=0;i<this.dataAnsNum[this.currentNum];i++){

        flag[i]=new JTextField(this.dataflag[this.currentNum][i],8);
        option.add(flag[i]);
     
      flag[i].addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e) {
          if(e.getKeyCode()==KeyEvent.VK_ENTER){
            for(int i=0;i<flag.length;i++){
              //System.out.println(flag.length);
              dataflag[currentNum][i]=flag[i].getText();
             
            }
            pie.removeAll();
            pie.add(getPie());
            pie.updateUI();
           
          }
        }
      });
    }
   
    return option;
  }

  public void setCountrol() {
    jp = new JPanel();

    this.pie=new JPanel();
    this.pie.removeAll();
    jp.removeAll();
    c.removeAll();
    String[] list = new String[this.AnsNum-1];
    for (int i = 1; i < this.AnsNum; i++) {
      list[i-1] = String.valueOf(i + 1);
    }
    this.box = new JComboBox(list);
    this.box.setSelectedIndex(this.dataAnsNum[this.currentNum]-2);
    this.box.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent event) {
        dataAnsNum[currentNum] = box.getSelectedIndex() + 2;
        for(int i=0;i<dataAnsNum[currentNum];i++){
          //System.out.println(defaultflag[dataAnsNum[currentNum]-1][i]);
          dataflag[currentNum][i]=defaultflag[dataAnsNum[currentNum]-1][i];
         
        }
        pie.removeAll();
        pie.add(getPie());
        pie.updateUI();
        jp.removeAll();
        jp.add(c, BorderLayout.SOUTH);
        jp.add(getOption(),BorderLayout.CENTER);
        jp.add(pie, BorderLayout.NORTH);
        jp.updateUI();
      }
    });

    c.add(new JLabel("這題共有"));
    c.add(this.box);
    c.add(new JLabel("個選項"));
    pie.add(this.getPie());
    jp.setLayout(new BorderLayout());
    jp.add(c, BorderLayout.SOUTH);
    jp.add(getOption(),BorderLayout.CENTER);
    jp.add(pie, BorderLayout.NORTH);

  }

  public JPanel showData(int n) {
    this.currentNum=n;
    this.setCountrol();
    jp.updateUI();
   
    return jp;
  }
  public String getData(){
    String d=new String();
   
    for(int i=0;i<this.QuestionNum;i++){
      d+="Q" +String.valueOf(i+1)+":   ";
      for(int j=0;j<this.dataAnsNum[i];j++){
        d+=this.data[i][j]+",";
      }
      d+="\n";
    }
    d+="選項1→"+this.AnsNum;
    return d;
  }

  public void showData() {
    for (int i = 0; i < this.QuestionNum; i++) {
      for (int j = 0; j < 5; j++) {
        System.out.print(this.data[i][j]+1 + ",");
      }
      System.out.print("\n");
    }
    System.out.println("next>");
  }

}
TOP

Related Classes of DataCollection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.