I have been using Graphics.drawImage for a very long time to render images, but after I installed java 8, it stopped working. There are no errors. I don't understand what's going on.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game implements Runnable {
private BufferedImage img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
private boolean running;
private Thread thread;
private JFrame f;
private Canvas c;
public Game() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (GraphicsDevice gd : gs) {
GraphicsConfiguration[] gc = gd.getConfigurations();
for (GraphicsConfiguration gc1 : gc) {
f = new JFrame("Title", gd.getDefaultConfiguration());
c = new Canvas(gc1);
f.getContentPane().add(c);
f.setUndecorated(false);
f.setIgnoreRepaint(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700, 600);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setFocusable(true);
f.setVisible(true);
}
}
}
public void init() {
img.getGraphics().drawImage(img, 0, 0, Color.BLACK, null);
}
#Override
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
frames = 0;
ticks = 0;
}
}
}
public synchronized void start() {
running = true;
thread = new Thread(this, "GAME");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
new Game().start();
}
public void tick() { /*player movement, ect*/ }
public void render() {
c.createBufferStrategy(3);
BufferStrategy bs = c.getBufferStrategy();
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 100, 100, null);
//I should also not that strings won't draw either.
g.drawString("Hello, world", 50, 50);
g.dispose();
bs.show();
}
}
I don't understand why this wouldn't be working, because it was working fine in java 7, and I did not make any changes to my code before or after I installed java 8.
Also I am using a MacBook pro if that helps.
On Mac OS X version 10.9.4, I can verify that createBufferStrategy(2) works under Java 8, while createBufferStrategy(3) fails unless I revert to Java 7. As an aside, note that Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
As tested:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.awt.EventQueue;
public class Game implements Runnable {
private BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
private boolean running;
private Thread thread;
private JFrame f;
private Canvas c;
public Game() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (GraphicsDevice gd : gs) {
GraphicsConfiguration[] gc = gd.getConfigurations();
for (GraphicsConfiguration gc1 : gc) {
f = new JFrame("Title", gd.getDefaultConfiguration());
c = new Canvas(gc1);
f.add(c);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(360, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
}
public void init() {
img.getGraphics().drawImage(img, 0, 0, Color.BLUE, null);
}
#Override
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
frames = 0;
ticks = 0;
}
}
}
public synchronized void start() {
running = true;
thread = new Thread(this, "GAME");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Game().start();
}
});
}
public void tick() { /*player movement, ect*/ }
public void render() {
c.createBufferStrategy(2);
BufferStrategy bs = c.getBufferStrategy();
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 100, 100, null);
g.drawString("Hello, world: " + System.currentTimeMillis(), 50, 50);
g.dispose();
bs.show();
}
}
Related
I have a sprite sheet that I want to use in a src folder within the project. Here is some code I have for a simple game I am making. I get an error when trying to draw anything with the paintbrush or (g). Can you help me figure out what is wrong?
I have a pre-existing background that I want to draw the images over. I deleted the code, the background image isn't what's causing the issues.
package NewCards;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Game extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private boolean live = false; //running
private boolean dead = false;
//instances of external classes here.
private Thread thread;
/**
* Launch the application.
*/
//initialize here.
public void init() {
}
private synchronized void start() {
if (running)
return;
live = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop() {
if(!running)
return;
try {
thread.join(); //attempts to join threads, if unsuccessful, throw Exception error.
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
public void run() {
init();
long lasttime = System.nanoTime();
final double amountofticks = 60.0;
double ns = 1000000000 / amountofticks;
double delta = 0;
int update = 0;
int frames = 0;
long timer = System.currentTimeMillis();
// TODO Auto-generated method stub
while (running) {
long now = System.nanoTime();
delta += (now - lasttime) / ns;
lasttime = now;
if (delta >= 1) {
tick();
update++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(update + " Frames, FPS: " + frames);
update = 0; // resets fps for updates. If not the beats would just double.
frames = 0; // statement above applies here in terms of FPS
}
}
stop();
}
private void tick(){
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs== null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.fillRect(1, 1, 200, 500);
g.dispose();
bs.show();
}
public static void main(String[] args) {
final Game game = new Game(); //game starter.
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
JLabel backgroundImage = new JLabel();
backgroundImage.setSize(800, 800);
frame.getContentPane().add(backgroundImage);
Image background = new ImageIcon(this.getClass().getResource("/Artificial intelligence.png")).getImage();
backgroundImage.setIcon(new ImageIcon(background));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
game.start();
}
});
}
/**
* Create the frame.
*/
public Game() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 800);
setTitle("MyEmpire-1.1.1/Windows/Display/Menus/Loop");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
}
I have designed a code for a game. The problem is that the background won't change to any of my selected colors I have picked from graphics color library.
I need someone to figure this out with the code i have provided (please don't make a whole new code). idk why java/ eclipse won't display it??? am i missing something?? The program here should display a GUI with a background color blue. instead i get white.
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
private Thread thread;
private boolean running= false;
public MainApp()
{
new Window(WIDTH, HEIGHT, "App", this);
}
public synchronized void start()
{
thread= new Thread(this);
thread.start();
running= true;
}
public void run()
{
long lastTime= System.nanoTime();
double amountOfTicks= 60.0;
double ns= 1000000000 / amountOfTicks;
double delta= 0;
long timer= System.currentTimeMillis();
int frames= 0;
while(running){
long now= System.nanoTime();
delta += (now- lastTime) / ns;
lastTime= now;
while(delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.print("FPS: " + frames);
frames= 0;
}
}
stop();
}
public synchronized void stop()
{
try
{
thread.join();
running= false;
}catch(Exception e){e.printStackTrace();}
}
public void tick()
{
}
public void render()
{
BufferStrategy bs= this.getBufferStrategy();
if(bs== null)
{
this.createBufferStrategy(3);
return;
}
Graphics g= bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
}
Your codes a little messed up, you shouldn't be making a new instance of Window from MainApp, the Window should be creating it (IMHO).
Also, you should be overriding the getPreferredSize method the the MainApp, as this is what should be controlling the viewable size of the window, this way, when you use pack on the JFrame, it will ensure that the window is larger then the preferredSize of it's contents, allowing the frame decorations to wrap around the outside of it.
BUT, the main problem you have, is adding the MainApp to the JFrame AFTER it's already been made visible
The following works for me...
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH = 648, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public MainApp() {
new Window("App", this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running) {
render();
}
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.print("FPS: " + frames);
frames = 0;
}
}
stop();
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
public static class Window {
private Window(String title, MainApp app) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(app);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
app.start();
}
}
}
This is my Game Class:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel implements Runnable {
Thread thread;
private boolean running = true;
private double FPS = 1.0/60.0;
private Level level;
public Game(){
level = new LevelOne();
level.setBackground(Color.BLACK);
add(level);
}
public synchronized void start() {
thread = new Thread(this, "Game");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void run() {
double firstTime = 0;
double lastTime = System.nanoTime() / 1000000000.0;
double passedTime = 0;
double updateTime = 0;
double timer = System.nanoTime() / 1000000000.0;
int rendered = 0;
int updates = 0;
while (running) {
firstTime = System.nanoTime() / 1000000000.0;
passedTime = firstTime - lastTime;
lastTime = firstTime;
updateTime += passedTime;
while(updateTime > FPS){
updates++;
update();
updateTime -= FPS;
}
render();
rendered++;
if((System.nanoTime() / 1000000000) - timer > 1){
timer += 1;
System.out.println("FPS:"+rendered+" Updates:"+updates);
updates = 0;
rendered = 0;
}
}
}
private void update() {
}
private void render() {
this.repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
level.repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setTitle("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game game = new Game();
frame.add(game);
game.start();
}
}
My LevelOne class:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LevelOne extends Level {
private BufferedImage spriteSheet;
private BufferedImage[] player = new BufferedImage[4]; //0 = down, 1 = right, 2 = up, 3 = left
private int dir = 0;
private String UP = "W";
private String DOWN = "S";
private String LEFT = "A";
private String RIGHT = "D";
private int speedX = 0;
private int speedY = 0;
public LevelOne(){
try {
spriteSheet = ImageIO.read(new File("images/sprites.png"));
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < 4; i++) {
player[i] = spriteSheet.getSubimage((i * 18), 0, 18, 28);
}
this.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {
String key = e.getKeyText(e.getKeyCode());
if (key.equals(UP)) {
dir = 2;
speedY = -5;
} else if (key.equals(LEFT)) {
dir = 3;
speedX = -5;
} else if (key.equals(RIGHT)) {
dir = 1;
speedX = 5;
} else if (key.equals(DOWN)) {
dir = 0;
speedY = 5;
}
}
#Override
public void keyReleased(KeyEvent e) {
String key = e.getKeyText(e.getKeyCode());
if (key.equals(UP)) {
speedY = 0;
} else if (key.equals(LEFT)) {
speedX = 0;
} else if (key.equals(RIGHT)) {
speedX = 0;
} else if (key.equals(DOWN)) {
speedY = 0;
}
}
#Override
public void keyTyped(KeyEvent e) {
}
});
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(player[dir], 0, 0, 18, 28, null);
}
}
LevelOne extends Level, which is currently empty(And extends JPanel), I'm not sure if I need to add anything to Level. I just dont get what I need to do to make the player image show up...
And sorry if my code is sloppy, I'm trying to get into higher level Java, and im not sure if I am just approaching it wrong.
Thanks.
Six things...
Don't call level.repaint from within the paintComponent method of Game, this could cause an infinite loop of repaint requests which is going to screw with your frame rate. Consider calling it within your render method
paintComponent should never public, there's no reason for anybody to ever call it directly.
Use key bindings over KeyListener, they will solve focus related issues. How to Use Key Bindings
Move frame.setVisible AFTER frame.add(game);, in fact, it really should the last thing you do
Make sure your images are loading properly
Set the layout manager for Game to BorderLayout
I'm trying to set the dimensions for this game i want to create. The error is at while(running); I went over it for an hour but i can't seem to catch the error. I'm using NetbeansIDE also setPreserredSize (new Dimension(WIDTH, HEIGHT)); gives me an error, it is asking to Create a method " setPrefferedSize(java.awt.Dimension)"in.com.francesc.game.Game
private void setPreserredSize(Dimension dimension) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
when i run it i get this error
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of type
at com.francescstudio.game.Game.<init>(Game.java:67)
at com.francescstudio.game.Start.main(Start.java:16)
Java Result: 1
heres all the code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* #author francesc
*/
public class Game extends JPanel implements KeyListener, Runnable {
public static final long SerialVersionUID = 1L;
public static final int WIDTH = 400;
public static final int HEIGHT = 630;
public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28);
private Thread game;
private boolean running;
private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private long startTime;
private long elapse;
private boolean set;
public Game() {
setFocusable(true);
setPreserredSize (new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
}
private void update() {
}
private void render() {
Graphics2D g = (Graphics2D) Image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
// render board
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(Image, 0, 0, null);
g2d.dispose();
}
#Override
public void run() {
}
int fps = 0, update = 0;
long fpsTimer = System.currentTimeMillis();
double nsPerUpdate = 1000000000.0 / 60;
// last update time in nano seconds
double then = System.nanoTime();
double unprocessed = 0;
while (running){
boolean shouldRender = false;
double now = System.nanoTime();
unprocessed += (now - then) / nsPerUpdate;
then = now;
// update queque
while (unprocessed >= 1) {
update++;
update();
unprocessed--;
shouldRender = true;
}
// render
if (shouldRender) {
fps++;
render();
shouldRender = false;
} else {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized start(){
if(running)return;
running = true;
game = new Thread (this, "game");
game.start();
}
public synchronized stop (){
if(!running) return;
running = false;
System.exit(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
Your problem is while (running) {} isn't inside of a method, you defined it in the class where it will never get called.
EDIT:
Your code should look something a little more like this
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* #author francesc
*/
public class Game extends JPanel implements KeyListener, Runnable {
public static final long SerialVersionUID = 1L;
public static final int WIDTH = 400;
public static final int HEIGHT = 630;
public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28);
private Thread game;
private boolean running;
private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private long startTime;
private long elapse;
private boolean set;
public Game() {
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
}
private void update() {}
private void render() {
Graphics2D g = (Graphics2D) Image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
// render board
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(Image, 0, 0, null);
g2d.dispose();
}
#Override
public void run() {
int fps = 0, update = 0;
long fpsTimer = System.currentTimeMillis();
double nsPerUpdate = 1000000000.0 / 60;
// last update time in nano seconds
double then = System.nanoTime();
double unprocessed = 0;
while (running) {
boolean shouldRender = false;
double now = System.nanoTime();
unprocessed += (now - then) / nsPerUpdate;
then = now;
// update queque
while (unprocessed >= 1) {
update++;
update();
unprocessed--;
shouldRender = true;
}
// render
if (shouldRender) {
fps++;
render();
shouldRender = false;
} else {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public synchronized start(){
if(running)return;
running = true;
game = new Thread (this, "game");
game.start();
}
public synchronized stop (){
if(!running) return;
running = false;
System.exit(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
If you run the program, you can see that it prints "Run() method is called", when the run gets called. But the System.out.println() inside the if statement does not get called nor the render() method gets called.
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static int WIDTH = 300;
public static int HEIGHT = WIDTH / 16*9;
public static int SCALE = 3;
public static String TITLE = "";
private Thread thread;
private boolean running = false;
private JFrame frame;
public void start() {
if(running) return;
thread = new Thread(this);
thread.start();
}
public void stop() {
if(!running) return;
try{
thread.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("Run() has been called");
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
double ns = 1000000000.0 / 60.0;
double delta = 0;
int ticks = 0;
int fps = 0;
while(running) {
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
ticks++;
delta--;
}
render();
fps++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("Fps: " + fps + " Ticks: " + ticks);
fps = 0;
ticks = 0;
}
}
stop();
}
public void tick() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs==null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.fillRect(36, 25, 25, 25);
g.dispose();
bs.show();
}
public Game() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("SPACE ADAPT PRE-ALPHA 0.001");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
You never set running to true, then it's false. As a side note not related with this question but most of swing components methods are not thread-safe so calling in another thread that is not the Event Dispatch Thread would not work as you expected.
Read more Concurrency in Swing