Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user

I'm trying to make a simple game loop but it's already failing package main;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;

public class Logic extends JPanel implements Runnable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// Main Game Stuff
private int gWidth = 800;
private int gHeight = 640;
private int scale = 2;

private int fps = 30;

private boolean running;

private Thread thread;
private BufferedImage image;
private Graphics2D g;

public Logic(){
    super();
    setPreferredSize(new Dimension(gWidth, gHeight));
    setFocusable(true);
    requestFocus();
}

public void addNotify(){
    if(thread==null)
    {
        thread=new Thread(this);
        thread.start();
    }
}

public void init()
{
    image = new BufferedImage(
            gWidth, gHeight, BufferedImage.TYPE_INT_RGB
        );
    g = (Graphics2D) image.getGraphics();
    System.out.println("Hello!");
    running = true;
}



private void update() //main logic
{
    //do stuff
}

private void render(Graphics2D g)
{
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, gWidth, gHeight);
}

private void PaintCanvas(){
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
}

public void run()
{
    init();
    
    int loops = 0;
    long startTime = System.nanoTime();
    float rate = 1000/fps;
    long lastTime = startTime;
    float waitTime = rate;
    
    
    while(running) // game loop
    {
         long currTime = System.nanoTime();
         if(currTime > (lastTime + waitTime))
         {
             waitTime = rate;
             if((currTime - lastTime) > rate)
                 waitTime = currTime - lastTime;
             
             
             //-- Paint Canvas --//
             PaintCanvas();
             
             //-- LOGIC -- //
             
                update();
             
             ////////////////
             //-- RENDER --//
             if(loops % 2 == 0)// draw every second frame
             {
                 render(g);
             }
             ////////////////
             loops++;
             
             lastTime = currTime;
         }
         
    }
}

}
package main;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;

public class Logic extends JPanel implements Runnable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// Main Game Stuff
private int gWidth = 800;
private int gHeight = 640;
private int scale = 2;

private int fps = 30;

private boolean running;

private Thread thread;
private BufferedImage image;
private Graphics2D g;

public Logic(){
    super();
    setPreferredSize(new Dimension(gWidth, gHeight));
    setFocusable(true);
    requestFocus();
}

public void addNotify(){
    if(thread==null)
    {
        thread=new Thread(this);
        thread.start();
    }
}

public void init()
{
    image = new BufferedImage(
            gWidth, gHeight, BufferedImage.TYPE_INT_RGB
        );
    g = (Graphics2D) image.getGraphics();
    System.out.println("Hello!");
    running = true;
}



private void update() //main logic
{
    //do stuff
}

private void render(Graphics2D g)
{
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, gWidth, gHeight);
}

private void PaintCanvas(){
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
}

public void run()
{
    init();
    
    int loops = 0;
    long startTime = System.nanoTime();
    float rate = 1000/fps;
    long lastTime = startTime;
    float waitTime = rate;
    
    
    while(running) // game loop
    {
         long currTime = System.nanoTime();
         if(currTime > (lastTime + waitTime))
         {
             waitTime = rate;
             if((currTime - lastTime) > rate)
                 waitTime = currTime - lastTime;
             
             
             //-- Paint Canvas --//
             PaintCanvas();
             
             //-- LOGIC -- //
             
                update();
             
             ////////////////
             //-- RENDER --//
             if(loops % 2 == 0)// draw every second frame
             {
                 render(g);
             }
             ////////////////
             loops++;
             
             lastTime = currTime;
         }
         
    }
}

}

I'm trying to make a simple game loop but it's already failing package main;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;

public class Logic extends JPanel implements Runnable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// Main Game Stuff
private int gWidth = 800;
private int gHeight = 640;
private int scale = 2;

private int fps = 30;

private boolean running;

private Thread thread;
private BufferedImage image;
private Graphics2D g;

public Logic(){
    super();
    setPreferredSize(new Dimension(gWidth, gHeight));
    setFocusable(true);
    requestFocus();
}

public void addNotify(){
    if(thread==null)
    {
        thread=new Thread(this);
        thread.start();
    }
}

public void init()
{
    image = new BufferedImage(
            gWidth, gHeight, BufferedImage.TYPE_INT_RGB
        );
    g = (Graphics2D) image.getGraphics();
    System.out.println("Hello!");
    running = true;
}



private void update() //main logic
{
    //do stuff
}

private void render(Graphics2D g)
{
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, gWidth, gHeight);
}

private void PaintCanvas(){
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
}

public void run()
{
    init();
    
    int loops = 0;
    long startTime = System.nanoTime();
    float rate = 1000/fps;
    long lastTime = startTime;
    float waitTime = rate;
    
    
    while(running) // game loop
    {
         long currTime = System.nanoTime();
         if(currTime > (lastTime + waitTime))
         {
             waitTime = rate;
             if((currTime - lastTime) > rate)
                 waitTime = currTime - lastTime;
             
             
             //-- Paint Canvas --//
             PaintCanvas();
             
             //-- LOGIC -- //
             
                update();
             
             ////////////////
             //-- RENDER --//
             if(loops % 2 == 0)// draw every second frame
             {
                 render(g);
             }
             ////////////////
             loops++;
             
             lastTime = currTime;
         }
         
    }
}

}

I'm trying to make a simple game loop but it's already failing

package main;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;

public class Logic extends JPanel implements Runnable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// Main Game Stuff
private int gWidth = 800;
private int gHeight = 640;
private int scale = 2;

private int fps = 30;

private boolean running;

private Thread thread;
private BufferedImage image;
private Graphics2D g;

public Logic(){
    super();
    setPreferredSize(new Dimension(gWidth, gHeight));
    setFocusable(true);
    requestFocus();
}

public void addNotify(){
    if(thread==null)
    {
        thread=new Thread(this);
        thread.start();
    }
}

public void init()
{
    image = new BufferedImage(
            gWidth, gHeight, BufferedImage.TYPE_INT_RGB
        );
    g = (Graphics2D) image.getGraphics();
    System.out.println("Hello!");
    running = true;
}



private void update() //main logic
{
    //do stuff
}

private void render(Graphics2D g)
{
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, gWidth, gHeight);
}

private void PaintCanvas(){
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
}

public void run()
{
    init();
    
    int loops = 0;
    long startTime = System.nanoTime();
    float rate = 1000/fps;
    long lastTime = startTime;
    float waitTime = rate;
    
    
    while(running) // game loop
    {
         long currTime = System.nanoTime();
         if(currTime > (lastTime + waitTime))
         {
             waitTime = rate;
             if((currTime - lastTime) > rate)
                 waitTime = currTime - lastTime;
             
             
             //-- Paint Canvas --//
             PaintCanvas();
             
             //-- LOGIC -- //
             
                update();
             
             ////////////////
             //-- RENDER --//
             if(loops % 2 == 0)// draw every second frame
             {
                 render(g);
             }
             ////////////////
             loops++;
             
             lastTime = currTime;
         }
         
    }
}

}
Source Link
Loading