Pages

Selasa, 11 September 2012

Program Animasi JavaScript Bola Bertebaran

Postingan kali ini tentang JavaScript "Bola Bertebaran".
Dimana lisitng program ini disimpan dalam bentuk formati html.
Berikut Listingnya.!!



<canvas height="400" id="semburan" width="650"></canvas>
<script type="text/javascript">
//Membuat variable dan mulai menentukan jari-jari bola
var line = {
x: 400,
y: 0,
length: 100,
angle: 0
};
//Memfungsikan dengan this yang nantinya akan di panggil di translate.
function Ball(x, y, r) {
this.x = 300;
this.y = 400;
this.r = 25;
this.vx = 300;
this.vy = 300;
Ball.all.push(this);
}
//Menggambar seluruh bola dan menetapkan panjang.
Ball.all = [];
Ball.draw_all = function() {
var i = Ball.all.length;
while (i--) {
Ball.all[i].draw();
}
};
//mulai memanggil this yang sudah di berikan, Seperti terlihat di ctx.translate.
Ball.prototype = {
draw: function() {
ctx.save();
ctx.translate(this.x, this.y, this.vx, this.vy, this.vx);
ctx.fillStyle = "red";
ctx.beginPath();
//Menggambar lingkaran dengan metode arcs.
ctx.arc(0, 0, this.r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
ctx.restore();
},
//memindahkan function lalu mengindex seluruh bola.
remove: function() {
Ball.all.splice(Ball.all.indexOf(this), 1);
}
};
//menentukan id yang akan di panggil dalam bidang canvas HTML5
var canvas = document.getElementById("semburan");
var ctx = canvas.getContext('2d');
setInterval(function() {
ctx.save();
ctx.fillStyle = "#f5f5f5";
ctx.fillRect(0, 0,800,400);
ctx.restore();
//Mulai menganimasi berapa banyak bola dan penentuan coordinate
line.angle += (Math.PI * 2) / 20;
var x = line.x + line.length * Math.cos(line.angle);
var y = line.y + line.length * Math.cos(line.angle);
if (Ball.all.length < 100) {
for (var i = 0; i < 5; i++) {
var ball = new Ball(x, y, 2);
var random_offset = Math.random() * 1 - .5;
var speed = Math.random() * 15 + 3;
ball.vx = speed * Math.cos(line.angle + random_offset);
ball.vy = speed * Math.sin(line.angle + random_offset);
}
}
var i = Ball.all.length;
while(i--) {
var ball = Ball.all[i];
ball.x += ball.vx;
ball.y += ball.vy;
ball.vy += .2;
ball.vx *= 1;
ball.vy *= .99;
if (ball.x % 800 !== ball.x) {
ball.remove();
}
else if (ball.y >= 130) {
ball.vy = -Math.abs(ball.vy);
ball.vy *= 1;
if (Math.abs(ball.vy) < 1 && Math.abs(ball.vx) < 1) {
ball.remove();
}
}
}
//Melakukan metode save dan restore, perlu di perhatikan di sini yaitu moveto
//lineto yang memberikan nilai line.x. line.y sobat bisa memodifikasinya sendiri
ctx.save();
ctx.beginPath();
ctx.moveTo(line.x, line.y);
ctx.lineTo(x, y);
ctx.fill();
ctx.restore();
Ball.draw_all();
}, 1000 / 77);
</script>

Hasilnya Nanti i gambar diatas, Tapi Dia dalam bentuk animasi.



Artikel Lain:

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"


Artikel Lain :