Selasa, 23 Mei 2023

[FIX] Kalkulator Anti Mumet

 

package shkalkulator;
 
import java.awt.Color;
import javax.swing.*;
 
/**
     * @author Soni Hendarkanto PTI 4B
     * With Apache NetBeans IDE 16, Belsoft JAVA JDK 19
     * on Linux Mint 21.1 "VERA" Cinnamon Desktop Edition
     * 
     * My github for this project: https://github.com/Soni-Hendar-Kip/SHKalkulator
     * 
     * link tutor:
     * - original version from pak dosen rozi,
     * and
     * - https://www.tutorialsfield.com/simple-calculator-program-in-java-using-swing/
     * - https://www.duniaprogramming.com/2017/02/cara-membuat-program-kalkulator-keren.html
     */
public class NoxKalkulatorVersiDosenRozi extends javax.swing.JFrame {
    String TandaHitung=""; //untuk mewakili simbol operator hitung +-/*
    String LayarAwal; //untuk mewakili layar awal
    boolean FirstClick=false;
    double Angka_ke1 = 0; //untuk mewakili angka pertama yg akan di hitung pada kalkulator
    double Angka_ke2 = 0; //untuk mewakili angka kedua
    double Hasil_Hitung; //untuk mewakili hasil perhitungan
 
    public NoxKalkulatorVersiDosenRozi() {
        initComponents();
        NoxRadioBtOn_.setEnabled(false);
    }
 
    public void AturTextLayar(String k){
        LayarAwal = NoxLayar_.getText();
        if(LayarAwal.equals("0")) {
            LayarAwal=LayarAwal.replace("0","");
        }
        if(!TandaHitung.equals("") && FirstClick) {
            NoxLayar_.setText(k);
            FirstClick=false;
        }
        else{
            NoxLayar_.setText(LayarAwal+k);
        }
    }
 
    public void AturWarnaTombolAngka(boolean yes_or_no){
        setBackground(Color.yellow);
    }
 
    public void HilangkanWarnaTombol(boolean yes_or_no){
        NoxBt0_.setBackground(Color.LIGHT_GRAY);
        NoxBt1_.setBackground(Color.LIGHT_GRAY);
        NoxBt2_.setBackground(Color.LIGHT_GRAY);
        NoxBt3_.setBackground(Color.LIGHT_GRAY);
        NoxBt4_.setBackground(Color.LIGHT_GRAY);
        NoxBt5_.setBackground(Color.LIGHT_GRAY);
        NoxBt6_.setBackground(Color.LIGHT_GRAY);
        NoxBt7_.setBackground(Color.LIGHT_GRAY);
        NoxBt8_.setBackground(Color.LIGHT_GRAY);
        NoxBt9_.setBackground(Color.LIGHT_GRAY);
        NoxBtBagi_.setBackground(Color.magenta);
        NoxBtClear_.setBackground(Color.LIGHT_GRAY);
        NoxBtKali_.setBackground(Color.magenta);
        NoxBtKoma_.setBackground(Color.magenta);
        NoxBtKurang_.setBackground(Color.magenta);
        NoxBtPersen_.setBackground(Color.magenta);
        NoxBtSamaDengan_.setBackground(Color.blue);
        NoxBtTambah_.setBackground(Color.magenta);
        NoxBtDelete_.setBackground(Color.blue);
    }
 
    public void Modiarrr(){
        NoxBt1_.setEnabled(false);
        NoxBt2_.setEnabled(false);
        NoxBt3_.setEnabled(false);
        NoxBt4_.setEnabled(false);
        NoxBt5_.setEnabled(false);
        NoxBt6_.setEnabled(false);
        NoxBt7_.setEnabled(false);
        NoxBt8_.setEnabled(false);
        NoxBt9_.setEnabled(false);
        NoxBt0_.setEnabled(false);
        NoxBtAbout_.setEnabled(false);
        NoxBtAkarKuadrat_.setEnabled(false);
        NoxBtAkarKubik_.setEnabled(false);
        NoxBtBagi_.setEnabled(false);
        NoxBtClear_.setEnabled(false);
        NoxBtDelete_.setEnabled(false);
        NoxBtKali_.setEnabled(false);
        NoxBtKoma_.setEnabled(false);
        NoxBtKurang_.setEnabled(false);
        NoxBtPangkatKuadrat_.setEnabled(false);
        NoxBtPersen_.setEnabled(false);
        NoxBtSamaDengan_.setEnabled(false);
        NoxBtTambah_.setEnabled(false);
        NoxLayar_.setEnabled(false);
        NoxRadioBtOff_.setEnabled(false);
        NoxRadioBtOn_.setEnabled(true);
    }
 
    public void Muruppp(){
        NoxBt1_.setEnabled(true);
        NoxBt2_.setEnabled(true);
        NoxBt3_.setEnabled(true);
        NoxBt4_.setEnabled(true);
        NoxBt5_.setEnabled(true);
        NoxBt6_.setEnabled(true);
        NoxBt7_.setEnabled(true);
        NoxBt8_.setEnabled(true);
        NoxBt9_.setEnabled(true);
        NoxBt0_.setEnabled(true);
        NoxBtAbout_.setEnabled(true);
        NoxBtAkarKuadrat_.setEnabled(true);
        NoxBtAkarKubik_.setEnabled(true);
        NoxBtBagi_.setEnabled(true);
        NoxBtClear_.setEnabled(true);
        NoxBtDelete_.setEnabled(true);
        NoxBtKali_.setEnabled(true);
        NoxBtKoma_.setEnabled(true);
        NoxBtKurang_.setEnabled(true);
        NoxBtPangkatKuadrat_.setEnabled(true);
        NoxBtPersen_.setEnabled(true);
        NoxBtSamaDengan_.setEnabled(true);
        NoxBtTambah_.setEnabled(true);
        NoxLayar_.setEnabled(true);
        NoxRadioBtOff_.setEnabled(true);
        NoxRadioBtOn_.setEnabled(false);
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")                     
 
    private void NoxBt9_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("9");
            HilangkanWarnaTombol(true);
            NoxBt9_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt6_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("6");
            HilangkanWarnaTombol(true);
            NoxBt6_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt5_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("5");
            HilangkanWarnaTombol(true);
            NoxBt5_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt2_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("2");
            HilangkanWarnaTombol(true);
            NoxBt2_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt3_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:        
 
            AturTextLayar("3");
            HilangkanWarnaTombol(true);
            NoxBt3_.setBackground(Color.GREEN);
    }                                       
 
    private void NoxBtClear_ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
         NoxLayar_.setText("");
 
         HilangkanWarnaTombol(true);
         NoxBtClear_.setBackground(Color.blue);
    }                                           
 
    private void NoxBtTambah_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
            TandaHitung = "+";
            FirstClick = true;
            Angka_ke1=Double.parseDouble(NoxLayar_.getText());
 
            HilangkanWarnaTombol(true);
            NoxBtTambah_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxBtKurang_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
        TandaHitung = "-";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
            HilangkanWarnaTombol(true);
            NoxBtKurang_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxBtKali_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
 
        TandaHitung = "*";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
            HilangkanWarnaTombol(true);
            NoxBtKali_.setBackground(Color.YELLOW);
    }                                          
 
    private void NoxBtBagi_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
 
        TandaHitung = "/";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
            HilangkanWarnaTombol(true);
            NoxBtBagi_.setBackground(Color.YELLOW);
    }                                          
 
    private void NoxBtKoma_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        if(NoxLayar_.getText().contains(".")){
            return;
        }
        else{
            AturTextLayar("."); //koma dalam inggris menjadi . (titik)
        }      
 
        HilangkanWarnaTombol(true);
        NoxBtKoma_.setBackground(Color.YELLOW);
 
    }                                          
 
    private void NoxBtPersen_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
        Angka_ke2 = Double.parseDouble(NoxLayar_.getText());
        Hasil_Hitung = Angka_ke1*Angka_ke2/100;
        NoxLayar_.setText(Double.toString(Hasil_Hitung));
    }                                            
 
    private void NoxBtSamaDengan_ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        Hasil_Hitung = 0;
        Angka_ke2 = Double.parseDouble(NoxLayar_.getText());
        if (TandaHitung == "+"){
            Hasil_Hitung = Angka_ke1 + Angka_ke2 ;
        }
        else if (TandaHitung == "-"){
            Hasil_Hitung = Angka_ke1 - Angka_ke2 ;
        }
        else if (TandaHitung == "*"){
            Hasil_Hitung = Angka_ke1 * Angka_ke2 ;
        }
        else if (TandaHitung == "/"){
            Hasil_Hitung = Angka_ke1 / Angka_ke2 ;
        }
 
        String SHasil = Double.toString(Hasil_Hitung);
        NoxLayar_.setText(SHasil);
 
        HilangkanWarnaTombol(true);
        NoxBtSamaDengan_.setBackground(Color.RED);
    }                                                
 
    private void NoxBtAbout_ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(rootPane, "Matkul Pemrograman lanjut\n"
                + "Dosen: Pak Fahrur Rozi\n\n"
                + "Soni Hendarkanto\n"
                + "21183207051\n"
                + "PTI 4B\n\n"
                + "Bug:\n"
                + "- [SUDAH FIX] Tombol persen (%) belum berfungsi, googling belum ketemu tutor codingnya", "Tentang Kalkulator ini", HEIGHT);
    }                                           
 
    private void NoxBt7_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("7");
            HilangkanWarnaTombol(true);
            NoxBt7_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt8_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("8");
            HilangkanWarnaTombol(true);
            NoxBt8_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt4_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("4");
            HilangkanWarnaTombol(true);
            NoxBt4_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt1_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
            AturTextLayar("1");
            HilangkanWarnaTombol(true);
            NoxBt1_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt0_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
            AturTextLayar("0");
            HilangkanWarnaTombol(true);
            NoxBt0_.setBackground(Color.GREEN);
    }                                       
 
    private void NoxBtDelete_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        int PanjangAngka = NoxLayar_.getText().length();
        int DinasPajak = PanjangAngka - 1;
 
        if (PanjangAngka > 0){
            StringBuilder HapusKarakter = new StringBuilder(NoxLayar_.getText());
            HapusKarakter.deleteCharAt(DinasPajak);
            NoxLayar_.setText(HapusKarakter.toString());
        }              
 
        HilangkanWarnaTombol(true);
        NoxBtDelete_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxRadioBtOff_ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        Modiarrr();
        NoxLayar_.setText("");
    }                                              
 
    private void NoxBtAkarKuadrat_ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double AkarKuadrat = Math.sqrt(Angka_ke1);
        NoxLayar_.setText(Double.toString(AkarKuadrat));
    }                                                 
 
    private void NoxBtAkarKubik_ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // Akar Kubik adalah akar 3, misal 27 hasilnya 3
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double AkarKubik = Math.cbrt(Angka_ke1);
        NoxLayar_.setText(Double.toString(AkarKubik));
    }                                               
 
    private void NoxBtPangkatKuadrat_ActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        // Rumus pangkat kuadrat adalah Math.pow(angka, 2) artinya angka x angka;
        // bisa juga Math.pow(angka, 3) artinya angka x angka x angka;
        // Math.pow(7, 4) artinya 7x7x7x7;
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double PangkatKuadrat = Math.pow(Angka_ke1,2);
        NoxLayar_.setText(Double.toString(PangkatKuadrat));
    }                                                    
 
    private void NoxRadioBtOn_ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        Muruppp();
        NoxLayar_.setText("0");
    }                                             
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* 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(NoxKalkulatorVersiDosenRozi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NoxKalkulatorVersiDosenRozi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NoxKalkulatorVersiDosenRozi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NoxKalkulatorVersiDosenRozi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NoxKalkulatorVersiDosenRozi().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton NoxBt0_;
    private javax.swing.JButton NoxBt1_;
    private javax.swing.JButton NoxBt2_;
    private javax.swing.JButton NoxBt3_;
    private javax.swing.JButton NoxBt4_;
    private javax.swing.JButton NoxBt5_;
    private javax.swing.JButton NoxBt6_;
    private javax.swing.JButton NoxBt7_;
    private javax.swing.JButton NoxBt8_;
    private javax.swing.JButton NoxBt9_;
    private javax.swing.JButton NoxBtAbout_;
    private javax.swing.JButton NoxBtAkarKuadrat_;
    private javax.swing.JButton NoxBtAkarKubik_;
    private javax.swing.JButton NoxBtBagi_;
    private javax.swing.JButton NoxBtClear_;
    private javax.swing.JButton NoxBtDelete_;
    private javax.swing.JButton NoxBtKali_;
    private javax.swing.JButton NoxBtKoma_;
    private javax.swing.JButton NoxBtKurang_;
    private javax.swing.JButton NoxBtPangkatKuadrat_;
    private javax.swing.JButton NoxBtPersen_;
    private javax.swing.JButton NoxBtSamaDengan_;
    private javax.swing.JButton NoxBtTambah_;
    private javax.swing.JTextField NoxLayar_;
    private javax.swing.JRadioButton NoxRadioBtOff_;
    private javax.swing.JRadioButton NoxRadioBtOn_;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
} 
 

 

Tidak ada komentar:

Posting Komentar