Ads 468x60px

Rabu, 15 Agustus 2012

Contoh Program Java Lampu Lalu Lintas

Artikel kali ini yaitu contoh program tentang java Lampu Lalu Lintas...
Berikut Listing Programnya.
Simpan dengan MainFrame.java.  


package Latihan.Blog;
import java.awt.Color;
import java.awt.Cursor;

public class MainFrame extends javax.swing.JFrame {
int a =2;   //pesan variable a
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents(); //memasang atau mengompile objek-objek
    }
//bagian ini tidak dapat di ubah
    /** 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pMerah = new javax.swing.JPanel();
        pHijau = new javax.swing.JPanel();
        pKuning = new javax.swing.JPanel();
        rbStop = new javax.swing.JRadioButton();
        rbHati2 = new javax.swing.JRadioButton();
        rbGo = new javax.swing.JRadioButton();
        bGanti = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 255, 255));
        jPanel1.setForeground(new java.awt.Color(255, 153, 204));
        jPanel1.setFont(new java.awt.Font("Ravie", 1, 24)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Ravie", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 14, 237));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("LAMPU LALU LINTAS");

        pMerah.setBackground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout pMerahLayout = new javax.swing.GroupLayout(pMerah);
        pMerah.setLayout(pMerahLayout);
        pMerahLayout.setHorizontalGroup(
            pMerahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 59, Short.MAX_VALUE)
        );
        pMerahLayout.setVerticalGroup(
            pMerahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 57, Short.MAX_VALUE)
        );

        pHijau.setBackground(new java.awt.Color(0, 255, 0));

        javax.swing.GroupLayout pHijauLayout = new javax.swing.GroupLayout(pHijau);
        pHijau.setLayout(pHijauLayout);
        pHijauLayout.setHorizontalGroup(
            pHijauLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 59, Short.MAX_VALUE)
        );
        pHijauLayout.setVerticalGroup(
            pHijauLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 57, Short.MAX_VALUE)
        );

        pKuning.setBackground(new java.awt.Color(255, 255, 0));

        javax.swing.GroupLayout pKuningLayout = new javax.swing.GroupLayout(pKuning);
        pKuning.setLayout(pKuningLayout);
        pKuningLayout.setHorizontalGroup(
            pKuningLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 59, Short.MAX_VALUE)
        );
        pKuningLayout.setVerticalGroup(
            pKuningLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 57, Short.MAX_VALUE)
        );

        rbStop.setBackground(new java.awt.Color(212, 208, 200));
        rbStop.setFont(new java.awt.Font("Curlz MT", 1, 14)); // NOI18N
        rbStop.setText("BERHENTI");
        rbStop.setEnabled(false);

        rbHati2.setBackground(new java.awt.Color(212, 208, 200));
        rbHati2.setFont(new java.awt.Font("Curlz MT", 1, 14)); // NOI18N
        rbHati2.setText("HATI-HATI");
        rbHati2.setEnabled(false);

        rbGo.setBackground(new java.awt.Color(212, 208, 200));
        rbGo.setFont(new java.awt.Font("Curlz MT", 1, 14)); // NOI18N
        rbGo.setText("JALAN");
        rbGo.setEnabled(false);

        bGanti.setFont(new java.awt.Font("Algerian", 1, 24)); // NOI18N
        bGanti.setText("GANTI");
        bGanti.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                bGantiMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                bGantiMouseExited(evt);
            }
        });
        bGanti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGantiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pKuning, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pMerah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pHijau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(rbHati2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(rbStop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(rbGo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(82, 82, 82))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(bGanti, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(68, 68, 68))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pMerah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(pKuning, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rbStop)
                        .addGap(27, 27, 27)
                        .addComponent(rbHati2)
                        .addGap(18, 18, 18)
                        .addComponent(rbGo)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pHijau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bGanti))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        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();
    }// </editor-fold>//GEN-END:initComponents
//yang terjadi ketika tombol ubah ditekan

private void bGantiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGantiActionPerformed
// TODO add your handling code here:
     a++;     //nilai a terus bertambah satu setiap di tekan
//if conditional, bagian ini mengatur warna lampu dan radio button       

        if(a%3 == 0){
                     pMerah.setBackground(Color.GRAY);      //set warna
                     pHijau.setBackground(Color.GREEN);
                     rbStop.setSelected(false);     //seleksi yang tidak aktif
                     rbGo.setSelected(true);       //seleksi yang aktif
                               }

         else if(a%3 == 1){
                     pHijau.setBackground(Color.GRAY);
                     pKuning.setBackground(Color.ORANGE);
                     rbGo.setSelected(false);
                     rbHati2.setSelected(true);
                 }

          else {
                     pKuning.setBackground(Color.GRAY);
                     pMerah.setBackground(Color.RED);
                     rbHati2.setSelected(false);
                     rbStop.setSelected(true);
       }

}//GEN-LAST:event_bGantiActionPerformed

private void bGantiMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bGantiMouseEntered
// TODO add your handling code here:
    setCursor(Cursor.HAND_CURSOR);     //saat kursor masuk area tombol, kursor berubah bentuk
}//GEN-LAST:event_bGantiMouseEntered

private void bGantiMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bGantiMouseExited
// TODO add your handling code here:
    setCursor(Cursor.DEFAULT_CURSOR);   //saat kursor kelua area tombol, kursor kembali seperti semula
}//GEN-LAST:event_bGantiMouseExited

    /**
     * @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bGanti;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pHijau;
    private javax.swing.JPanel pKuning;
    private javax.swing.JPanel pMerah;
    private javax.swing.JRadioButton rbGo;
    private javax.swing.JRadioButton rbHati2;
    private javax.swing.JRadioButton rbStop;
    // End of variables declaration//GEN-END:variables
}

Output:


Klo mau download Program Netbeansnya sillahkan klik disini "Download Program Netbeans"

0 Komentar: