Results 1 to 2 of 2
- 10-19-2012, 12:52 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Applet draws sprite but does not remove it
Hello all,
I'm remaking a game I made for my AP computer science class a year ago, and making everything better. However, no matter what it draws the old sprites on some of the ones I want to be displayed.
I'm not sure why it's doing thisJava Code:package SMBBB; import java.awt.*; import java.applet.*; import java.io.IOException; import java.text.DecimalFormat; import javax.swing.JFrame; @SuppressWarnings("serial") public class Game extends Applet implements Runnable { Graphics bufferGraphics; Image offscreen; int mapwidth = 21; int mapheight = 15; private int map[][] = new int[mapwidth][mapheight]; int cellwidth = 16; int cellheight = 16; public double px = 132; public double py = 90; int pwidth = cellwidth; int pheight = cellheight; boolean isjumping = false; boolean isfalling = false; double gravity = 0; boolean ismovingright = false; boolean ismovingleft = false; boolean ismovingup = false; boolean ismovingdown = false; double jumpforce = 4; boolean onladder = false; int scale = 2; boolean battle = false; int level = 1; JFrame j; DecimalFormat o; MapMaker M; Font font; int Spawn; Toolkit kit = Toolkit.getDefaultToolkit(); Image crouch = kit.createImage("graphics/crouch.png"); Image floormid = kit.createImage("graphics/floormid.png"); int deathheight = 20; Image floorcent = kit.createImage("graphics/floorcent.png"); Image bg; Image stand = kit.createImage("graphics/stand.png"); Image walk = kit.createImage("graphics/walk.gif"); Image running = kit.createImage("graphics/run.gif"); Image runningleft = kit.createImage("graphics/runl.gif"); Image walkleft = kit.createImage("graphics/walkl.gif"); Image block = kit.createImage("graphics/block.gif"); Image crouchleft = kit.createImage("graphics/crouchl.png"); Image standleft = kit.createImage("graphics/standl.png"); Image jumpleft = kit.createImage("graphics/jumpl.png"); Image leftcorner = kit.createImage("graphics/leftcorner.png"); Image wallsideleft = kit.createImage("graphics/wallsideleft.png"); Image wallturnupright = kit.createImage("graphics/wallturnupright.png"); Image lava = kit.createImage("graphics/lava.gif"); Image blockempty = kit.createImage("graphics/block.png"); AudioClip bgm; boolean debug = false; boolean run = false; int mariosize = 2; boolean canmove = true; int hit = 0; String dir = "right"; AudioClip jumps; Color Lava; AudioClip item; int lives = 3; AudioClip death; int deathsnd; Image deadimage = kit.createImage("graphics/dead.png"); boolean dead; double deathtimer = 0; Image flower = kit.createImage("graphics/flower.png"); Image transform = kit.createImage("graphics/transform.gif"); Image jump = kit.createImage("graphics/jump.png"); //fire mario images Image jumpfire = kit.createImage("graphics/firemario/jump.png"); Image jumpfireleft = kit.createImage("graphics/firemario/jumpl.png"); Image firestand = kit.createImage("graphics/firemario/stand.png"); Image firestandleft = kit.createImage("graphics/firemario/standl.png"); int flowery; double transformtimer; double dt = 0.01666667; boolean fire; boolean small; public Game(){ } public void init() { offscreen = createImage(700,700); bufferGraphics = offscreen.getGraphics(); new Thread(this).start(); setSize(320*scale, 240*scale); jumps = getAudioClip(getCodeBase(), "audio/jump.wav"); item = getAudioClip(getCodeBase(), "audio/powerup.wav"); death = getAudioClip(getCodeBase(), "audio/death.wav"); bgm = getAudioClip(getCodeBase(), "audio/level" + level + ".wav"); j = new JFrame("Super Mario Bros. Boss Battles"); j.add(this); j.setSize(328*scale, 260*scale); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.setFocusable(true); j.setVisible(true); o = new DecimalFormat("0.##"); Lava = new Color(120, 0, 0); M = new MapMaker(); try { map = M.newMap("maps/level" + level + ".png"); } catch (IOException e) { e.printStackTrace(); } font = new Font("Arial", Font.PLAIN, 18); bg = kit.createImage("graphics/background" + level + ".png"); bgm.loop(); if (mariosize == 3){ jump = kit.createImage("graphics/jump2.png"); } } public void run() { for(;;) { // animation loop never ends updateplayer(); repaint(); try { Thread.sleep(16); } catch (InterruptedException e) { } } } public void die(){ lives--; death.play(); } public void update (Graphics g) { bufferGraphics.clearRect( 0 , 0 , 800 , 800 ); bufferGraphics.drawImage(bg, 0, 0, 340*scale, 260*scale, this); // Draw map for( int y = 0 ; y < mapheight ; y++ ){ for ( int x = 0 ; x < mapwidth ; x++){ if( map[ y ][ x ] == 1 && (map[y-1][x] == 0 || map[y-1][x] == 2)){ bufferGraphics.setColor ( Color.white ); bufferGraphics.drawImage(floormid, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } else if ( map[y][x] == 4){ bufferGraphics.drawImage(leftcorner, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } else if (map[y][x] == 1){ bufferGraphics.drawImage(floorcent, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } else if (map[y][x] == 5 ){ bufferGraphics.drawImage(wallsideleft, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } else if (map[y][x] == 6){ bufferGraphics.drawImage(wallturnupright, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } if (map[y][x] == 7 && map[y-1][x] == 0){ bufferGraphics.drawImage(lava, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); } else if (map[y][x] == 7){ bufferGraphics.setColor(Lava); bufferGraphics.fillRect(x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale); } if (map[y][x] == 2 && Spawn == 0){ px = x*cellwidth; py = y*cellheight; Spawn = 1; mariosize = 2; fire = false; } if( map[ y ][ x ] == 3 && hit == 0){ bufferGraphics.setColor ( Color.white ); bufferGraphics.drawImage(block, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale+28, this ); if (debug){ bufferGraphics.setColor(Color.white); bufferGraphics.drawRect(x*cellwidth*scale, y*cellheight*scale, cellwidth*scale, cellheight*scale); } } if( map[ y ][ x ] == 3 && hit == 1){ bufferGraphics.setColor ( Color.white ); bufferGraphics.drawImage(blockempty, x * cellwidth*scale , y * cellheight*scale , cellwidth*scale , cellheight*scale, this ); if (debug){ bufferGraphics.setColor(Color.white); bufferGraphics.drawRect(x*cellwidth*scale, y*cellheight*scale, cellwidth*scale, cellheight*scale); } bufferGraphics.drawImage(flower, x * cellwidth*scale , y * cellheight*scale+flowery , cellwidth*scale , cellheight*scale, this ); } } } System.out.println(fire); if (!dead && mariosize == 3 && fire == true && small == false){ if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "right"){ bufferGraphics.drawImage(firestand, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "left"){ bufferGraphics.drawImage(firestandleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (ismovingright && !ismovingleft && !run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(walk, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (ismovingright && !ismovingleft && run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(running, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (ismovingdown && (isjumping || !isjumping)){ if (!ismovingright && !ismovingleft && !isjumping && !isfalling && ismovingdown && dir == "right"){ bufferGraphics.drawImage(crouch, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && ismovingdown && dir == "left"){ bufferGraphics.drawImage(crouchleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (ismovingright && ismovingdown){ bufferGraphics.drawImage(crouch, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (ismovingleft && ismovingdown){ canmove = false; bufferGraphics.drawImage(crouchleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } } if (!ismovingright && ismovingleft && !run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(walkleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (!ismovingright && ismovingleft && run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(runningleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "left"){ bufferGraphics.drawImage(standleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (canmove){ if (isjumping && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "right"){ bufferGraphics.drawImage(jumpfire, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isfalling && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "right"){ bufferGraphics.drawImage(jumpfire, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isfalling && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "left"){ bufferGraphics.drawImage(jumpfireleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isjumping && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "left"){ bufferGraphics.drawImage(jumpfireleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } } } if (mariosize == 2 && fire == false){ if (!dead && mariosize == 2 && fire == false && small == false){ if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "right"){ bufferGraphics.drawImage(stand, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "left"){ bufferGraphics.drawImage(standleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (ismovingright && !ismovingleft && !run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(walk, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (ismovingright && !ismovingleft && run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(running, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (ismovingdown && (isjumping || !isjumping)){ if (!ismovingright && !ismovingleft && !isjumping && !isfalling && ismovingdown && dir == "right"){ bufferGraphics.drawImage(crouch, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && ismovingdown && dir == "left"){ bufferGraphics.drawImage(crouchleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (ismovingright && ismovingdown){ bufferGraphics.drawImage(crouch, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); canmove = false; } if (ismovingleft && ismovingdown){ canmove = false; bufferGraphics.drawImage(crouchleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } } if (!ismovingright && ismovingleft && !run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(walkleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (!ismovingright && ismovingleft && run && !ismovingdown && !isjumping && !isfalling){ bufferGraphics.drawImage(runningleft, (int)px*scale, (int)py *scale-30, pwidth*scale+2 , pheight*scale+30, this ); } if (!ismovingright && !ismovingleft && !isjumping && !isfalling && !ismovingdown && dir == "left"){ bufferGraphics.drawImage(standleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (canmove){ if (isjumping && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "right"){ bufferGraphics.drawImage(jump, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isfalling && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "right"){ bufferGraphics.drawImage(jump, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isfalling && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "left"){ bufferGraphics.drawImage(jumpleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } if (isjumping && (ismovingright || ismovingleft || ismovingdown || !ismovingright || !ismovingleft || !ismovingdown) && dir == "left"){ bufferGraphics.drawImage(jumpleft, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); } } } } System.out.println(level); // Draw player System.out.println(mariosize); canmove = true; if (dead){ bufferGraphics.drawImage(deadimage, (int)px*scale, (int)py *scale, pwidth*scale , pheight*scale, this ); } if (blockcollision ( (int)px , (int)(py +1) , pwidth , pheight ) == true && hit == 1 && transformtimer < 2){ bufferGraphics.drawImage(transform, (int)px*scale, (int)py *scale-30, pwidth*scale , pheight*scale+30, this ); canmove = false; transformtimer = transformtimer + 1*dt; } if (debug){ bufferGraphics.setColor(Color.RED); bufferGraphics.drawRect((int)px*scale, (int)py*scale, pwidth*scale, pheight*scale); } bufferGraphics.setColor ( Color.green ); g.drawImage(offscreen,0,0,this); } public void updateplayer(){ if (debug){ bufferGraphics.setColor(Color.white); bufferGraphics.drawRect((int)px*scale, (int)py *scale, pwidth*scale , pheight*scale); } if (mariosize == 3){ fire = true; } if (transformtimer > 2){ hit = 2; canmove = true; mariosize = 3; transformtimer = 0; } if (dead){ deathtimer = deathtimer + 1 * 0.01666667; isfalling = false; bgm.stop(); if (deathheight > 0){ py -=3; deathheight--; } if (deathheight <= 0){ deathheight = 0; py += 3; } } if (deathtimer >= 5){ deathtimer = 0; deathsnd = 0; deathheight = 20; dead = false; Spawn = 0; } if (!dead){ if ( isjumping == false && isfalling == false ){ if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false && blockcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false && !dead){ isfalling = true; gravity = 0; } } if (canmove){ if (ismovingright && !run && blockcollision ( (int)px+1 , (int)(py) , pwidth , pheight ) == false){ if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){ px += 1; } } if (isfalling && lavacollision((int)px, (int)py+1, pwidth, pheight) && deathsnd == 0){ die(); deathsnd = 1; dead = true; } if (ismovingright && run && blockcollision ( (int)px+1 , (int)(py) , pwidth , pheight ) == false){ if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){ px += 2; } } if (ismovingleft && run && blockcollision ( (int)px-1 , (int)(py) , pwidth , pheight ) == false){ if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){ px -= 2; } } if (ismovingleft && !run && blockcollision ( (int)px-1 , (int)(py) , pwidth , pheight ) == false){ if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){ px -= 1; } } } if (ismovingright){ dir = "right"; } if (ismovingleft){ dir = "left"; } } if ( isfalling == true && isjumping == false ){ for ( int i = 0 ; i < gravity ; i++ ){ if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false && blockcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){ py += 1; }else{ gravity = 0; isfalling = false; } } gravity += .1; } if (blockcollision ( (int)px , (int)(py - 14) , pwidth , pheight ) == true && hit == 0){ hit = 1; item.play(); } if (hit == 1 && mariosize > 1){ flowery--; if (flowery < -32){ flowery = -32; } } if ( isjumping == true && isfalling == false ){ for ( int i = 0 ; i < gravity ; i++){ if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false && blockcollision ( (int)px , (int)(py - 14) , pwidth , pheight ) == false){ py -= 1; //System.out.print("still jumping : " + gravity); }else{ gravity = 0; isfalling = true; isjumping = false; } } if( gravity < 1 ) { gravity = 0; isfalling = true; isjumping = false; } gravity -= .1; } } public boolean mapcollision( int x , int y , int width , int height ){ int mapx = x / cellwidth; int mapy = y / cellheight; for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){ for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){ if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){ if ( map[y1][x1] == 1 || map[y1][x1] == 4 || map[y1][x1] == 5 || map[y1][x1] == 6 ){ Rectangle rec1 = new Rectangle( x , y , width , height ); Rectangle rec2 = new Rectangle( x1 * cellwidth, y1 * cellheight, cellwidth, cellheight); if( rec1.intersects( rec2 )) return true; } } } } return false; } public boolean blockcollision( int x , int y , int width , int height ){ int mapx = x / cellwidth; int mapy = y / cellheight; for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){ for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){ if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){ if ( map[y1][x1] == 3){ Rectangle rec1 = new Rectangle( x , y , width , height ); Rectangle rec2 = new Rectangle( x1 * cellwidth, y1 * cellheight, cellwidth, cellheight); if( rec1.intersects( rec2 )) return true; } } } } return false; } public boolean lavacollision( int x , int y , int width , int height ){ int mapx = x / cellwidth; int mapy = y / cellheight; for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){ for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){ if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){ if ( map[y1][x1] == 7 ){ Rectangle rec1 = new Rectangle( x , y , width , height ); Rectangle rec2 = new Rectangle( x1 * cellwidth, y1 * cellheight, cellwidth, cellheight); if( rec1.intersects( rec2 )) return true; } } } } return false; } public boolean keyDown (Event e, int key){ if( key == 97 ) // a key { ismovingleft = true; } if(key== 100) // d key { ismovingright = true; } if( key == 119 ) // w key { ismovingup = true; } if(key== 115) // s key { ismovingdown = true; } if (key == 46){ debug = !debug; } if( key == 32 ) // space bar for jump { if( isfalling == false && isjumping == false ) { isjumping = true; gravity = jumpforce; jumps.play(); } } if (key == 113){ run = true; } if (key == 49){ mariosize = 2; fire = false; } if (key == 50){ mariosize = 3; } System.out.println (" Integer Value: " + key); return true; } public boolean keyUp (Event e, int key){ if( key == 97 ) // a key { ismovingleft = false; } if( key == 100 ) // d key { ismovingright = false; } if( key == 119 ) // w key { ismovingup = false; } if( key == 115 ) // s key { ismovingdown = false; } if (key == 113){ run = false; } return true; } }
EDIT: This occurs when I change the sprites during "mariosize == 3"Last edited by Jerpost; 10-19-2012 at 02:06 AM.
- 10-19-2012, 04:05 AM #2
Re: Applet draws sprite but does not remove it
1. Why are you using the old java.awt.Applet and not the Swing javax.swing.JApplet which replaced it more than 10 years ago?
2. Nobody here is likely to go through 600+ lines of code to find what you have done wrong. To get better help sooner, post a SSCCE that clearly demonstrates your problem.
3. Go through this Tutorial Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbLast edited by DarrylBurke; 10-19-2012 at 04:11 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Button that Draws to canvas
By Rylogy in forum AWT / SwingReplies: 7Last Post: 02-11-2012, 05:42 AM -
Confisuion about sprite.getX() and sprite.getY()
By Basit781 in forum Java GamingReplies: 0Last Post: 01-10-2011, 06:16 AM -
JApplet.getGraphics() draws but JFrame doesn't
By ChazZeromus in forum New To JavaReplies: 5Last Post: 07-27-2009, 10:02 PM -
Remove Flashing in Applet
By Unome in forum Java AppletsReplies: 5Last Post: 05-30-2009, 07:26 PM -
FreeHand Draws poorly
By Chetanji in forum Java 2DReplies: 1Last Post: 04-24-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks