Created
October 9, 2017 12:07
-
-
Save sugiartocokrowibowo/cbc486a0d26e62b1f876c3da546244e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package latihan1; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.EventQueue; | |
import java.awt.Font; | |
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 | |
int x=0; | |
public Canvas() { | |
initializeCanvas(); | |
} | |
private void loadImage(){ | |
//LOAD IMAGE DISINI | |
} | |
private void initializeCanvas(){ | |
setBackground(Color.WHITE); | |
setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); | |
setDoubleBuffered(true); | |
} | |
//@Override | |
public void addNotify() { | |
super.addNotify(); | |
resume(); | |
} | |
private void updateState() { | |
//UPDATE DISINI | |
x++; | |
} | |
//@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 | |
//g2d.setFont(new Font("Neuropol X Rg", Font.PLAIN, 50)); | |
g2d.setFont(new Font("Arial", Font.PLAIN, 50)); | |
g2d.drawString("X: "+x, 100, 100); | |
} | |
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