Skip to content

Instantly share code, notes, and snippets.

@sugiartocokrowibowo
Created December 20, 2017 14:20
Show Gist options
  • Save sugiartocokrowibowo/35e7801b6bb055e16f342820bfa3e0d1 to your computer and use it in GitHub Desktop.
Save sugiartocokrowibowo/35e7801b6bb055e16f342820bfa3e0d1 to your computer and use it in GitHub Desktop.
package latihan0;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame{
public Main(){
initUI();
}
private void initUI(){
add(new Canvas());
setResizable(false);
pack();
setTitle("Computer Graphics Unsulbar");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
//@Override
public void run() {
JFrame ex = new Main();
ex.setVisible(true);
}
});
}
class Canvas extends JPanel implements Runnable {
private final int CANVAS_WIDTH = 600;
private final int CANVAS_HEIGHT = 600;
private final static int MAX_FPS = 50; //desired fps
private final static int FRAME_PERIOD = 1000 / MAX_FPS; // the frame period
private volatile boolean isRunning = false;
private Thread thread;
//DEKLARASI VARIABEL/ATRIBUT DISINI
public Canvas() {
initializeCanvas();
}
private void loadImage(){
//LOAD IMAGE DISINI
}
private void initializeCanvas(){
setBackground(Color.WHITE);
setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
setDoubleBuffered(true);
loadImage();
}
//@Override
public void addNotify() {
super.addNotify();
resume();
}
private void updateState() {
//UPDATE DISINI
}
//@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
render(g2d);
g2d.dispose();
Toolkit.getDefaultToolkit().sync();
}
private void render(Graphics2D g2d){
//RENDER DISINI
}
public void resume() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
public void pause() {
isRunning = false;
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
//@Override
public void run(){
while(isRunning){
long started = System.currentTimeMillis();
updateState();
repaint();
float deltaTime = (System.currentTimeMillis() - started);
int sleepTime = (int) (FRAME_PERIOD - deltaTime);
if (sleepTime > 0) {
try {
thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.out.println("Interrupted: " + e.getMessage());
}
}
while (sleepTime < 0) {
updateState();
sleepTime += FRAME_PERIOD;
}
}//end of while(isRunning)
}//end of run()
}//end of Canvas class
}//end of Main class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment