7/14/2011
Create a new method at the end of your GamePanel class public static BufferedImage loadImage(String path) { }
Cut the following code found in the GamePanel() constructor: // load the background image try { File f = new File("C:\\...\\background.jpg"); BufferedImage im = ImageIO.read(f); int transparency = im.getColorModel().getTransparency(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); BufferedImage copy = gc.createCompatibleImage( im.getWidth(), im.getHeight(),transparency ); // create a graphics context Graphics2D g2d = copy.createGraphics(); // copy image g2d.drawImage(im,0,0,null); g2d.dispose(); bgImage = copy; } catch(IOException e) { System.out.println("Load Image error" + e); }
public static BufferedImage loadImage(String path) { try { File f = new File(path); BufferedImage im = ImageIO.read(f); int transparency = im.getColorModel().getTransparency(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); BufferedImage copy = gc.createCompatibleImage( im.getWidth(), im.getHeight(), transparency ); // create a graphics context Graphics2D g2d = copy.createGraphics(); // copy image g2d.drawImage(im,0,0,null); g2d.dispose(); //bgImage = copy; return copy; } catch(IOException e) { System.out.println("Load Image error" + e); return null; } }
GamePanel constructor public GamePanel() { setBackground(Color.blue); // blue background setPreferredSize( new Dimension(PWIDTH, PHEIGHT)); // load the background image bgImage = loadImage("C:\\...\\background.jpg"); } // end of GamePanel()
Replace JPanel with GamePanel In our main method cut all of the lines that have JPanel in them, and replace with: GamePanel gamePanel = new GamePanel(); content.add(gamePanel);
Run your program and you should see this:
Add an enemy sprite Add these variables to the top of the GamePanel class: private Image enemyImage = null; private int enemyPosX = 100; private int enemyPosY = 100;
Load enemy image in constructor: public GamePanel() { setBackground(Color.blue); // white background setPreferredSize( new Dimension(PWIDTH, PHEIGHT)); // load the background image bgImage = loadImage("C:\\...\\background.jpg"); enemyImage = loadImage("C:\\...\\enemy.png"); }
Draw the enemy sprite to the screen: private void gameRender() { // Render game content if (dbImage == null){ dbImage = createImage(PWIDTH, PHEIGHT); if (dbImage == null) { System.out.println("dbImage is null"); return; } else dbg = dbImage.getGraphics(); } // draw the background: use the image or a black colour if (bgImage == null) { dbg.setColor(Color.black); dbg.fillRect (0, 0, PWIDTH, PHEIGHT); } else { dbg.drawImage(bgImage, 0, 0, this); } dbg.drawImage(enemyImage, enemyPosX, enemyPosY, this); }
Update the enemy sprite’s position: private void gameUpdate() { if (!gameOver) ; enemyPosX += 2; enemyPosY += -2; }
Experiment by changing the enemy sprites start position: private int enemyPosX = 100; private int enemyPosY = 100; And changing the rate that the enemy sprite moves: private void gameUpdate() { if (!gameOver) ; enemyPosX += 2; enemyPosY += -2; }
Recommend
More recommend