7 14 2011 create a new method at the end of
play

7/14/2011 Create a new method at the end of your GamePanel class - PowerPoint PPT Presentation

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


  1. 7/14/2011

  2. Create a new method at the end of your GamePanel class public static BufferedImage loadImage(String path) { }

  3.  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); }

  4. 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; } }

  5. 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()

  6. 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);

  7. Run your program and you should see this:

  8. 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;

  9. 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"); }

  10. 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); }

  11. Update the enemy sprite’s position: private void gameUpdate() { if (!gameOver) ; enemyPosX += 2; enemyPosY += -2; }

  12.  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; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend