Full-screen Exclusive API: Menggambar Satu Layar Penuh


Tidak semua program puas menggambar di komponen Swing. Passive rendering yang memakai method paint(Graphics g) tidak sesuai untuk program grafis yang harus memiliki performance tinggi. Contohnya adalah program game, yang umumnya memakai full-screen active rendering. Umumnya, program seperti game memakai bantuan API DirectX atau OpenGL. Java juga mendukung penggambaran full-screen dengan double-buffering berupa page-flipping. Tapi karena Java dirancang untuk multi-platform, tidak seluruh platform mendukung fitur ini. Aku dapat memeriksanya dengan kode seperti berikut:

GraphicsEnvironment env = GraphicsEnvironment.
  getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
  System.out.println("Fullscreen API is supported.");
} else {
  System.out.println("Fullscreen API is not supported.");
}
if (gd.isDisplayChangeSupported()) {
  System.out.println("Display resolution change is supported.");
} else {
  System.out.println("Display resolution change is not supported.");
}

DisplayMode dm = gd.getDisplayMode();
System.out.format("Display resolution %d x %d (%d bit, refresh rate %d Hz)",
  dm.getWidth(), dm.getHeight(), dm.getBitDepth(), dm.getRefreshRate());

Sekarang aku akan latihan membuat sebuah program yang menggambar full screen sekumpulan lingkaran dengan posisi dan warna yang acak, seperti berikut:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;

public class Main extends JFrame {
  
  private static final long serialVersionUID = 4648172894076113183L;
  
  public Main() {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
  
    setUndecorated(false);
    setResizable(false);
    setIgnoreRepaint(true);
    device.setFullScreenWindow(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    createBufferStrategy(2);
    BufferStrategy buffer = getBufferStrategy();
    
    Graphics g;
    List<LingkaranWarna> lstLingkaran = new ArrayList<LingkaranWarna>();
    while (true) {
      if (lstLingkaran.size()<100) {
        lstLingkaran.add(new LingkaranWarna());
      } else {
        lstLingkaran.clear();
      }
      g = buffer.getDrawGraphics();
      g.clearRect(0,0, getWidth(), getHeight());
      for (LingkaranWarna b : lstLingkaran) {
        b.gambar(g);
      }
      g.dispose();
      buffer.show();
      
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } 
    }
    
  }

  protected class LingkaranWarna {

    private int lokasiX;
    private int lokasiY;
    private Color warna;
    private int sizeX;
    private int sizeY;
    
    public LingkaranWarna() {
      atributBaru();
    }
    
    private void atributBaru() {
      Random rand = new Random();
      lokasiX = rand.nextInt(1000);
      lokasiY = rand.nextInt(600);
      warna = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
      sizeX = rand.nextInt(100);
      sizeY = rand.nextInt(100);
    }
    
    private void gambar(Graphics g) {
      g.setColor(warna);
      g.fillOval(lokasiX, lokasiY, sizeX, sizeY);
      atributBaru();
    }
  }
  public static void main(String[] args) {
    new Main();
  }

}

Perihal Solid Snake
I'm nothing...

Apa komentar Anda?

Please log in using one of these methods to post your comment:

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

%d blogger menyukai ini: