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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

7/14/2011

slide-2
SLIDE 2

Create a new method at the end of your GamePanel class

public static BufferedImage loadImage(String path) { }

slide-3
SLIDE 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); }

slide-4
SLIDE 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; } }

slide-5
SLIDE 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()

slide-6
SLIDE 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);

slide-7
SLIDE 7

Run your program and you should see this:

slide-8
SLIDE 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;

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

slide-10
SLIDE 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);

}

slide-11
SLIDE 11

Update the enemy sprite’s position:

private void gameUpdate() { if (!gameOver) ; enemyPosX += 2; enemyPosY += -2; }

slide-12
SLIDE 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; }

slide-13
SLIDE 13