/* * BigForest.java * * The pieces of BigForest that are different * from ForestFire * * written by mike slattery - sep 2003 */ public class BigForest extends Applet implements Runnable { Point dots[]; // Keep track of which coords bufgr is // currently using static boolean world_coords = false; // Variables to track scrolling view window public static int view_x=0, view_y=0; public static final int VIEW_WIDTH = 400; public static final int VIEW_HEIGHT = 300; public static final int WORLD_WIDTH = 800; public static final int WORLD_HEIGHT = 1000; public static final int SCROLL_X = 100; public static final int SCROLL_Y = 100; public void run() { ... checkScrolling(tree.getBoundingBox()); ... } public void checkScrolling(Rectangle r) { // Adjust view window so that the rectangle // r (in world coords) is inside the // scrolling limits (if possible) // // Check horizontal scrolling if (r.x < view_x + SCROLL_X) { view_x = r.x - SCROLL_X; if (view_x < 0) view_x = 0; } else if (r.x + r.width > view_x + VIEW_WIDTH - SCROLL_X) { view_x = r.x + r.width - VIEW_WIDTH + SCROLL_X; if (view_x +VIEW_WIDTH > WORLD_WIDTH) view_x = WORLD_WIDTH - VIEW_WIDTH; } // Check vertical scrolling if (r.y < view_y + SCROLL_Y) { view_y = r.y - SCROLL_Y; if (view_y < 0) view_y = 0; } else if (r.y + r.height > view_y + VIEW_HEIGHT - SCROLL_Y) { view_y = r.y + r.height - VIEW_HEIGHT + SCROLL_Y; if (view_y +VIEW_HEIGHT > WORLD_HEIGHT) view_y = WORLD_HEIGHT - VIEW_HEIGHT; } } public void setLevel1() { scenery = new Hittable[11]; scenery[0] = new SceneSprite(SceneSprite.ROCK, 150,100,rockImage,this); scenery[1] = new SceneSprite(SceneSprite.ROCK, 30,270,rockImage,this); scenery[2] = new SceneSprite(SceneSprite.LAKE, 420,550,lakeImage,this); scenery[3] = new SceneSprite(SceneSprite.ROCK, 120,40,rockImage,this); scenery[4] = new SceneSprite(SceneSprite.ROCK, 700,600,rockImage,this); scenery[5] = new SceneSprite(SceneSprite.ROCK, 400,650,rockImage,this); scenery[6] = new SceneSprite(SceneSprite.ROCK, 100,870,rockImage,this); scenery[7] = new WallSprite(0,0,5,WORLD_WIDTH); scenery[8] = new WallSprite(0,0,WORLD_HEIGHT,5); scenery[9] = new WallSprite(0,WORLD_HEIGHT-6,WORLD_HEIGHT,5); scenery[10] = new WallSprite(WORLD_WIDTH-6,0,5,WORLD_WIDTH); tree = new TreeSprite(5,5,scenery,treeImage,burnImage,this); fire = new FireSprite(320,100,scenery,tree,fireImage,smokeImage,this); // Create a bunch of random dots to make it easier to follow // movement dots = new Point[40]; for (int i = 0; i < dots.length; i++) { int dot_x = (int)(Math.random()*WORLD_WIDTH); int dot_y = (int)(Math.random()*WORLD_HEIGHT); dots[i] = new Point(dot_x, dot_y); } } public void update(Graphics g) { paint(bufgr); // Set back to screen coords to be ready // for next frame useScreenCoords(bufgr); g.drawImage(buffer,0,0,this); } static final void useWorldCoords(Graphics g) { if (!world_coords) { g.translate(-view_x, -view_y); world_coords = true; } } static final void useScreenCoords(Graphics g) { if (world_coords) { g.translate(view_x, view_y); world_coords = false; } } public void paint(Graphics g) { useScreenCoords(g); g.setColor(Color.white); g.fillRect(0,0,400,300); // Reset coords to reflect view window useWorldCoords(g); // First layer the dots g.setColor(Color.green); for (int i = 0; i < dots.length; i++) g.drawOval(dots[i].x, dots[i].y, 40, 30); //System.out.println("Begin scenery"); for (int i = 0; i < scenery.length; i++) { //System.out.println("In paint loop, i="+i); scenery[i].draw(g, boxDebug); } fire.draw(g, boxDebug); tree.draw(g, boxDebug); // Now, reset coords to draw things that // don't scroll useScreenCoords(g); g.setColor(Color.black); g.setFont(font); g.drawString("Status Line",100,25); if (wonGame()) { g.drawString("You win!",100,100); } if (lostGame()) { g.drawString("You lose.",100,100); } } private class mseL extends MouseAdapter { public void mouseClicked(MouseEvent e) { //This is kind of clumsy, but it works. // It would be better to keep track of whether or //not the applet has focus (with FocusListener) //and then either requestFocus or headTo as //appropriate. // requestFocus(); // Adjust x and y coords to allow for view window tree.headTo(view_x + e.getX(),view_y + e.getY()); } } }