I'm having a lot of trouble figuring out how to apply Double Buffering to this program I'm working on. I've tried several things, but none have worked so far. I've deleted my attempts to make the code more readable. Any help would be greatly appreciated!
My Main class:
import java.awt.*;import java.awt.event.KeyEvent;import java.awt.image.*;import java.io.File;import java.io.IOException;import java.util.Vector;import java.awt.event.KeyListener;import javax.imageio.*;import javax.swing.*;public class Map extends JFrame{ private BufferedImage overworld; private GameCharacter character; private Vector illegalPoints; Image offscreenImage; Graphics offscr; public Map(){ super("Zelda"); this.setSize(500,350); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try{ overworld = ImageIO.read(new File("overworld.gif")); } catch(IOException e){ System.err.println("Unable to read the map"); } this.setVisible(true); illegalPoints = new Vector(); //Add the top, and bottom for (int i = 0; i < 16; i++ ){ if (i != 7){ illegalPoints.add(new Point(i,0)); } illegalPoints.add(new Point(i,8)); } //add the middle points illegalPoints.add(new Point(14,7)); illegalPoints.add(new Point(11,7)); illegalPoints.add(new Point(10,7)); illegalPoints.add(new Point(9,7)); illegalPoints.add(new Point(8,7)); illegalPoints.add(new Point(9,6)); illegalPoints.add(new Point(10,6)); illegalPoints.add(new Point(1,7)); illegalPoints.add(new Point(1,1)); illegalPoints.add(new Point(14,1)); illegalPoints.add(new Point(8,1)); illegalPoints.add(new Point(9,1)); illegalPoints.add(new Point(10,1)); illegalPoints.add(new Point(11,1)); illegalPoints.add(new Point(9,2)); illegalPoints.add(new Point(10,2)); illegalPoints.add(new Point(4,3)); illegalPoints.add(new Point(4,5)); illegalPoints.add(new Point(12,5)); illegalPoints.add(new Point(12,3)); illegalPoints.add(new Point(0,7)); illegalPoints.add(new Point(0,6)); illegalPoints.add(new Point(0,1)); illegalPoints.add(new Point(0,2)); offscreenImage = createImage(this.size().width, this.size().height); offscr = offscreenImage.getGraphics(); myKeyListener listen = new myKeyListener(); this.addKeyListener(listen); } public boolean isLegalMove(int x, int y){ if (x < 0 || x > 14 || y < 0 || y > 14 ){ return false; } else if (illegalPoints.contains(new Point(x,y))){ return false; } return true; } public void setCharacter(GameCharacter c){ character = c; this.repaint(); } public void paint(Graphics g){ //super.paint(g); g.drawImage(overworld,0,0,null); if (character != null){ character.draw((Graphics2D) g); } } class myKeyListener implements KeyListener{ public void keyPressed(KeyEvent event) { switch(event.getKeyCode()) { case KeyEvent.VK_DOWN: GameCharacter.isDown = true; break; case KeyEvent.VK_UP: GameCharacter.isUp = true; break; case KeyEvent.VK_LEFT: GameCharacter.isLeft = true; break; case KeyEvent.VK_RIGHT: GameCharacter.isRight = true; break; } GameCharacter.update(); } public void keyReleased(KeyEvent event) { switch(event.getKeyCode()) { case KeyEvent.VK_DOWN: GameCharacter.isDown = false; break; case KeyEvent.VK_UP: GameCharacter.isUp = false; break; case KeyEvent.VK_LEFT: GameCharacter.isLeft = false; break; case KeyEvent.VK_RIGHT: GameCharacter.isRight = false; break; } GameCharacter.update(); } public void keyTyped(KeyEvent event) { // TODO Auto-generated method stub } } public static void main(String[] args){ Map m = new Map(); GameCharacter c = new GameCharacter(m); m.setCharacter(c); }}
My Character Class:
import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.*;public class GameCharacter { public static BufferedImage up; private BufferedImage down; private BufferedImage left; private BufferedImage right; public static int moveDirection; private static int x; private static int y; public static int iconSize; private static Map map; public static final int UP = 0; public static final int DOWN = 1; public static final int LEFT = 2; public static final int RIGHT = 3; public static boolean isUp; public static boolean isDown; public static boolean isLeft; public static boolean isRight; public static int drawX = 0; public static int drawY = 0; static int vX = 0; static int vY = 0; public GameCharacter(Map m){ try{ up = ImageIO.read(new File("Link_back.gif")); down = ImageIO.read(new File("Link_front.gif")); left = ImageIO.read(new File("Link_left.gif")); right = ImageIO.read(new File("Link_right.gif")); } catch(Exception e){ System.err.println("Unable to read the file"); } moveDirection = DOWN; iconSize = 32; map = m; x = 0; y = 3; } public void afterMove(){ map.repaint(); try{ Thread.sleep(20); } catch(InterruptedException e){ System.err.println("Woke up early"); } } public static boolean checkMapForMove(int x, int y){ if (map.isLegalMove(x, y)){ return true; } else{ System.err.println("Illegal Move at " + x + " " + y); return false; } } public static void update() { vX = 0; vY = 0; if (isUp) { if(checkMapForMove(x, y-1)){ vY -= 1; } moveDirection = UP; } if (isDown) { if(checkMapForMove(x, y+1)){ vY += 1; } moveDirection = DOWN; } if (isLeft) { if(checkMapForMove(x-1, y)){ vX -= 1; } moveDirection = LEFT; } if (isRight) { if(checkMapForMove(x+1, y)){ vX += 1; } moveDirection = RIGHT; } } public void draw(Graphics2D g){ x += vX; y += vY; drawX = x * iconSize; drawY = y * iconSize+iconSize; if(moveDirection == UP){ g.drawImage(up,drawX,drawY,null); } else if (moveDirection == DOWN){ g.drawImage(down,drawX,drawY,null); } else if (moveDirection == LEFT){ g.drawImage(left,drawX,drawY,null); } else if (moveDirection == RIGHT){ g.drawImage(right,drawX,drawY,null); } afterMove(); }}
Thanks again.