Log in

View Full Version : [ASK] Tentang JAVA(J2SE): Terjadi Flicker. Tolong dong


Guesuper
20th November 2011, 09:28 AM
kenapa ya kok jadinya flicker di layar?



Nih listingnya gan. Pendek kok. Cuman 81 baris.

Tujuanku mau buat game adventure 2D. Cuman aku harus tau konsep2 dasar di java. Jadi aku latihan mini2 gini.

CELAKANYA, mini2 kayak gini ajah udah error. Apalagi nanti kalo udah kompleks. Tolong banget ya gan...




Code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author MWC
*/
public class MayBeBounceTest implements KeyListener{
//Final Property
public final int WIDTH=800;
public final int HEIGHT=600;

public JFrame frame;
public Graphics g;
public boolean isRunning;

public static void main(String[] args){
new MayBeBounceTest();
}

public MayBeBounceTest(){
frame=new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
frame.addKeyListener(this);
frame.createBufferStrategy(2);


isRunning=true;
while(isRunning){
g=frame.getGraphics();
draw(g);
g.dispose();
update();
}

System.exit(0);
}

public void draw(Graphics g){
//Background
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);

//Draw Oval
g.setColor(Color.WHITE);
g.drawOval(100, 100, 100, 100);
}

public void update(){
BufferStrategy strategy=frame.getBufferStrategy();
if(!strategy.contentsLost())strategy.show();
}

public void keyTyped(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}

public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_ESCAPE==e.getKeyCode()){
isRunning=false;
}
}

public void keyReleased(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}



</div>