java

Belajar Java Sangatlah Menyenangkan Ayoooo Belajar Java . . . . . .

Senin, 16 September 2013

P/ JFrame - membuat gambar berjalan memakai warna ke arah kiri.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
public class JFrameLatihan6 extends javax.swing.JFrame {
private static int waktu = 100;
  Image gambar;
  Dimension ukuran;
  Insets insets;
  Color colors[] = { Color.RED,Color.GREEN,
  Color.BLUE};
    /** Creates new form JFrameLatihan6 */
    public JFrameLatihan6() {
        initComponents();
    }
public void paint(Graphics g) {
    if ((ukuran == null) || (ukuran != getSize())) {
      ukuran = getSize();
      gambar = new BufferedImage(getWidth(), getHeight(),
          BufferedImage.TYPE_INT_RGB);
    }
    if (insets == null) {
      insets = getInsets();
    }
    int x = insets.left;
    int y = insets.top;
    int width = getWidth() - insets.left - insets.right;
    int height = getHeight() - insets.top - insets.bottom;
    int start = 0;
    int steps = colors.length;
    int stepSize = 360 / steps;
    synchronized (colors) {
      Graphics tampil_gambar = gambar.getGraphics();
      tampil_gambar.setColor(Color.WHITE);
      tampil_gambar.fillRect(x, y, width, height);
      for (int i = 0; i < steps; i++) {
        tampil_gambar.setColor(colors[i]);
        tampil_gambar.fillArc(x, y, width, height, start, stepSize);
        start += stepSize;
      }
    }
    g.drawImage(gambar, 0, 0, this);
  }

  public void jalan() {
    TimerTask task = new TimerTask() {
      public void run() {
        Color c = colors[0];
        synchronized (colors) {
          System.arraycopy(colors, 1, colors, 0, colors.length - 1);
          colors[colors.length - 1] = c;
        }
        repaint();
      }
    };
    Timer timer = new Timer();
    timer.schedule(task, 0, waktu);
  }

 public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrameLatihan6 tampil =new JFrameLatihan6();
                tampil.setVisible(true);
                tampil.jalan();
            }
        });
    }


Tidak ada komentar: