Plotting Simple Lineare Regression Formula in JAVA
In this tutorial we are going to code a simple java program that show a linear regression model, this model is used to show or predict the relationship between two variables X and Y (Xi,Yi) with the equation Y= a + bX.
Source Code
Steps
Create a Database and name it ‘regr’ then create table ‘chart’ -> rows (Xi,Yi). don’t forget to add JAR file “JDBC” in Libraries to connect !
then create a class and name it ConnectDB and Copy / Past the code bellow !
Note : Download com.mysql.jdbc_5.1.5.jar
class ConnectDb.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package datamining.RegressionLineare; |
Create other class in same package and name it regression.java
Note : Download the jar file of “jfreechart-1.0.19” and add it in Libraries !
class regression.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | package datamining.RegressionLineare; import java.awt.HeadlessException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import net.proteanit.sql.DbUtils; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.jdbc.JDBCCategoryDataset; public class regression extends javax.swing.JFrame { Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; String sum1; String sum2; float a; float valfb; float moyexmx; double katorz; public regression() { initComponents(); conn = ConnectDb.ConnectDb(); Update_table(); } private void Update_table() { try { String sql = "select Xi,Yi from regr"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); jTable1.setModel(DbUtils.resultSetToTableModel(rs)); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } @SuppressWarnings("unchecked") // private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jPanel3 = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); xi = new javax.swing.JTextField(); yi = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jSeparator2 = new javax.swing.JSeparator(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); moyyi = new javax.swing.JLabel(); moyxi = new javax.swing.JLabel(); somxi = new javax.swing.JLabel(); somyi = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); jLabel15 = new javax.swing.JLabel(); vala = new javax.swing.JLabel(); valb = new javax.swing.JLabel(); jSeparator3 = new javax.swing.JSeparator(); jLabel18 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jLabel17 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); txtarea = new javax.swing.JTextPane(); jLabel6 = new javax.swing.JLabel(); jSeparator4 = new javax.swing.JSeparator(); jPanel4 = new javax.swing.JPanel(); vala1 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); txttest = new javax.swing.JTextField(); jLabel12 = new javax.swing.JLabel(); testres = new javax.swing.JLabel(); valb2 = new javax.swing.JLabel(); jButton5 = new javax.swing.JButton(); jLabel16 = new javax.swing.JLabel(); panel = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setUndecorated(true); jPanel1.setBackground(new java.awt.Color(0, 0, 0)); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153), 5)); jPanel2.setBackground(new java.awt.Color(0, 0, 0)); jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153))); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Régression Linéaire Simple (Y = aX + b)"); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel2.setForeground(new java.awt.Color(204, 0, 0)); jLabel2.setText("X"); jLabel2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jLabel2MouseClicked(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jLabel2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153))); jTable1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Xi", "Yi" } )); jTable1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTable1MouseClicked(evt); } }); jScrollPane1.setViewportView(jTable1); jPanel3.setBackground(new java.awt.Color(0, 0, 0)); jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153))); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel3.setForeground(new java.awt.Color(255, 255, 255)); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel3.setText("Setting"); jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255))); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel4.setForeground(new java.awt.Color(255, 255, 255)); jLabel4.setText("Xi :"); jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel5.setForeground(new java.awt.Color(255, 255, 255)); jLabel5.setText("Yi :"); xi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N yi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton1.setBackground(new java.awt.Color(0, 0, 0)); jButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 255, 255)); jButton1.setText("Insert"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setBackground(new java.awt.Color(0, 0, 0)); jButton2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 255, 255)); jButton2.setText("Delete"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel7.setForeground(new java.awt.Color(255, 255, 255)); jLabel7.setText("Somme Yi :"); jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel8.setForeground(new java.awt.Color(255, 255, 255)); jLabel8.setText("Moyenne Xi :"); jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel9.setForeground(new java.awt.Color(255, 255, 255)); jLabel9.setText("Moyenne Yi :"); moyyi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N moyyi.setForeground(new java.awt.Color(255, 255, 255)); moyyi.setText("*******"); moyxi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N moyxi.setForeground(new java.awt.Color(255, 255, 255)); moyxi.setText("*******"); somxi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N somxi.setForeground(new java.awt.Color(255, 255, 255)); somxi.setText("*******"); somyi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N somyi.setForeground(new java.awt.Color(255, 255, 255)); somyi.setText("*******"); jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel14.setForeground(new java.awt.Color(255, 255, 255)); jLabel14.setText("Value a :"); jLabel15.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel15.setForeground(new java.awt.Color(255, 255, 255)); jLabel15.setText("Value b :"); vala.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N vala.setForeground(new java.awt.Color(255, 255, 255)); vala.setText("*******"); valb.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N valb.setForeground(new java.awt.Color(255, 255, 255)); valb.setText("*******"); jLabel18.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel18.setForeground(new java.awt.Color(255, 255, 255)); jLabel18.setText("Set Plot :"); jButton3.setBackground(new java.awt.Color(0, 0, 0)); jButton3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton3.setForeground(new java.awt.Color(255, 255, 255)); jButton3.setText("Chart"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setBackground(new java.awt.Color(0, 0, 0)); jButton4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton4.setForeground(new java.awt.Color(255, 255, 255)); jButton4.setText("Calcule"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jLabel17.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel17.setForeground(new java.awt.Color(255, 255, 255)); jLabel17.setText("Somme Xi :"); jScrollPane2.setViewportView(txtarea); jLabel6.setForeground(new java.awt.Color(255, 255, 255)); jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel6.setText("****************** Output *********************"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator1) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel4) .addGap(18, 18, 18) .addComponent(xi)) .addComponent(jSeparator3) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel5) .addGap(18, 18, 18) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(yi))) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel18) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jSeparator4) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel14) .addComponent(jLabel15)) .addGap(35, 35, 35) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(valb) .addComponent(vala))) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel17) .addGap(18, 18, 18) .addComponent(somxi, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(somyi)) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(moyxi)) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(moyyi))) .addGap(0, 29, Short.MAX_VALUE))) .addContainerGap()) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(xi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(yi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) .addGap(16, 16, 16) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(somxi) .addComponent(jLabel17) .addComponent(jLabel7) .addComponent(somyi)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(moyxi)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9) .addComponent(moyyi)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel14) .addComponent(vala)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel15) .addComponent(valb)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(3, 3, 3) .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel18) .addComponent(jButton3)) .addContainerGap()) ); jPanel4.setBackground(new java.awt.Color(0, 0, 0)); jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153))); vala1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N vala1.setForeground(new java.awt.Color(255, 255, 255)); vala1.setText("****a****"); jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel11.setForeground(new java.awt.Color(51, 204, 0)); jLabel11.setText("x"); txttest.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel12.setForeground(new java.awt.Color(51, 204, 0)); jLabel12.setText("+\\-"); testres.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N testres.setForeground(new java.awt.Color(255, 255, 255)); testres.setText("Yi"); valb2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N valb2.setForeground(new java.awt.Color(255, 255, 255)); valb2.setText("****b****"); jButton5.setBackground(new java.awt.Color(0, 204, 51)); jButton5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton5.setText("="); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jLabel16.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel16.setForeground(new java.awt.Color(51, 204, 0)); jLabel16.setText("Test equation :"); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGap(11, 11, 11) .addComponent(jLabel16) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(vala1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel11) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txttest, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel12) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(valb2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(testres, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(vala1) .addComponent(jLabel11) .addComponent(txttest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel12) .addComponent(testres) .addComponent(jButton5) .addComponent(jLabel16) .addComponent(valb2)) .addContainerGap()) ); panel.setBackground(new java.awt.Color(102, 102, 102)); panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 153))); javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); panel.setLayout(panelLayout); panelLayout.setHorizontalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); panelLayout.setVerticalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); setLocationRelativeTo(null); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String sql; try { sql = "insert into regr (Xi,Yi) values (?,?)"; PreparedStatement st = conn.prepareStatement(sql); st.setString(1, xi.getText()); st.setString(2, yi.getText()); st.execute(); //JOptionPane.showMessageDialog(null, "Insert"); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "erreur " + "hhh " + e); } Update_table(); } private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) { System.exit(0); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { try { String sql = "DELETE FROM regr WHERE Xi=? "; PreparedStatement st = conn.prepareStatement(sql); st.setString(1, xi.getText()); st.execute(); JOptionPane.showMessageDialog(null, "Delete"); //ProBar.setValue(0); } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, e); } Update_table(); } private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { try { int row = jTable1.getSelectedRow(); String tableclic = (jTable1.getModel().getValueAt(row, 0).toString()); String sql = "select * from regr where Xi = '" + tableclic + "' "; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()) { String add1 = rs.getString("Xi"); xi.setText(add1); String add2 = rs.getString("Yi"); yi.setText(add2); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } public void laSomme() { try { String sql = "select sum(Xi),sum(Yi) from regr"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); if (rs.next()) { sum1 = rs.getString("sum(Xi)"); somxi.setText(sum1); sum2 = rs.getString("sum(Yi)"); somyi.setText(sum2); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "somme " + e); } } public void laMoyenne() { try { String sql = "select avg(Xi),avg(Yi) from regr"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); if (rs.next()) { sum1 = rs.getString("avg(Xi)"); moyxi.setText(sum1); sum2 = rs.getString("avg(Yi)"); moyyi.setText(sum2); katorz = Double.parseDouble(sum1); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "moyenne " + e); } } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { laSomme(); laMoyenne(); // (moyx - xi)*(moyy - yi) ************************************************************ float moyeymy; float sommoyxy = 0; float mult; try { String sql = "select Xi,Yi from regr"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { //moyexmx = (Float.parseFloat(sum1) - Float.parseFloat(rs.getString("Xi"))); moyexmx = (Float.parseFloat(rs.getString("Xi")) - Float.parseFloat(sum1)); System.err.println(moyexmx + " moyXi - Xi"); //txtarea.setText("******** Output *********"); txtarea.setText(txtarea.getText() + "\n" + moyexmx + " moyXi - Xi"); //katorz = ; //moyeymy = (Float.parseFloat(sum2) - Float.parseFloat(rs.getString("Yi"))); moyeymy = (Float.parseFloat(rs.getString("Yi")) - Float.parseFloat(sum2)); System.err.println(moyeymy + " moyYi - Yi"); txtarea.setText(txtarea.getText() + "\n" + moyeymy + " moyYi - Yi"); mult = moyexmx * moyeymy; System.err.println(mult + " Multiplication "); sommoyxy = sommoyxy + mult; txtarea.setText(txtarea.getText() + "\n" + mult + " Multiplication "); } System.err.println(sommoyxy + " Somme Multiplication "); txtarea.setText(txtarea.getText() + "\n" + sommoyxy + " Somme Multiplication "); //********************************************************** } catch (Exception e) { JOptionPane.showMessageDialog(null, "Moyx - Xi " + e); } //********************************************************************** //(Xi -moyx)^2 ****************************************************************** double somcarr = 0; try { String sql = "select Xi from regr"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { double xicarr = Math.pow(Double.parseDouble(rs.getString("Xi")) - katorz, 2); //xicar.setText(Double.toString(xicarr)); System.err.println(xicarr + " "); txtarea.setText(txtarea.getText() + "\n" + xicarr + " (MoyXi - Xi)^2"); somcarr = somcarr + xicarr; } System.err.println(somcarr + " 176.56 "); txtarea.setText(txtarea.getText() + "\n" + somcarr + " Somme carré "); //********************************************************** } catch (Exception e) { JOptionPane.showMessageDialog(null, "x^2 " + e); } // (moyx - xi)*(moyy - yi)/Xi^é **************************************** //float a; float moyy = Float.parseFloat(moyyi.getText()); float moyx = Float.parseFloat(moyxi.getText()); a = (float) (sommoyxy/somcarr); vala.setText(Float.toString(a)); vala1.setText(Float.toString(a)); valfb = moyy - (a * (moyx)); valb.setText(Float.toString(valfb)); valb2.setText(Float.toString(valfb)); //********************************************************************** } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { panel.removeAll(); try { String Query = "select Xi,Yi from regr"; JDBCCategoryDataset dataset = new JDBCCategoryDataset(ConnectDb.ConnectDb(), Query); JFreeChart chart = ChartFactory.createLineChart("PLOT", "Xi", "Yi", dataset, PlotOrientation.VERTICAL, false, true, true); BarRenderer renderer = null; CategoryPlot plot = null; //plot.setWeight(5); renderer = new BarRenderer(); //frame = new ChartFrame("Plot JFreeChart", chart); //frame.setVisible(true); //frame.setSize(600, 300); ChartPanel cp = new ChartPanel(chart); cp.setSize(panel.getWidth(), panel.getHeight()); cp.setVisible(true); panel.add(cp); panel.repaint(); } catch (SQLException ex) { //Logger.getLogger(plot.class.getName()).log(Level.SEVERE, null, ex); } } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { int test = (int) ((a*(Float.parseFloat(txttest.getText())))+ valfb); testres.setText(Float.toString(test)); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(regression.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(regression.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(regression.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(regression.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new regression().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel16; private javax.swing.JLabel jLabel17; private javax.swing.JLabel jLabel18; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JTable jTable1; private javax.swing.JLabel moyxi; private javax.swing.JLabel moyyi; private javax.swing.JPanel panel; private javax.swing.JLabel somxi; private javax.swing.JLabel somyi; private javax.swing.JLabel testres; private javax.swing.JTextPane txtarea; private javax.swing.JTextField txttest; private javax.swing.JLabel vala; private javax.swing.JLabel vala1; private javax.swing.JLabel valb; private javax.swing.JLabel valb2; private javax.swing.JTextField xi; private javax.swing.JTextField yi; // End of variables declaration } |
Comments
Post a Comment