Created
December 1, 2013 20:14
-
-
Save poemdexter/7740099 to your computer and use it in GitHub Desktop.
Internet Janitor's VectorLand client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import java.awt.image.*; | |
import java.awt.geom.*; | |
import javax.swing.*; | |
import java.io.*; | |
import java.net.*; | |
public class VectorLand { | |
static final Image buff = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB); | |
public static void main(String[] args) { | |
Game app = new Game(); | |
Renderer ren = new Renderer(); | |
JFrame window = new JFrame("VectorLand"); | |
window.add(app); | |
window.addKeyListener(app); | |
app.addMouseListener(app); | |
app.addMouseMotionListener(app); | |
window.setResizable(false); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.pack(); | |
window.setVisible(true); | |
ren.start(); | |
while(true) { | |
app.repaint(); | |
if (app.out == null || ren.in == null) { | |
try { | |
//Socket sock = new Socket("127.0.0.1", 10101); | |
Socket sock = new Socket("wopr.csl.mtu.edu", 10101); | |
app.setOut(sock.getOutputStream()); | |
ren.setIn(sock.getInputStream()); | |
System.err.println("[game] server found!"); | |
} | |
catch(IOException e) { | |
try { Thread.sleep(5000); } | |
catch(InterruptedException i) {} | |
} | |
} | |
try { Thread.sleep(1000 / 30); } | |
catch(InterruptedException e) {} | |
} | |
} | |
} | |
class Game extends JPanel implements KeyListener, MouseListener, MouseMotionListener { | |
DataOutputStream out; | |
public Game() { | |
setPreferredSize(new Dimension(640, 480)); | |
} | |
public void setOut(OutputStream out) { | |
this.out = new DataOutputStream(out); | |
} | |
public void paint(Graphics g) { | |
synchronized(VectorLand.buff) { | |
g.drawImage(VectorLand.buff, 0, 0, null); | |
} | |
} | |
void writeCommand(int command, int... args) { | |
if (out == null) { return; } | |
try { | |
out.writeByte((byte)command); | |
for(int a : args) { out.writeInt(a); } | |
out.flush(); | |
} | |
catch(IOException e) { | |
System.err.println("[input] server has disconnected."); | |
out = null; | |
} | |
} | |
public void keyPressed (KeyEvent e) { writeCommand(0, e.getKeyCode()); } | |
public void keyReleased(KeyEvent e) { writeCommand(1, e.getKeyCode()); } | |
public void keyTyped (KeyEvent e) { writeCommand(2, e.getKeyChar()); } | |
public void mousePressed(MouseEvent e) { writeCommand(3); } | |
public void mouseReleased(MouseEvent e) { writeCommand(4); } | |
public void mouseClicked(MouseEvent e) {} | |
public void mouseEntered(MouseEvent e) {} | |
public void mouseExited(MouseEvent e) {} | |
public void mouseDragged(MouseEvent e) { writeCommand(5, e.getX(), e.getY()); } | |
public void mouseMoved(MouseEvent e) { writeCommand(5, e.getX(), e.getY()); } | |
} | |
class Renderer extends Thread { | |
final Graphics2D g = (Graphics2D)VectorLand.buff.getGraphics(); | |
{ | |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
//g.setStroke(new BasicStroke(1)); | |
} | |
DataInputStream in; | |
Color fill = Color.BLACK; | |
Color stroke = Color.BLACK; | |
public void setIn(InputStream in) { | |
this.in = new DataInputStream(in); | |
} | |
public void run() { | |
while(true) { | |
if (in == null) { | |
synchronized(VectorLand.buff) { | |
g.setColor(Color.WHITE); | |
g.fillRect(0, 0, 640, 480); | |
g.setColor(Color.BLACK); | |
g.drawString("No connection to server.", 10, 20); | |
} | |
try { Thread.sleep(500); } | |
catch(InterruptedException e) {} | |
} | |
else { | |
try { | |
in.readByte(); | |
synchronized(VectorLand.buff) { | |
g.setColor(Color.WHITE); | |
g.fillRect(0, 0, 640, 480); | |
frame(); | |
} | |
} | |
catch(IOException e) { | |
System.err.println("[renderer] server has disconnected."); | |
in = null; | |
} | |
} | |
} | |
} | |
private void frame() throws IOException { | |
while(true) { | |
byte command = in.readByte(); | |
int size = in.readInt(); | |
switch(command) { | |
case 0: return; | |
case 1: | |
fill = readColor(); | |
break; | |
case 2: | |
stroke = readColor(); | |
break; | |
case 3: | |
Path2D.Float path = new Path2D.Float(); | |
path.moveTo(in.readFloat(), in.readFloat()); | |
for(int z = (size/8)-1; z > 0; z--) { | |
path.lineTo(in.readFloat(), in.readFloat()); | |
} | |
path.closePath(); | |
g.setColor(fill); | |
g.fill(path); | |
g.setColor(stroke); | |
g.draw(path); | |
break; | |
default: | |
in.skip(size); | |
} | |
} | |
} | |
private Color readColor() throws IOException { | |
int c = in.readInt(); | |
return new Color( | |
(c >> 16) & 0xFF, | |
(c >> 8) & 0xFF, | |
c & 0xFF, | |
(c >> 24) & 0xFF | |
); | |
} | |
} |
JohnEarnest
commented
Dec 1, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment