Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main extends Canvas {
- //Static Variables
- public static Main main;
- public static String name = "Game";
- public static double version = 1.0;
- public static int FPS;
- //Object Variables
- private JFrame frame;
- private boolean running;
- private int screenX;
- private int screenY;
- private int x = 0;
- //Constructor
- public Main() {
- setSize(new Dimension(500, 500));
- }
- //Main Method
- public static void main(String[] args) {
- main = new Main();
- main.init();
- }
- //Object Methods
- private void init() {
- frame = new JFrame(name);
- frame.setSize(500, 500);
- frame.setResizable(false);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(main);
- loop();
- }
- public void loop() {
- running = true;
- int fps = 0;
- long timer = System.currentTimeMillis();
- long lastTime = System.nanoTime();
- final double ns = 1000000000.0 / 60;
- double delta = 0;
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- if (fps <= 60) {
- fps++;
- update();
- frame.repaint();
- delta--;
- }
- }
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- log("Running at " + fps + " FPS and UPS");
- FPS = fps;
- fps = 0;
- }
- }
- }
- public void update() {
- screenX = frame.getWidth();
- screenY = frame.getHeight();
- x++;
- if (x >= 500) x = 0;
- log("update");
- //update gametstate
- }
- public void log(String string) {
- System.out.println("[" + name + "] [" + version + "] " + string);
- }
- public void log() {
- System.out.println("[" + name + "] [" + version + "]");
- }
- @Override
- public void paint(Graphics graphics) {
- graphics.setColor(Color.WHITE);
- graphics.fillRect(0, 0, screenX, screenY);
- //update gamestate
- graphics.setColor(Color.BLUE);
- graphics.fillRect(0, 200, x, 300);
- log("rendered");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement