Skróciłem trochę ten kod i zamiast JPanelu użyłem Canvasa, niestety efekt ten sam, dalej nic nie wyświetla:
Klasa główna (Container):
import javax.swing.*;
import java.awt.*;
public class Container extends JFrame implements Runnable {
/* Ustawienia okna */
private final String title = "Sim";
private final int width = 1280;
private final int height = 720; // hd res
/* Zawartość okna */
private Screen screen;
/* Ustawienia Runnable */
private boolean running;
private Thread thread;
private int frames;
private int updates;
public Container() {
/* Utworzenie okna */
setTitle(title);
setPreferredSize(new Dimension(width, height)); // -------- //
setMinimumSize(new Dimension(width, height)); // Rozmiary //
setMaximumSize(new Dimension(width, height)); // -------- //
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setLocationRelativeTo(null);
setResizable(false);
/* Dodanie zawartości do okna */
pack();
setVisible(true);
}
public synchronized void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
running = false;
System.exit(0);
}
public void init() {
screen = new Screen(width, height);
add(screen);
}
public void update() {
}
public void render() {
Graphics2D g = screen.getG();
g.setColor(Color.black);
g.fillRect(10, 10, 100, 100);
}
@Override
public void run() {
long x = System.nanoTime();
long y = System.currentTimeMillis();
double ns = 1000000000D / 60D;
double delta = 0; // Unprocessed
int updates = 0;
int frames = 0;
init();
while(running) {
long now = System.nanoTime();
delta += (now - x) / ns;
x = now;
boolean shouldRender = true;
while(delta >= 1) {
updates++;
update();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(1);
} catch(Exception e) {
e.printStackTrace();
}
if(shouldRender) {
frames++;
render();
//Pokaż fpsy
if(this.frames != 0) {
System.out.printf("%d FPS \n", this.frames);
}
}
if(System.currentTimeMillis() - y >= 1000) {
y += 1000;
this.frames = frames;
this.updates = updates;
frames = 0;
updates = 0;
}
}
}
public static void main(String[] args) {
new Container().start();
}
}
oraz klasa Screen:
import java.awt.*;
import java.awt.image.BufferedImage;
public class Screen extends Canvas {
private BufferedImage content;
private Graphics2D g;
public Screen(int width, int height) {
setSize(new Dimension(width, height));
content = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) content.getGraphics();
}
public Graphics2D getG() {
return g;
}
}