Download: jar file
GameBoard
Game
Player
GameBoard.java
01: import java.awt.*; 02: import javax.swing.*; 03: import java.util.*; 04: 05: public class GameBoard extends JApplet 06: { 07: Game panel; 08: public void init() 09: { 10: EventQueue.invokeLater(new Runnable() { 11: public void run() 12: { 13: panel = new Game(); 14: add(panel); 15: } 16: }); 17: } 18: 19: public void start() { 20: panel.start(); 21: } 22: 23: public void stop() { 24: panel.stop(); 25: } 26: 27: public static void main(String args[]) { 28: Game game = new Game(); 29: game.start(); 30: JFrame application = new JFrame("GameBoard"); 31: application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 32: application.add(game); 33: application.setSize(412,425); 34: application.setVisible(true); 35: } 36: 37: }
Game.java
001: import java.net.*; 002: import java.io.*; 003: import java.awt.Graphics; 004: import java.awt.event.*; 005: import javax.swing.*; 006: import javax.swing.Timer; 007: import java.util.*; 008: 009: public class Game extends JPanel { 010: ArrayList<Player> players; 011: 012: public Game () 013: { 014: players = new ArrayList<Player>(); 015: } 016: 017: public void showPlayers() 018: { 019: for (Player p: players) System.out.println(p); 020: } 021: 022: public void getStatus() { 023: URL url; 024: String name; 025: name = "https://johnloomis.org/python/db/gameboard.py?action=status"; 026: char quote = '"'; 027: 028: String inp; 029: String modified, player, status; 030: try { 031: url = new URL(name); 032: BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //open input URL 033: boolean done = false; 034: while (!done) { 035: inp = in.readLine(); //read line from input, and parse as desired 036: if (inp==null) break; 037: //System.out.println("input: " + inp); 038: if (inp.charAt(0) != '"') continue; 039: modified = inp.substring(1,20); 040: inp = inp.substring(21); 041: int n1, n2, cc2; 042: n1 = inp.indexOf(quote); 043: n2 = inp.substring(n1+1).indexOf(quote); 044: player = inp.substring(n1+1,n1+n2+1); 045: inp = inp.substring(n1+n2+2); 046: StringTokenizer tokens = new StringTokenizer(inp); 047: String str; 048: int id = Integer.parseInt(tokens.nextToken()); 049: int r = Integer.parseInt(tokens.nextToken()); 050: int g = Integer.parseInt(tokens.nextToken()); 051: int b = Integer.parseInt(tokens.nextToken()); 052: double px = Double.valueOf(tokens.nextToken()); 053: double py = Double.valueOf(tokens.nextToken()); 054: double vx = Double.valueOf(tokens.nextToken()); 055: double vy = Double.valueOf(tokens.nextToken()); 056: str = tokens.nextToken(); 057: str = str.substring(1,str.length()-1); 058: boolean found = false; 059: for (Player p: players) { 060: if (p.id!=id) continue; 061: found = true; 062: break; 063: } 064: if (found) continue; 065: Player p = new Player(id,player,str); 066: p.setColor(r,g,b); 067: p.setPosition(px,py); 068: p.setVelocity(vx,vy); 069: players.add(p); 070: } 071: in.close(); //when done, close the input 072: } 073: catch (MalformedURLException e) { //catch errors 074: System.out.println(e); 075: } 076: catch (IOException e) { 077: System.out.println(e); 078: } 079: } 080: 081: public void getMove() { 082: URL url; 083: String name; 084: name = "https://johnloomis.org/python/db/gameboard.py?action=move"; 085: char quote = '"'; 086: 087: String inp; 088: try { 089: //player tempPlayer=new player(); //current working player 090: url = new URL(name); 091: BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //open input URL 092: boolean done = false; 093: while (!done) { 094: inp = in.readLine(); //read line from input, and parse as desired 095: if (inp==null) break; 096: //System.out.println("input: " + inp); 097: if (!inp.startsWith("id")) continue; 098: StringTokenizer tokens = new StringTokenizer(inp); 099: String str; 100: tokens.nextToken(); 101: int id = Integer.parseInt(tokens.nextToken()); 102: double px = Double.valueOf(tokens.nextToken()); 103: double py = Double.valueOf(tokens.nextToken()); 104: double vx = Double.valueOf(tokens.nextToken()); 105: double vy = Double.valueOf(tokens.nextToken()); 106: //System.out.printf("id %d pos (%g, %g)\n",id,px,py); 107: for (Player p: players) { 108: if (p.id != id) continue; 109: p.setPosition(px,py); 110: p.setVelocity(vx,vy); 111: break; 112: } 113: } 114: in.close(); //when done, close the input 115: } 116: catch (MalformedURLException e) { //catch errors 117: System.out.println(e); 118: } 119: catch (IOException e) { 120: System.out.println(e); 121: } 122: } 123: 124: public static void main(String[] args) { 125: Game game = new Game(); 126: game.getStatus(); 127: game.getMove(); 128: game.showPlayers(); 129: } 130: 131: 132: private Timer myTimer; 133: public void start() { 134: myTimer = new Timer(1000,new TimerHandler() ); //create our timer 135: myTimer.start(); //start the timer 136: getStatus(); 137: getMove(); 138: } 139: 140: public void stop() { 141: myTimer.stop(); 142: } 143: 144: private class TimerHandler implements ActionListener 145: { 146: public void actionPerformed(ActionEvent actionevent) 147: { 148: getMove(); 149: repaint(); 150: } 151: } 152: public void paintComponent (Graphics g) 153: { 154: super.paintComponent(g); 155: 156: for(Player p : players) p.draw(g); 157: }
Player.java
01: import java.awt.*; 02: import java.awt.geom.*; 03: import java.awt.Color; 04: import java.awt.Graphics; 05: import java.awt.Graphics2D; 06: import java.util.*; 07: 08: public class Player 09: { 10: int id; 11: double x; //local variables 12: double y; 13: double vx, vy; 14: Color color; 15: String name; 16: String status; 17: int radius; 18: 19: public Player() //default constructor 20: { 21: } 22: public Player(int id, String name, String status) 23: { 24: this.id = id; 25: this.name = name; 26: this.status = status; 27: radius = 17; 28: } 29: 30: public void setPosition (double px, double py) 31: { 32: x = px; 33: y = py; 34: } 35: public void setVelocity(double vx, double vy) 36: { 37: this.vx = vx; 38: this.vy = vy; 39: } 40: 41: public void setColor (int R, int G, int B) //set color 42: { 43: color = new Color(R,G,B); 44: } 45: 46: public String toString() 47: { 48: String str = name + " (" + id + "):"; 49: str += " color(" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + ")"; 50: str += " position (" + x + ", " + y + ")"; 51: str += " velocity (" + vx + ", " + vy + ")"; 52: return str; 53: } 54: 55: public void draw(Graphics g) //plot current player to graphics window 56: { 57: g.setColor(color); 58: g.fillOval((int)(x-radius), (int) (y-radius), (int)2*radius, (int)2*radius); 59: } 60: }
Maintained by John Loomis, updated Sat Apr 05 21:14:31 2008