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);
}
}