This program is actually from a tutorial I found and I tried to change it around and make it into my own. Basically the tutorial had the user create a box and be able to move it around the screen. I am simply trying to load an image and move it around the screen but for some reason Java doesn't want me to. (It's very mean like that sometimes)
The code runs and all and no errors come up, but that is all I get is the black background I set and no image I am trying to load.
*****This is the Class with the main method on it, not much going on in here*********
package captain; import javax.swing.*; public class MainGame { JFrame window; Screen panel; public MainGame(){ window = new JFrame("Collistion detection, movement, double buffering, and a game loop!"); panel = new Screen(); window.setSize(800,600); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(panel); window.setVisible(true); } public void go(){ panel.startGame(); } public static void main(String[] args){ MainGame game = new MainGame(); game.go(); } }
********This is the class that I am actually trying to draw my images on the screen with*********
********And this is the class that controls my player / entity************package captain; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class Screen extends JPanel implements KeyListener{ BufferedImage buffer; Entity player; Entity enemy; Image myImage; Image playerImage; Image enemyImage; public Screen(){ setIgnoreRepaint(true); addKeyListener(this); setFocusable(true); } public void loadImages(){ myImage = new ImageIcon("C:\\Users\\Thrash\\Desktop\\practice\\CaptainStandRight.jpg").getImage(); } public void keyTyped(KeyEvent e){ } public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if(key ==KeyEvent.VK_LEFT) player.left = true; if(key == KeyEvent.VK_RIGHT) player.right = true; if(key == KeyEvent.VK_UP) player.up = true; if(key == KeyEvent.VK_DOWN) player.down = true; } public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); if(key ==KeyEvent.VK_LEFT) player.left = false; if(key == KeyEvent.VK_RIGHT) player.right = false; if(key == KeyEvent.VK_UP) player.up = false; if(key == KeyEvent.VK_DOWN) player.down = false; } public void Initialize(){ buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); player = new Entity(myImage, 100,100); } public void update(){ player.move(); } public void drawBuffer(){ Graphics2D b = buffer.createGraphics(); b.setColor(Color.BLACK); b.fillRect(0, 0, 800, 600); b.drawImage(player.getImage(),100,100,null); b.dispose(); } public void drawScreen(){ Graphics2D g = (Graphics2D)this.getGraphics(); g.drawImage(buffer,0,0,this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void startGame(){ Initialize(); while(true){ try{ update(); drawBuffer(); drawScreen(); Thread.sleep(15); } catch(Exception e){ e.printStackTrace(); } } } }
package captain; import java.awt.Image; public class Entity { int x,y,speed; boolean up,down,left,right,collision; Image image; public Entity(Image image, int x, int y){ this.image = image; this.x = x; this.y = y; speed = 3; up = down = left = right = collision = true; } public int getX(){ return x; } public int getY(){ return y; } public Image getImage(){ return image; } public int getWidth(){ return image.getWidth(null); } public int getHeight(){ return image.getHeight(null); } public void move(){ if (up) y -= speed; if(down) y += speed; if(left) x -= speed; if(right) x += speed; } }