⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bayes2d.java

📁 A Web Tutorial on Discrete Features of Bayes Decision Theory This applet allows for the calculation
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * bayes.java * * Created on April 28, 2004, 12:09 PM */package applet;import java.awt.*;import java.applet.Applet;import javax.swing.*; import java.awt.geom.*;import java.awt.*;import java.awt.event.*;import java.math.*;/** * * @author  friggi */public class bayes2d extends java.applet.Applet {        /** Initialization method that will be called after the applet is loaded     *  into the browser.     */       /** Initialization method that will be called after the applet is loaded     *  into the browser.     */        private double pw1;    private double pw2;   // private int  x2, y1, y2;    private Graphics g1;          private int   xx1 = 0;     private int   xx2 = 0;      private int  yy1= 0;     private int   yy2= 0;           private int   x1 = 0;     private int   x2 = 0;      private int  y1= 0;     private int   y2= 0;              double [] x = new double[80];    double [] y = new double[80];            public void init() {        // TODO start asynchronous download of heavy resources         txt_pw1 = new java.awt.TextField();        txt_pw2 = new java.awt.TextField();        btn_calculate = new java.awt.Button();        scr_pw1 = new java.awt.Scrollbar();        scr_pw2 = new java.awt.Scrollbar();        label1 = new java.awt.Label();        label2 = new java.awt.Label();       /* panel1 = new java.awt.Panel();*/        txt_q1 = new java.awt.TextField();        txt_q2 = new java.awt.TextField();        txt_p1 = new java.awt.TextField();        txt_p2 = new java.awt.TextField();        lbl_q1 = new java.awt.Label();        lbl_q2 = new java.awt.Label();        lbl_p1 = new java.awt.Label();        lbl_p2 = new java.awt.Label();        scr_p1 = new java.awt.Scrollbar();        scr_p2 = new java.awt.Scrollbar();        scr_q1 = new java.awt.Scrollbar();        scr_q2 = new java.awt.Scrollbar();        txt_answer = new java.awt.TextField();        label3 = new java.awt.Label();        label4 = new java.awt.Label();        rect = new java.awt.Rectangle();        setLayout(null);        setForeground(java.awt.Color.black);                txt_pw1.setText("0");        txt_pw1.setBackground(new java.awt.Color(255, 255, 255));        txt_pw1.setEditable(false);        txt_pw1.addTextListener(new java.awt.event.TextListener() {            public void textValueChanged(java.awt.event.TextEvent evt) {                txt_pw1TextValueChanged(evt);            }        });        add(txt_pw1);        txt_pw1.setBounds(140, 50, 30, 20);        txt_pw2.setText("1.0");        txt_pw2.setBackground(new java.awt.Color(255, 255, 255));        txt_pw2.setEditable(false);        txt_pw2.addTextListener(new java.awt.event.TextListener() {            public void textValueChanged(java.awt.event.TextEvent evt) {                txt_pw2TextValueChanged(evt);            }        });        add(txt_pw2);        txt_pw2.setBounds(140, 80, 30, 20);        btn_calculate.setLabel("Calculate");        btn_calculate.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                btn_calculateActionPerformed(evt);            }        });        add(btn_calculate);        btn_calculate.setBounds(120, 350, 60, 24);        scr_pw1.setBackground(new java.awt.Color(204, 204, 204));        scr_pw1.setBlockIncrement(1);        scr_pw1.setForeground(java.awt.Color.gray);        scr_pw1.setMaximum(20);        scr_pw1.setValue(0);        scr_pw1.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_pw1.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_pw1AdjustmentValueChanged(evt);            }        });        add(scr_pw1);        scr_pw1.setBounds(180, 50, 60, 16);        scr_pw2.setBackground(new java.awt.Color(204, 204, 204));        scr_pw2.setBlockIncrement(1);        scr_pw2.setForeground(java.awt.Color.lightGray);        scr_pw2.setMaximum(20);        scr_pw2.setValue(10);        scr_pw2.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_pw2.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_pw2AdjustmentValueChanged(evt);            }        });        add(scr_pw2);        scr_pw2.setBounds(180, 80, 60, 16);        label1.setText("Class 1 Prior - P(w1)");        add(label1);        label1.setBounds(20, 50, 118, 20);        label2.setText("Class 2 Prior - P(w2)");        add(label2);        label2.setBounds(20, 80, 118, 20);        //panel1.setBackground(new java.awt.Color(102, 102, 102));        //add(panel1);        //panel1.setBounds(380, 20, 380, 300);        txt_q1.setText("0");        txt_q1.setEditable(false);        add(txt_q1);        txt_q1.setBounds(210, 140, 30, 20);        txt_q2.setText("0");        txt_q2.setEditable(false);        add(txt_q2);        txt_q2.setBounds(210, 170, 30, 20);        txt_p1.setText("0");        txt_p1.setEditable(false);        add(txt_p1);        txt_p1.setBounds(50, 140, 30, 20);        txt_p2.setText("0");        txt_p2.setEditable(false);        add(txt_p2);        txt_p2.setBounds(50, 170, 30, 20);        lbl_q1.setText("q1");        add(lbl_q1);        lbl_q1.setBounds(180, 140, 18, 20);        lbl_q2.setText("q2");        add(lbl_q2);        lbl_q2.setBounds(180, 170, 18, 20);        lbl_p1.setText("p1");        add(lbl_p1);        lbl_p1.setBounds(30, 140, 18, 20);        lbl_p2.setText("p2");        add(lbl_p2);        lbl_p2.setBounds(30, 170, 18, 20);        scr_p1.setBackground(new java.awt.Color(204, 204, 204));        scr_p1.setBlockIncrement(1);        scr_p1.setMaximum(20);        scr_p1.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_p1.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_p1AdjustmentValueChanged(evt);            }        });        add(scr_p1);        scr_p1.setBounds(90, 140, 60, 16);        scr_p2.setBackground(new java.awt.Color(204, 204, 204));        scr_p2.setBlockIncrement(1);        scr_p2.setMaximum(20);        scr_p2.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_p2.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_p2AdjustmentValueChanged(evt);            }        });        add(scr_p2);        scr_p2.setBounds(90, 170, 60, 16);        scr_q1.setBackground(new java.awt.Color(204, 204, 204));        scr_q1.setBlockIncrement(1);        scr_q1.setMaximum(20);        scr_q1.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_q1.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_q1AdjustmentValueChanged(evt);            }        });        add(scr_q1);        scr_q1.setBounds(250, 140, 60, 16);        scr_q2.setBackground(new java.awt.Color(204, 204, 204));        scr_q2.setBlockIncrement(1);        scr_q2.setMaximum(20);        scr_q2.setOrientation(java.awt.Scrollbar.HORIZONTAL);        scr_q2.addAdjustmentListener(new java.awt.event.AdjustmentListener() {            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                scr_q2AdjustmentValueChanged(evt);            }        });        add(scr_q2);        scr_q2.setBounds(250, 170, 60, 16);        txt_answer.setBackground(new java.awt.Color(255, 255, 255));        txt_answer.setEditable(false);        add(txt_answer);        txt_answer.setBounds(560, 350, 200, 20);        label3.setText("Equation of Decision Boundary");        add(label3);        label3.setBounds(380, 350, 173, 20);        label4.setText("where pi=P(xi=1|wi) and qi=P(xi=1|w2)");        add(label4);        label4.setBounds(30, 240, 280, 20);             }        public void update( Graphics g)    {        this.g1=g;              g1.draw3DRect(380, 20, 380, 300, true);        g1.setColor(Color.white);          g1.fill3DRect(380, 20, 380, 300, true);                    //find slope and b         double m = ((double)(y2-y1))/((double)(x2-x1));         double b = (double) y2 - m*((double) x2);                  //find 4 extremum points         double y1n = -190*m+b;         double y2n = 190*m+b;         double x1n = (-150 - b)/m;         double x2n = (150 - b)/m;                   int [] xdraw = new int[2];           int [] ydraw = new int[2];           int the_temp = 0;                      //variables for filling areas           boolean left = false;           boolean right = false;           boolean top= false;           boolean bottom = false;           boolean drawn = false;    //      g1.fillPolygon(xset1, yset1, 3);                   if ((y1n >= -150) && (y1n <=150))         {             xdraw[the_temp]  = -190 +190+380;             ydraw[the_temp]  = (int) -y1n + 150 +20;             the_temp++;             left = true;             drawn = true;         }                   if ((y2n >= -150) && (y2n <=150))         {             xdraw[the_temp] = 190 + 190+380;             ydraw[the_temp] = (int) -y2n + 150+20;             the_temp++;             right = true;             drawn = true;         }                    if ((x1n >= -190) && (x1n <=190)  && (the_temp < 2))         {             xdraw[the_temp] = (int) x1n+ 190+380;             ydraw[the_temp] = 150 + 150+20; //invert the y's since 0 starts from up             the_temp++;             bottom = true;             drawn = true;         }                     if ((x2n >= -190) && (x2n <=190) && (the_temp < 2))         {             xdraw[the_temp] = (int) x2n+190+380;             ydraw[the_temp] = -150 +150+20;             the_temp++;             top = true;             drawn = true;         }                      //if vertical line then none apply           if((the_temp < 2) && (x1 <= 190))           {               xdraw[0] = x1+190+380;               xdraw[1] = x1+190+380;               ydraw[0] = 320;               ydraw[1] = 20;               top = true;               bottom = true;               drawn = true;           }                    //need to make x and y start at                    //bounds to fill areas         //order checked was left right bottom top, so the points placed would be in this order         //if points are left and bottom         if ((left == true) && (bottom == true))         {              int[] xset1 = {380, xdraw[0], xdraw[1]};              int[] yset1 = {320, ydraw[0], ydraw[1]};               g1.setColor(Color.pink);              g1.fillPolygon(xset1, yset1, 3);                            int[] xset2 = {xdraw[0], 380, 760, 760, xdraw[1]};              int[] yset2 = {ydraw[0], 20, 20, 320, ydraw[1]};               g1.setColor(Color.YELLOW);              g1.fillPolygon(xset2, yset2, 5);         }         //if points are left and right               if ((left == true) && (right == true))        {              int[] xset1 = {xdraw[0], 380, 760, xdraw[1]};              int[] yset1 = {ydraw[0], 20, 20,  ydraw[1]};               g1.setColor(Color.pink);              g1.fillPolygon(xset1, yset1, 4);                            int[] xset2 = {xdraw[0], 380, 760, xdraw[1]};              int[] yset2 = {ydraw[0], 320, 320, ydraw[1]};               g1.setColor(Color.YELLOW);              g1.fillPolygon(xset2, yset2, 4);                  }                      //if points are left and top          if ((left == true) && (top == true))          {              int[] xset1 = {xdraw[0], 380, xdraw[1]};              int[] yset1 = {ydraw[0], 20, ydraw[1]};               g1.setColor(Color.pink);              g1.fillPolygon(xset1, yset1, 3);                            int[] xset2 = {xdraw[0], xdraw[1], 760, 760, 380};              int[] yset2 = {ydraw[0], ydraw[1], 20, 320, 320};               g1.setColor(Color.YELLOW);              g1.fillPolygon(xset2, yset2, 5);                  }         //if points are right and bottom          if ((right == true) && (bottom == true))          {              int[] xset1 = {xdraw[0], 760, xdraw[1]};              int[] yset1 = {ydraw[0], 320, ydraw[1]};               g1.setColor(Color.pink);              g1.fillPolygon(xset1, yset1, 3);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -