So all of the exceptions I have now are where I had my Graphics variable bb that would draw stuff, such as right here
Display.bb.drawImage(img, x*16, y*16, this);
How would i do that with my new setup with Graphics 2d?
So all of the exceptions I have now are where I had my Graphics variable bb that would draw stuff, such as right here
Display.bb.drawImage(img, x*16, y*16, this);
How would i do that with my new setup with Graphics 2d?
Well, I guess you could change bb to a Graphics2D object then:
public void paint(Graphics g){
super.paint(g);
bb= (Graphics2D) g;
GameLoop.stateStack.render();
bb.drawImage(backbuffer, 0, 0, this);
}
I haven’t tried this but it might work
Ok so that works! now the only issue that seems to be left is that my initializers in my run() method (lines 40-43) arnt working. These method calls basically import all my textures and such.
Heres the exception I’m getting
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at InventoryState.initialize(InventoryState.java:20)
at GameLoop.run(GameLoop.java:42)
at java.lang.Thread.run(Unknown Source)
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at GamePlayState.initialize(GamePlayState.java:26)
at GameLoop.run(GameLoop.java:43)
at java.lang.Thread.run(Unknown Source)
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at Player.<init>(Player.java:23)
at GamePlayState.entityAdder(GamePlayState.java:17)
at GameLoop.run(GameLoop.java:44)
at java.lang.Thread.run(Unknown Source)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GameLoop extends JFrame implements Runnable, KeyListener, State {
public static StateStack stateStack = new StateStack(); //in old code this was not static
final double gameplayFrequency=1000/60;
public static boolean [] keysPressed = new boolean[256];
public static boolean [] keysDown = new boolean[256];
public static boolean [] keysUp = new boolean[256];
private static boolean [] keysPrevious = new boolean[256];
public boolean running=true;
public static JFrame frame;
public GameLoop(){
frame=new JFrame("frame");// Create new JFrame
frame.add(new Display());//add the Display (this is possible because the Display class extends JPanel)
frame.setSize(200,200);//Set the size of the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Make sure all processes are closed when the window is closed
frame.setLocationRelativeTo(null);//Centers window
frame.setVisible(true);//Make the window visible
}
public static void main(String args[]){
new Thread(new GameLoop()).start();
}
//run method
public void run() {
//initializers
InventoryState.initialize();
GamePlayState.initialize();
GamePlayState.entityAdder();
//------------------------
addKeyListener(this);
double lastGameplayTime= System.currentTimeMillis();
double now;
double counting = 0;
while(running)
{
//this enables frame skipping
now = System.currentTimeMillis();
if(now>lastGameplayTime){
counting=0;
while (now>lastGameplayTime)
{
counting++;
if(counting>=10){
lastGameplayTime=System.currentTimeMillis();
break;
}
lastGameplayTime+=gameplayFrequency;
stateStack.update();
keyUpdate();
}
repaint(); //this refreshes the screen
}
}
now=System.currentTimeMillis();
if(now>lastGameplayTime){
try {Thread.sleep((long) (now-lastGameplayTime));}
catch (InterruptedException e) {e.printStackTrace();}
}
}
//key press methods
public void keyPressed(KeyEvent e) { //this method controls what to do when a key is pressed
keysPressed[e.getKeyCode()] = true;
keysDown[e.getKeyCode()]=true;
}
public void keyReleased(KeyEvent e) { //this method controls what happens when a key is released from being pressed
keysPressed[e.getKeyCode()] = false;
keysUp[e.getKeyCode()]=true;
}
public void keyTyped(KeyEvent arg0) { //ignore this method
}
//this method updates keysDown and keysUp so that they return to 'false' each frame.
private void keyUpdate(){
for(int i=255; i>0;i--)
keysDown[i] = false;
for(int i=255; i>0;i--)
keysUp[i]= false;
for(int i=256; i>keysPrevious.length; i--)
keysPrevious[i]=false;
}
//ignore these methods
public void update() {
}
public void render() {
}
}
try checking your paths for your files.
the paths are in the same directory as the source files, its not a filenotfound exception X.X
facepalm your right. I don’t know. Post the code to the classes that are throwing the exception
ill just post the code to one of the classes since its the same exact exception on each of the classes
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
//TODO: Inventory array for items.
//TODO: Quantity array for stackable items.
public class InventoryState implements State, ImageObserver {
public static boolean inventoryOpen = false;
public static BufferedImage [] inventoryImg = new BufferedImage[1];
//initialize method
public static void initialize(){
try {
inventoryImg[0]=ImageIO.read(new File("Inventory.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
//Update State method
public void update() {
if(GameLoop.keysDown[73] == true)
StateStack.popState();
//quantity grid
int qGrid[]= new int[26];
}
//Render State method
public void render() {
Image img = inventoryImg[0];
Display.bb.drawImage(img, 3, 50, this);
}
private void inventory(){
ArrayList<Items> iGrid = new ArrayList <Items>(26);
}
// ignore this method
public boolean imageUpdate(Image img, int infoflags, int x, int y,int width, int height) {return false;} //ignore this method
}
I was able to fix those exceptions by changing the lines where I load my textures to somthing like this
inventoryImg[0]=ImageIO.read(InventoryState.class.getClassLoader().getResourceAsStream("Inventory.png"));
but now im getting another exception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GamePlayState.render(GamePlayState.java:65)
at StateStack.render(StateStack.java:23)
at Display.paint(Display.java:15)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I think its a problem with the way I’m drawing stuff, heres my display class do yall see anything wrong with it?
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Display extends JPanel implements State { // oldcode extends gameloop class to Display.
public Image backbuffer;
public static Graphics2D bb;
public void paint(Graphics g){
super.paint(g);
Graphics2D bb= (Graphics2D) g;
GameLoop.stateStack.render();
bb.drawImage(backbuffer, 0, 0, this);
}
public void init(){
Thread th= new Thread();
th.start();
backbuffer=createImage(256,224);
bb=(Graphics2D) backbuffer.getGraphics();
}
public void update() {
}
public void render() {
}
// all of this is old code
/* public void init(){ //this method opens the Applet window
setSize (257,225);
Thread th= new Thread (this);
th.start();
backbuffer=createImage(256,224); //backbuffer image size
bb= backbuffer.getGraphics();
}
//TODO: fix flickering, may be in here...
public void paint (Graphics g) { //this method paints.
//draws foreground and menus
stateStack.render();
//copy back buffer to front buffer
g.drawImage(backbuffer, 0, 0, this);
} */
}
try this. All I did was remove the
Graphics2D
from in front of “bb”
public void paint(Graphics g){
super.paint(g);
bb= (Graphics2D) g;
GameLoop.stateStack.render();
bb.drawImage(backbuffer, 0, 0, this);
}-
nope, that didnt do anything, im just getting blank frames when I start it up, I think it has somthing to do with Graphics2D, so I think i’m gonna go back to just Graphics
unless it has somthing to do here in my GameLoop class
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GameLoop extends JFrame implements Runnable, KeyListener, State {
public static StateStack stateStack = new StateStack(); //in old code this was not static
final double gameplayFrequency=1000/60;
public static boolean [] keysPressed = new boolean[256];
public static boolean [] keysDown = new boolean[256];
public static boolean [] keysUp = new boolean[256];
private static boolean [] keysPrevious = new boolean[256];
public boolean running;
public static JFrame frame;
public GameLoop(){
frame=new JFrame("frame");// Create new JFrame
frame.add(new Display());//add the Display (this is possible because the Display class extends JPanel)
frame.setSize(400,400);//Set the size of the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Make sure all processes are closed when the window is closed
frame.setLocationRelativeTo(null);//Centers window
frame.setVisible(true);//Make the window visible
}
public void start(){
running=true;
new Thread(this).start();
}
public void stop(){
running=false;
}
public static void main(String args[]){
new GameLoop().start();
}
//run method
public void run() {
//initializers
InventoryState.initialize();
GamePlayState.initialize();
GamePlayState.entityAdder();
//------------------------
addKeyListener(this);
double lastGameplayTime= System.currentTimeMillis();
double now;
double counting = 0;
while(running)
{
//this enables frame skipping
now = System.currentTimeMillis();
if(now>lastGameplayTime){
counting=0;
while (now>lastGameplayTime)
{
counting++;
if(counting>=10){
lastGameplayTime=System.currentTimeMillis();
break;
}
lastGameplayTime+=gameplayFrequency;
stateStack.update();
keyUpdate();
}
repaint(); //this refreshes the screen
}
}
now=System.currentTimeMillis();
if(now>lastGameplayTime){
try {Thread.sleep((long) (now-lastGameplayTime));}
catch (InterruptedException e) {e.printStackTrace();}
}
}
I actually think I might just Scrap everything I have there and start from scratch and make it work with JFrame and all right from the beginning. Only thing is I’m not so sure about using Graphics2D over Graphics, what are the differences? or does it not really make too much of a difference?
Graphics2D enhances the Graphics object by adding additional features and functionalities that weren’t in the Graphics object before. In other words, it extends functionalities of Graphics.