The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Java AWT Graphics2D - Not Updating Sprites

Hello, i have been working on a zombie defense game the past 3 days, and today i got to the point where i could compile my code and test it out before adding more features. The code compiled fine and the sprites are displayed but not updated to the screen, they are only drawn once. I have been trying to debug my code for the past hour to no success, hopefully your more experienced eyes can help solve the problem. I have also attached a zip file of all my code.

packageorg.zombiedefense;importjava.awt.Canvas;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics2D;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.awt.image.BufferStrategy;importjava.util.ArrayList;importjavax.swing.JFrame;importjavax.swing.JPanel;/*
* The main hook of my game. This class will be the manager for the dsplay
* and where all game logic is called/managed from.
*
* Display will consist of a loop that cycles through all entites within the game
* making each entity move and then drawing the entities in the new place. It will
* also allow the player to control their control.
*
* Game logic includes that it will be notified when entities within my game
* detect certain events or actions such as collision with other entities,
* enemy died, etc
*
* Max Winkler
*/publicclass Game extendsCanvas{//strategy allows me to use buffers/page flippingprivate BufferStrategy strategy;//to determine if game is running, main game loop is runningprivateboolean isRunning =true;//List of all entities that currently exist in my gameprivateArrayList entities =newArrayList();//List of all entities that ned to be removed, because they diedprivateArrayList removeEntities =newArrayList();//Entity for player 1private PlayerOneEntity player1;//speed that the player moves atprivateint moveSpeed =450;//the time that the player last fired a shotprivatelong lastShotFired =0;//The interval between the player's shots, how fast can the player shoot in msprivateint firingSpeed =150;//the number of enemies leftprivateint enemyCount;//true if left arrow key is pressedprivateboolean leftArrowPressed =false;//true if right arrow key is pressedprivateboolean rightArrowPressed =false;//true if the player is firingprivateboolean currentlyFiring =false;privateboolean onePlayer =true;privateboolean twoPlayer =false;privateboolean gameStarted =false;privateString string ="";//Constructor to create a window and set up the gamepublic Game(){//create a frame for our gameJFrame frame =newJFrame("Max's Zombie Defense");//get a hold on the frame and then set the resolutionJPanel panel =(JPanel)frame.getContentPane();
panel.setPreferredSize(newDimension(1024,768));
panel.setLayout(null);//set up canvas size
setBounds(0,0,1024,768);
panel.add(this);//stop AWT from repainting our canvas as i am going to do that manually with buffers
setIgnoreRepaint(true);//make the window visible
frame.pack();
frame.setResizable(false);
frame.setVisible(true);//check if user closed window
frame.addWindowListener(newWindowAdapter(){publicvoid windowClosing(WindowEvent e){System.exit(0);}});//adding a keyboard input listener to respond to keypresses
addKeyListener(new KeyInputHandler());//get focus so our program gets the key presses
requestFocus();//create the 2 way buffers
createBufferStrategy(2);
strategy = getBufferStrategy();}privatevoid startGame(){
entities.clear();
leftArrowPressed =false;
rightArrowPressed =false;
currentlyFiring =false;
player1 =new PlayerOneEntity(this,"sprites/mmright1.png",40,500);
entities.add(player1);
enemyCount =0;for(int i=0;i <10; i++){
EnemyEntity enemy =new EnemyEntity(this,"sprites/franken1.png",900+(i*25),500);
entities.add(enemy);
enemyCount++;}}publicvoid removeEntity(Entity entity){
removeEntities.add(entity);}publicvoid gameOver(){
string ="The Zombies have your BRAINS!";
onePlayer =false;
twoPlayer =false;}publicvoid gameWin(){
string ="You Have Survived, For Another Day!";
onePlayer =false;
twoPlayer =false;}publicvoid enemyKilled(){
enemyCount--;if(enemyCount ==0){
gameWin();}}publicvoid ableToShoot (){if((System.currentTimeMillis()- lastShotFired)< firingSpeed){return;}
lastShotFired =System.currentTimeMillis();
BulletEntity bullet =new BulletEntity(this,"sprites/bullet.gif",player1.getX()+30,player1.getY()+50);
entities.add(bullet);}publicvoid gameLoop(){long lastLoopTime =System.currentTimeMillis();while(isRunning){long lastUpdate =System.currentTimeMillis()- lastLoopTime;
lastLoopTime =System.currentTimeMillis();Graphics2D graphics =(Graphics2D) strategy.getDrawGraphics();
DrawBG bg =new DrawBG(graphics);
graphics.setColor(Color.orange);
graphics.drawString(string,512,450);//graphics.drawString("Press '1' or '2' for Single-Player or Two-Player",512,550);
string ="";
startGame();
gameStarted =true;if(gameStarted){for(int i=0;i<entities.size();i++){Entity entity =(Entity) entities.get(i);
entity.move();}}for(int i=0;i<entities.size();i++){Entity entity =(Entity) entities.get(i);
entity.draw(graphics);}for(int i=0;i<entities.size();i++){for(int j=i+1;j<entities.size();j++){Entity firstEntity =(Entity) entities.get(i);Entity checkAgainst =(Entity) entities.get(j);if(firstEntity.collisionDetection(checkAgainst)){
firstEntity.collisionWith(checkAgainst);
checkAgainst.collisionWith(firstEntity);}}}
entities.removeAll(removeEntities);
removeEntities.clear();
graphics.dispose();
strategy.show();
player1.setHorizontalVelocity(0);if((leftArrowPressed)&&(!rightArrowPressed)){
player1.setHorizontalVelocity(-moveSpeed);}elseif((rightArrowPressed)&&(!leftArrowPressed)){
player1.setHorizontalVelocity(moveSpeed);}if(currentlyFiring){
ableToShoot();}try{Thread.sleep(10);}catch(Exception e){}}}privateclass KeyInputHandler extendsKeyAdapter{publicvoid keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_LEFT){
leftArrowPressed =true;}if(e.getKeyCode()==KeyEvent.VK_RIGHT){
rightArrowPressed =true;}if(e.getKeyCode()==KeyEvent.VK_UP){
currentlyFiring =true;}if(e.getKeyCode()==KeyEvent.VK_1){
onePlayer =true;}}publicvoid keyReleased(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_LEFT){
leftArrowPressed =false;}if(e.getKeyCode()==KeyEvent.VK_RIGHT){
rightArrowPressed =false;}if(e.getKeyCode()==KeyEvent.VK_UP){
currentlyFiring =false;}}}publicstaticvoid main(String[] args){
Game g =new Game();
g.gameLoop();}}