Skip to content

Instantly share code, notes, and snippets.

@youngnh
Forked from anonymous/Display.java
Created April 7, 2011 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youngnh/906934 to your computer and use it in GitHub Desktop.
Save youngnh/906934 to your computer and use it in GitHub Desktop.
Main.java
// This class does something with the board. I don't completely understand it, our teacher wrote it
package ui;
import centipede.Main;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/**
* Class description
*
*
* @version Enter version here..., 2011.04.06 at 12:34:59 CDT
* @author Enter your name here...
*/
public class Display extends JFrame implements KeyListener {
/** TODO Field description */
private Main main;
/** TODO Field description */
private JTextArea textBoard;
/**
* Creates new Display
* @param height
* @param width
* @param main
*/
public Display(int height, int width, Main main) {
this.main = main;
textBoard = new JTextArea();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
textBoard.setColumns(width + 1);
textBoard.setRows(height);
textBoard.setEditable(false);
textBoard.setFont(new java.awt.Font("Monospaced", 0, 12));
textBoard.setLineWrap(true);
textBoard.setAutoscrolls(false);
textBoard.addKeyListener(this);
setTitle("Centipede Game");
add(textBoard);
pack();
setResizable(false);
}
/**
* Handle the key typed event from the text field.
* @param e
*/
public void keyTyped(KeyEvent e) {}
/**
* Handle the key pressed event from the text field.
* @param e
*/
public void keyPressed(KeyEvent e) {
main.moveRight();
}
/**
* Handle the key released event from the text field.
* @param e
*/
public void keyReleased(KeyEvent e) {
main.moveLeft();
}
/**
* TODO Method description
*
* @return
*/
public JTextArea getTextBoard() {
return textBoard;
}
}
//This is our main class
package centipede;
import ui.Display;
import ui.Centipede;
import javax.swing.JTextArea;
/**
* Class description
*
*
* @version Enter version here..., 2011.04.06 at 12:19:46 CDT
* @author Enter your name here...
*/
public class Main {
/** TODO Field description */
private static final int HEIGHT = 40;
/** TODO Field description */
private static final int WIDTH = 20;
/** TODO Field description */
private int location = 0;
/**
* @param args the command line arguments
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
new Main().run();
}
/**
* TODO Method description
*
* @throws InterruptedException
*/
private void run() throws InterruptedException {
Display display = new Display(WIDTH, HEIGHT, this);
JTextArea textBoard = display.getTextBoard();
//textBoard.setText("0123456789012345678901234567890123456789012345678901234567890123456789");
display.setVisible(true);
Thread.sleep(2000);
//This draws a centipede and makes it move across the board
StringBuffer sb = new StringBuffer(WIDTH * HEIGHT);
for (int i = 0; i < WIDTH * HEIGHT; i++) {
sb.append(' ');
}
for (int i = 3; i < WIDTH * HEIGHT; i++) {
sb.setCharAt(i - 3, ' ');
Thread.sleep(50);
sb.setCharAt(i, '@');
sb.setCharAt(i-1,'@');
sb.setCharAt(i-2, '@');
textBoard.setText(sb.toString());
} // for
while (true) {
for (int i = 0; i < WIDTH * HEIGHT; i++) {
sb.setCharAt(i, ' ');
}
sb.setCharAt(location, '@');
textBoard.setText(sb.toString());
}
} // run
/**
* TODO Method description
*
*/
public void moveRight() {
location++;
if (location > WIDTH * HEIGHT) {
location = WIDTH * HEIGHT;
}
}
/**
* TODO Method description
*
*/
public void moveLeft() {
location--;
if (location < 0) {
location = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment