Kamis, 20 Maret 2014

Tulisan Berjalan Horizontal dan Vertikal berbasis Gui dengan Java (Thread)

hapir sama dengan tulisan sebelumnya. kali ini tulisan berjalan secara horizontal dan vertikal. design formnya kaya gini
source Codenya:
package sister3;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jazilah
 */
public class no2 extends javax.swing.JFrame implements Runnable {

    boolean horz = false;
    int x, y;
    boolean kanankiri = true;
    boolean atasbawah = true;
    boolean arah;
public no2() {
        initComponents();
    }
private void berhentiActionPerformed(java.awt.event.ActionEvent evt) {                                        
        horz = true;
    }                                       

    private void lanjutActionPerformed(java.awt.event.ActionEvent evt) {                                      
        new Thread(this).start();
        horz = false;
    }                                     

    private void vertikalActionPerformed(java.awt.event.ActionEvent evt) {                                        

        arah = false;
    }      
 private void horizontalActionPerformed(java.awt.event.ActionEvent evt) {                                          
arah = true;
    }  
  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(no2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(no2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(no2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(no2.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 no2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton berhenti;
    private javax.swing.JButton horizontal;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton lanjut;
    private javax.swing.JButton vertikal;
    // End of variables declaration                  

    public void run() {
        while (horz == false) {
            if (arah == true) {

                if (kanankiri == true) {
                    x++;
                    if (x == 200) {
                        kanankiri = false;
                    }

                } else {
                    x--;
                    if (x == 10) {
                        kanankiri = true;
                    }
                }


            } else {
                if (atasbawah == true) {
                    y++;
                    if (y == 200) {
                        atasbawah = false;
                    }

                } else {
                    y--;
                    if (y == 0) {
                        atasbawah = true;
                    }
                }

            }
            jLabel2.setBounds(x, y, 400, 40);
            System.out.println("" + x);
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                Logger.getLogger(Tugas1.class.getName()).log(Level.SEVERE, null, ex);
            }



        }

    }
}
source code lengkapnya bisa di download di sini

Tidak ada komentar:

Posting Komentar