Sei sulla pagina 1di 7

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
/**
*
* @author Dicky Noorfuadi Rahman
*/
public class traficlight extends javax.swing.JFrame {
private int awal = 1;
private final int lred = 1;
private final int lyellow = 2;
int angka, mulai;
int nilai;
private Timer timer;

int r, y;
/**
* Creates new form traficlight
*/
public traficlight() {
initComponents();
start();
}
public void start()
{
int speed = 1000;
r = lred;
y = lyellow;

mulai = awal;

ActionListener action;
action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
if ((angka%2==0) && (angka <= 100)) {
nkuning.setText(null);
nkuning.setEnabled(true);
nmerah.setEnabled(false);
nkuning.setText(String.valueOf(angka));
angka=angka+1;
r--;

}
else if ((angka%1==0) && (angka <=100)) {
nmerah.setText(null);
nmerah.setEnabled(true);
nkuning.setEnabled(false);
nmerah.setText(String.valueOf(angka));
angka=angka+1;
y--;

}
}
};
timer = new Timer(speed, action);
timer.start();
}

/**
* 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">
private void initComponents() {

nmerah = new javax.swing.JLabel();


nkuning = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

nmerah.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N


nmerah.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/gui/merah.png"))); // NOI18N

nkuning.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N


nkuning.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/gui/yellow.png"))); // NOI18N

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(122, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(nkuning, javax.swing.GroupLayout.PREFERRED_SIZE,
255, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nmerah, javax.swing.GroupLayout.PREFERRED_SIZE,
243, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(78, 78, 78))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(nmerah)
.addGap(30, 30, 30)
.addComponent(nkuning)
.addContainerGap(38, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @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(traficlight.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(traficlight.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(traficlight.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(traficlight.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new traficlight().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JLabel nkuning;
private javax.swing.JLabel nmerah;
// End of variables declaration
}

Potrebbero piacerti anche