Membuat Program Animasi Matrix dengan Java Swing

Pada posting sebelumnya saya memberikan tutorial menampilkan animasi matrix di terminal Linux. Kali ini saya share source code membuat animasi matrix tersebut dengan menggunakan Java. Animasi komputer matrix tersebut mirip dengan tampilan komputer yang ada dalam film The Matrix.

Berikut ini source code animasi matrixnya

import java.awt.*;
import java.util.*;
import javax.swing.*;

// Original code by : Ricco
// Modified by : Resa CR
@SuppressWarnings("serial")
public class animasiMatrix extends JFrame { 

private static final int FONT_SIZE = 20;
private static final int NUMBER_OF_REPEATS = 5;
private static final String TEXT = new String("# % 1 2 3  $*()14578  (^%#%^&*123   $%-++=728276");
private static JPanel panel = new JPanel(null);
private static Random random = new Random();
private static JLabel label[] = new JLabel[NUMBER_OF_REPEATS];

public animasiMatrix() {        
   this.add(panel);
   panel.setBackground(Color.BLACK);
}
public void scroll() {
    //array to hold x coordinates for the labels
    int[] random_x = new int[NUMBER_OF_REPEATS];
    //create an infinite loop
    while (true) {
        //initialise all the labels to random characters
        for (int i = 0; i < NUMBER_OF_REPEATS; i++) {
          int character_initial = random.nextInt(TEXT.length());
          random_x[i] = random.nextInt(panel.getWidth() / FONT_SIZE) - 1;
          label[i] = new JLabel("" + TEXT.charAt(character_initial));
          panel.add(label[i]);
          label[i].setFont(new Font("Arial Black", Font.PLAIN, FONT_SIZE));
        label[i].setForeground(new Color(0, 255, 0));
     }
    // change the text of the labels and their position
    for (int j = 0; j < (panel.getHeight() / FONT_SIZE) * 2; j++) {
        int character = random.nextInt(TEXT.length());
        //move each character
        for (int i = 0; i < NUMBER_OF_REPEATS; i++) {
            label[i].setBounds(random_x[i] * FONT_SIZE, j * (FONT_SIZE / 2), FONT_SIZE, FONT_SIZE);
            label[i].setText("" + TEXT.charAt(character));
            label[i].setForeground(new Color(0, 255 - (j * 5), 0));     
            for (int k = 0; k < NUMBER_OF_REPEATS; k++) {
                int character_initial = random.nextInt(TEXT.length());
                random_x[k] = random.nextInt(panel.getWidth() / FONT_SIZE) - 1;
                label[k] = new JLabel("" + TEXT.charAt(character_initial));
                panel.add(label[k]);
                label[k].setFont(new Font("Comic Sans", Font.PLAIN, FONT_SIZE));
                label[k].setForeground(new Color(0, 255, 0));
                Color colour = label[k].getForeground();
                if (colour.getGreen() <= 80) {
                    panel.remove(label[k]);
                    k = (panel.getHeight() / FONT_SIZE) * 2;
                }
            }
        }
        // pause between each character
        try {
            Thread.sleep(15);
        } catch (Exception e) {
        }
     }
  }
}
  public static void main(String[] args) {
      animasiMatrix frame = new animasiMatrix();
      frame.setVisible(true);
      frame.setSize(600, 400);
      frame.setResizable(false);
      frame.setMinimumSize(new Dimension(300, 200));
      frame.setLocationRelativeTo(null);
      frame.setTitle("Animasi Matrix Java");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
      frame.scroll();
  }
}

Untuk menjalankan codenya anda dapat menggunakan Geany IDE, Eclipse IDE, Netbeans IDE, dsb.

Seperti ini tampilan programnya

Screenshot

Selamat mencoba dan semoga bermanfaat🙂

Untuk pertanyaan, saran dan kritik silahkan ditulis di kolom komentar.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s