/* The following is the skeleton program for lab 6. You'll need to write classes for the goblin and the treasure, using the human class as a guide. Then, you should modify the animate function of the public class goblinapp. The animated gifs are located at /shared/csc/local/stuff/ on Hofstra Solaris computers. */ import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; public class goblinapp extends JFrame implements KeyListener { private Graphics brush; private Graphics display; private Image canvas; // off-screen graphics buffer private human professor; // professor object - don't redefine! private static final Color bkcolor = Color.black; //background color private static final int XBOUND = 800; // width of window private static final int YBOUND = 600; // height of window private static final int STEP = 4; // movement default step private static final int FDELAY = 30; // animation frame delay public static void main(String[] args) // needed for application { goblinapp session = new goblinapp(); session.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); session.init(); } public void paint(Graphics g) {} // overrides auto-update public void init() { setBounds(0,0,XBOUND,YBOUND); show(); canvas = createImage(XBOUND,YBOUND); brush = canvas.getGraphics(); display = this.getGraphics(); brush.setColor(bkcolor); // clear brush.fillRect(0,0,XBOUND,YBOUND); // with black background brush.setColor(Color.blue); addKeyListener(this); requestFocus(); // keys go to applet animate(); } // init public void clearbuffer() { Color old; old = brush.getColor(); brush.setColor(bkcolor); // clear brush.fillRect(0,0,XBOUND,YBOUND); // with black background brush.setColor(old); // restores color } // clearbuffer public void nextframe(int delay) // delay in ms { try { Thread.sleep(delay); } catch (InterruptedException IE) {} display.drawImage(canvas,0,0,null); // draws to screen clearbuffer(); } // nextframe with ms delay // event handling: respond to key strokes public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); // key pressed if (key == KeyEvent.VK_UP) professor.setdirection(0,-1*STEP); // move up if (key == KeyEvent.VK_DOWN) professor.setdirection(0,STEP); if (key == KeyEvent.VK_LEFT) professor.setdirection(-1*STEP,0); if (key == KeyEvent.VK_RIGHT) professor.setdirection(STEP,0); if (key == KeyEvent.VK_SPACE) professor.setdirection(0,0); // stop // else do nothing - direction remains the same } /* ----------------******************------------------ */ public void animate() { int x1, y1, x2, y2; // temp variables you might need boolean stop; // controls when loop stops stop = false; professor = new human(XBOUND-50, YBOUND-50,brush); // create other objects here, you can declare the variables // either above, inside the class, or here locally, just don't // declare them twice! try {Thread.sleep(1000);} catch (Exception ee) {} // wait 1 second while (stop == false) { // catch the professor! professor.move(); nextframe(FDELAY); // set stop to true when it's time to exit the loop } /* Use the following lines to display a message when game over: display.setFont(new Font("Serif",Font.BOLD,24)); display.setColor(Color.red); display.drawString("The Professor Wins!",XBOUND/2-100,YBOUND/2); */ } // animate } // class goblinapp /* ************************************************* */ class human { private Image img; // animated gif private int xcord, ycord; // coordinates of center private int dx, dy; // movement vector private Graphics brush; private int radius; private static final int XBOUND = 800; private static final int YBOUND = 600; // accessor methods public int getx() { return xcord; } public int gety() { return ycord; } public int getr() { return radius; } public int getdx() { return dx; } public int getdy() { return dy; } public human(int x0, int y0, Graphics b0) // constructor { xcord = x0; ycord = y0; // change the following to correct location of file: img = Toolkit.getDefaultToolkit().getImage("/shared/csc/local/stuff/man15.gif"); radius = 15; // may need to fine-tune brush = b0; } public void draw() { brush.drawImage(img,xcord-radius,ycord-radius,null); //brush.drawOval(xcord-radius,ycord-radius,2*radius,2*radius); } public void setdirection(int newdx, int newdy) { dx = newdx; dy = newdy; } public void move() // will wrap around edges of window { xcord = xcord + dx; if (xcord < radius/2) xcord = XBOUND-(radius/2); if (xcord > XBOUND-(radius/2)) xcord = radius/2; ycord = ycord + dy; if (ycord < radius/2) ycord = YBOUND-(radius/2); if (ycord > YBOUND-(radius/2)) ycord = radius/2; draw(); } } // end class human /* create classes for the goblin and the diamond/treasure here */