Skip to content

Instantly share code, notes, and snippets.

@sugiartocokrowibowo
Created May 7, 2017 16:48
Show Gist options
  • Select an option

  • Save sugiartocokrowibowo/0bb8a29f2f2f97e713ed358bb6408bb7 to your computer and use it in GitHub Desktop.

Select an option

Save sugiartocokrowibowo/0bb8a29f2f2f97e713ed358bb6408bb7 to your computer and use it in GitHub Desktop.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame{
private JPanel jContentPane = null;
private Canvas canvas = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main thisClass = new Main();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}//end of main() method
public Main(){
super();
initialize();
}//end of constructor
private void initialize() {
this.setTitle("My Window");
this.setSize(800, 600);
this.setVisible(true);
this.setContentPane(getJContentPane());
}//end of initialize() method
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getCanvas(), BorderLayout.CENTER);
}
return jContentPane;
}
private Canvas getCanvas() {
if(canvas==null){
canvas = new Canvas();
}
return canvas;
}
class Canvas extends JComponent implements Runnable{
private Thread thread;
private volatile boolean running;
private final static int MAX_FPS = 60;
private final static int MAX_FRAME_SKIPS = 5;
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
public Canvas(){
resume();
}
public void resume() {
thread = new Thread(this);
running = true;
thread.start();
}
public void pause(){
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void exit(){
running = false;
System.exit(0);
}
@Override
public void run() {
long beginTime;
long timeDiff;
int sleepTime = 0;
int framesSkipped;
/*loop*/
while (running) {
try {
beginTime = System.currentTimeMillis();
framesSkipped = 0;
synchronized(this){
update();
repaint();
}
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
try {
thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
update();
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
}finally{}
}
}
private void update() {
//method untuk memperbaharui variabel
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
//tempat untuk menggambar di atas canvas
g2d.setColor(Color.decode("#3498db"));
int lebarKotak = 46;
for(int i=0;i<12;i++){
for(int j=0;j<17;j++){
int x = j*lebarKotak;
int y = i*lebarKotak;
g2d.fillRect(x+2, y+2, lebarKotak-2, lebarKotak-2);
}
}
g2d.dispose();
}
}//end of class Canvas
}//end of class Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment