first commit
This commit is contained in:
167
CS2335/lab4/Lab_files/Examples/Graphics/BasicGraphics.java
Normal file
167
CS2335/lab4/Lab_files/Examples/Graphics/BasicGraphics.java
Normal file
@@ -0,0 +1,167 @@
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.Container;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Color;
|
||||
|
||||
/**
|
||||
* CS2335 Lab 6 Example
|
||||
*
|
||||
* <PRE>
|
||||
* BasicGrpahics.java shows the basics of how to draw simple objects
|
||||
* <PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:tedchoc@cc.gatech.edu">Ted Choc</A>
|
||||
* @version Version 1.4, February 20, 2003
|
||||
*/
|
||||
public class BasicGraphics {
|
||||
/**
|
||||
* JFrame to which everything is rendered
|
||||
*/
|
||||
private JFrame guiFrame;
|
||||
|
||||
/**
|
||||
* Content Pane of the JFrame
|
||||
*/
|
||||
private Container contentPane;
|
||||
|
||||
/**
|
||||
* The panel to which all the graphics will be drawn
|
||||
*/
|
||||
private JPanel graphicsPanel;
|
||||
|
||||
/**
|
||||
* A modifier for guiFrame
|
||||
* @param guiFrame new guiFrame
|
||||
*/
|
||||
public void setGuiFrame(JFrame guiFrame) {
|
||||
this.guiFrame = guiFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* A modifier for contentPane
|
||||
* @param contentPane new contentPane
|
||||
*/
|
||||
public void setContentPane(Container contentPane) {
|
||||
this.contentPane = contentPane;
|
||||
}
|
||||
|
||||
/**
|
||||
* A modifier for graphicsPanel
|
||||
* @param graphicsPanel new graphicsPanel
|
||||
*/
|
||||
public void setGraphicsPanel(JPanel graphicsPanel) {
|
||||
this.graphicsPanel = graphicsPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for guiFrame
|
||||
* @return JFrame guiFrame
|
||||
*/
|
||||
public JFrame getGuiFrame() {
|
||||
return this.guiFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for contentPane
|
||||
* @return Container contentPane
|
||||
*/
|
||||
public Container getContentPane() {
|
||||
return this.contentPane;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for graphicsPanel
|
||||
* @return JPanel graphicsPanel
|
||||
*/
|
||||
public JPanel getGraphicsPanel() {
|
||||
return this.graphicsPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the window and sets it up for drawing
|
||||
*/
|
||||
public void makeWindow() {
|
||||
guiFrame = new JFrame("Basic Graphics Objects");
|
||||
|
||||
/* Handle Closing the Window */
|
||||
guiFrame.addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent we) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
contentPane = guiFrame.getContentPane();
|
||||
graphicsPanel = new JPanel() {
|
||||
/* Overwrite JPanels paintComponent Method */
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
drawGraphics(g);
|
||||
}
|
||||
};
|
||||
contentPane.add(graphicsPanel);
|
||||
guiFrame.setSize(175, 240);
|
||||
guiFrame.setVisible(true);
|
||||
graphicsPanel.setBackground(Color.lightGray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that draws the Graphics onto the graphicsPanel
|
||||
* @param g graphics g object
|
||||
*/
|
||||
public void drawGraphics(Graphics g) {
|
||||
/* Draws some Text */
|
||||
g.setColor(Color.black);
|
||||
g.drawString("Hey look, I'm some Text", 15, 15);
|
||||
|
||||
/* Draws a fancy 3-D Rectangle */
|
||||
g.setColor(Color.red);
|
||||
g.fill3DRect(50, 50, 50, 50, true);
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(60, 60, 30, 30);
|
||||
|
||||
/* Draws a line */
|
||||
g.setColor(Color.yellow);
|
||||
g.drawLine(50, 120, 130, 140);
|
||||
|
||||
/* Draws a circle */
|
||||
g.setColor(Color.orange);
|
||||
g.fillOval(140, 60, 20, 20);
|
||||
|
||||
/* Draws Complex Shape :) */
|
||||
g.setColor(Color.red);
|
||||
g.fillRoundRect(67, 145, 105, 30, 25, 25);
|
||||
g.fillRoundRect(75, 170, 89, 33, 10, 10);
|
||||
|
||||
g.setColor(Color.white);
|
||||
g.fillOval(72, 150, 18, 18);
|
||||
g.fillOval(147, 150, 18, 18);
|
||||
|
||||
g.setColor(Color.yellow);
|
||||
g.fillOval(84, 180, 6, 6);
|
||||
g.fillOval(147, 180, 6, 6);
|
||||
|
||||
g.setColor(Color.black);
|
||||
g.drawOval(72, 150, 18, 18);
|
||||
g.drawOval(147, 150, 18, 18);
|
||||
g.drawOval(84, 180, 6, 6);
|
||||
g.drawOval(147, 180, 6, 6);
|
||||
g.fillRoundRect(95, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(102, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(109, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(116, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(123, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(130, 150, 5, 50, 2, 2);
|
||||
g.fillRoundRect(137, 150, 5, 50, 2, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method that creates a new BasicGraphics
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
BasicGraphics test = new BasicGraphics();
|
||||
test.makeWindow();
|
||||
}
|
||||
}
|
||||
288
CS2335/lab4/Lab_files/Examples/Graphics/SimpleAnimation.java
Normal file
288
CS2335/lab4/Lab_files/Examples/Graphics/SimpleAnimation.java
Normal file
@@ -0,0 +1,288 @@
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.Container;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Color;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
/**
|
||||
* CS2335 Lab 6 Example
|
||||
*
|
||||
* <PRE>
|
||||
* SimpleAnimation.java shows the basics of how to create an animation
|
||||
* <PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:tedchoc@cc.gatech.edu">Ted Choc</A>
|
||||
* @version Version 1.4, February 20, 2003
|
||||
*/
|
||||
public class SimpleAnimation {
|
||||
/**
|
||||
* JFrame to which everything is rendered
|
||||
*/
|
||||
private JFrame guiFrame;
|
||||
|
||||
/**
|
||||
* Content Pane of the JFrame
|
||||
*/
|
||||
private Container contentPane;
|
||||
|
||||
/**
|
||||
* The panel to which all the graphics will be drawn
|
||||
*/
|
||||
private JPanel graphicsPanel;
|
||||
|
||||
/**
|
||||
* Determines the current rotation of the polygon
|
||||
*/
|
||||
private int rotationValue = 0;
|
||||
|
||||
/**
|
||||
* Determines how the current polygon should be scaled
|
||||
*/
|
||||
private double scaleValue = 1.0;
|
||||
|
||||
/**
|
||||
* Determines whether to shrink or grow the polygon
|
||||
*/
|
||||
private boolean increasingScale = true;
|
||||
|
||||
/**
|
||||
* Determines what color the square should be
|
||||
* 0 - Red, 1 - Green, 2 - Blue
|
||||
*/
|
||||
private int colorSelection = 0;
|
||||
|
||||
/**
|
||||
* Determines the current color
|
||||
*/
|
||||
private Color currentColor = Color.red;
|
||||
|
||||
/**
|
||||
* A modifier for guiFrame
|
||||
* @param guiFrame new guiFrame
|
||||
*/
|
||||
public void setGuiFrame(JFrame guiFrame) {
|
||||
this.guiFrame = guiFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* A modifier for contentPane
|
||||
* @param contentPane new contentPane
|
||||
*/
|
||||
public void setContentPane(Container contentPane) {
|
||||
this.contentPane = contentPane;
|
||||
}
|
||||
|
||||
/**
|
||||
* A modifier for graphicsPanel
|
||||
* @param graphicsPanel new graphicsPanel
|
||||
*/
|
||||
public void setGraphicsPanel(JPanel graphicsPanel) {
|
||||
this.graphicsPanel = graphicsPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for guiFrame
|
||||
* @return JFrame guiFrame
|
||||
*/
|
||||
public JFrame getGuiFrame() {
|
||||
return this.guiFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for contentPane
|
||||
* @return Container contentPane
|
||||
*/
|
||||
public Container getContentPane() {
|
||||
return this.contentPane;
|
||||
}
|
||||
|
||||
/**
|
||||
* An accessor for graphicsPanel
|
||||
* @return JPanel graphicsPanel
|
||||
*/
|
||||
public JPanel getGraphicsPanel() {
|
||||
return this.graphicsPanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the window and sets it up for drawing
|
||||
*/
|
||||
public void makeWindow() {
|
||||
guiFrame = new JFrame("Simple Animation");
|
||||
guiFrame.addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent we) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
contentPane = guiFrame.getContentPane();
|
||||
graphicsPanel = new JPanel() {
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
drawGraphics(g);
|
||||
}
|
||||
};
|
||||
contentPane.add(graphicsPanel);
|
||||
guiFrame.setSize(500, 500);
|
||||
guiFrame.setVisible(true);
|
||||
graphicsPanel.setBackground(Color.lightGray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the polygon on the Graphics
|
||||
* @param g the graphics object to be drawn on
|
||||
*/
|
||||
public void drawGraphics(Graphics g) {
|
||||
int[] xCoord = { 200, 300, 300, 200 };
|
||||
int[] yCoord = { 200, 200, 300, 300 };
|
||||
Polygon rectangle = new Polygon(xCoord, yCoord, 4);
|
||||
|
||||
//g.fillPolygon(rectangle);
|
||||
g.setColor(currentColor);
|
||||
g.fillPolygon(rotatePolygon(scalePolygon(rectangle, scaleValue),
|
||||
rotationValue));
|
||||
|
||||
//g.setColor(Color.blue);
|
||||
//g.fillPolygon(rotatePolygon(rectangle, 60));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the polygon by some angle - look into AffineTransform
|
||||
* for a better way to handle this. This is also kind of a
|
||||
* hack because it only works for rectangles, but it works for
|
||||
* this example
|
||||
* Rotation Matrix
|
||||
* | cos(a) -sin(a) 0 |
|
||||
* | sin(a) cos(a) 0 |
|
||||
* | 0 0 1 |
|
||||
* @param p polygon to be rotated
|
||||
* @param theta the angle to be rotated by
|
||||
* @return Polygon that has been rotated
|
||||
*/
|
||||
public Polygon rotatePolygon(Polygon p, int theta) {
|
||||
int[] xCoord = p.xpoints;
|
||||
int[] yCoord = p.ypoints;
|
||||
int xMidpoint = ((xCoord[1] - xCoord[0]) / 2) + xCoord[0];
|
||||
int yMidpoint = ((yCoord[2] - yCoord[1]) / 2) + yCoord[1];
|
||||
p.translate(-xMidpoint, -yMidpoint);
|
||||
xCoord = p.xpoints;
|
||||
yCoord = p.ypoints;
|
||||
|
||||
int[] xRotCoord = new int[p.xpoints.length];
|
||||
int[] yRotCoord = new int[p.ypoints.length];
|
||||
int numPoints = p.npoints;
|
||||
double thetaRad = Math.toRadians((double) theta);
|
||||
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
xRotCoord[i] = (int) (xCoord[i] * Math.cos(thetaRad))
|
||||
+ (int) (yCoord[i] * Math.sin(thetaRad));
|
||||
yRotCoord[i] = (int) (-(xCoord[i]) * Math.sin(thetaRad))
|
||||
+ (int) (yCoord[i] * Math.cos(thetaRad));
|
||||
}
|
||||
|
||||
Polygon rotatedP = new Polygon(xRotCoord, yRotCoord, numPoints);
|
||||
rotatedP.translate(xMidpoint, yMidpoint);
|
||||
p.translate(xMidpoint, yMidpoint);
|
||||
|
||||
return rotatedP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales the polygon by the specfied amount
|
||||
* @param p polygon prior to being scaled
|
||||
* @param scalar the amount to shrink or the grow the polygon by
|
||||
* @return the polygon that has been scaled
|
||||
*/
|
||||
public Polygon scalePolygon(Polygon p, double scalar) {
|
||||
int[] xCoord = p.xpoints;
|
||||
int[] yCoord = p.ypoints;
|
||||
int xMidpoint = ((xCoord[1] - xCoord[0]) / 2) + xCoord[0];
|
||||
int yMidpoint = ((yCoord[2] - yCoord[1]) / 2) + yCoord[1];
|
||||
p.translate(-xMidpoint, -yMidpoint);
|
||||
xCoord = p.xpoints;
|
||||
yCoord = p.ypoints;
|
||||
|
||||
int[] xScaleCoord = new int[p.xpoints.length];
|
||||
int[] yScaleCoord = new int[p.ypoints.length];
|
||||
int numPoints = p.npoints;
|
||||
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
xScaleCoord[i] = (int) (scalar * xCoord[i]);
|
||||
yScaleCoord[i] = (int) (scalar * yCoord[i]);
|
||||
}
|
||||
|
||||
Polygon scaledP = new Polygon(xScaleCoord, yScaleCoord, numPoints);
|
||||
scaledP.translate(xMidpoint, yMidpoint);
|
||||
p.translate(xMidpoint, yMidpoint);
|
||||
|
||||
return scaledP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Animates the square
|
||||
*/
|
||||
public void animateGraphics() {
|
||||
ActionListener listener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
rotationValue = (rotationValue + 5) % 360;
|
||||
|
||||
if (increasingScale == true) {
|
||||
if (scaleValue >= 2.5) {
|
||||
increasingScale = false;
|
||||
} else {
|
||||
scaleValue = scaleValue + 0.05;
|
||||
}
|
||||
} else {
|
||||
if (scaleValue <= 0.5) {
|
||||
increasingScale = true;
|
||||
} else {
|
||||
scaleValue = scaleValue - 0.05;
|
||||
}
|
||||
}
|
||||
|
||||
if (colorSelection == 0) {
|
||||
currentColor = new Color(currentColor.getRed() - 5,
|
||||
currentColor.getGreen() + 5, 0);
|
||||
|
||||
if (currentColor.getRed() <= 0) {
|
||||
colorSelection = 1;
|
||||
}
|
||||
} else if (colorSelection == 1) {
|
||||
currentColor = new Color(0,
|
||||
currentColor.getGreen() - 5,
|
||||
currentColor.getBlue() + 5);
|
||||
|
||||
if (currentColor.getGreen() <= 0) {
|
||||
colorSelection = 2;
|
||||
}
|
||||
} else {
|
||||
currentColor = new Color(currentColor.getRed() + 5, 0,
|
||||
currentColor.getBlue() - 5);
|
||||
|
||||
if (currentColor.getBlue() <= 0) {
|
||||
colorSelection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
graphicsPanel.repaint();
|
||||
}
|
||||
};
|
||||
|
||||
javax.swing.Timer animation = new javax.swing.Timer(50, listener);
|
||||
animation.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SimpleAnimation and begins the animation
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SimpleAnimation test = new SimpleAnimation();
|
||||
test.makeWindow();
|
||||
test.animateGraphics();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
public abstract class AbstractNetworkMessage implements Serializable {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private String messageName;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
public AbstractNetworkMessage() {
|
||||
messageName = "AbstractMessage";
|
||||
}
|
||||
|
||||
public AbstractNetworkMessage(String name) {
|
||||
messageName = name;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
|
||||
public String getMessageName() {
|
||||
return messageName;
|
||||
}
|
||||
|
||||
public String tokenize() {
|
||||
return messageName;
|
||||
}
|
||||
|
||||
}// end of class AbstractNetworkMessage
|
||||
32
CS2335/lab4/Lab_files/Examples/Networking/ClientExample.java
Normal file
32
CS2335/lab4/Lab_files/Examples/Networking/ClientExample.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ClientExample {
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
Socket s = new Socket("localhost", PORT);
|
||||
BufferedReader br = new BufferedReader(new
|
||||
InputStreamReader(s.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new
|
||||
OutputStreamWriter(s.getOutputStream()));
|
||||
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
|
||||
|
||||
System.out.println(s.getLocalAddress().toString());
|
||||
System.out.println(s.getLocalSocketAddress().toString());
|
||||
|
||||
String message = null;
|
||||
|
||||
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
while((message = stdIn.readLine()) != null) {
|
||||
System.out.println("Sending message: " + message);
|
||||
oos.writeObject(new TextMessage(message));
|
||||
oos.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
23
CS2335/lab4/Lab_files/Examples/Networking/NumberMessage.java
Normal file
23
CS2335/lab4/Lab_files/Examples/Networking/NumberMessage.java
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
public class NumberMessage extends AbstractNetworkMessage {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private int myNumber;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
/**
|
||||
* Creates a new <code>NumberMessage</code> instance.
|
||||
*
|
||||
*/
|
||||
public NumberMessage(int number) {
|
||||
super("NumberMessage");
|
||||
|
||||
myNumber = number;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
public String tokenize() {
|
||||
return super.tokenize() + ": " + Integer.toString(myNumber);
|
||||
}
|
||||
|
||||
}// end of class NumberMessage
|
||||
@@ -0,0 +1,24 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ClientExample {
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
Socket s = new Socket("localhost", PORT);
|
||||
BufferedReader br = new BufferedReader(new
|
||||
InputStreamReader(s.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new
|
||||
OutputStreamWriter(s.getOutputStream()));
|
||||
|
||||
System.out.println(s.getLocalAddress().toString());
|
||||
System.out.println(s.getLocalSocketAddress().toString());
|
||||
pr.println("Hello, world!");
|
||||
pr.flush();
|
||||
System.out.println(br.readLine());
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO Exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ServerExample extends Thread {
|
||||
|
||||
Socket socket;
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public ServerExample(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
String s = br.readLine();
|
||||
System.out.println("Received " + s);
|
||||
pr.println("Thank you for your input");
|
||||
pr.flush();
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(PORT);
|
||||
while (true) {
|
||||
Socket s = ss.accept();
|
||||
new ServerExample(s).start();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
45
CS2335/lab4/Lab_files/Examples/Networking/ServerExample.java
Normal file
45
CS2335/lab4/Lab_files/Examples/Networking/ServerExample.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class ServerExample extends Thread {
|
||||
|
||||
Socket socket;
|
||||
public static final int PORT = 1234;
|
||||
|
||||
public ServerExample(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
PrintWriter pr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
|
||||
AbstractNetworkMessage anm;
|
||||
|
||||
while((anm = ((AbstractNetworkMessage)(ois.readObject()))) != null) {
|
||||
String s = anm.tokenize();
|
||||
System.out.println("Received " + s);
|
||||
pr.println("Thank you for your input");
|
||||
pr.flush();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
} catch(Exception e) {
|
||||
System.out.println("Something aint workin': " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(PORT);
|
||||
while (true) {
|
||||
Socket s = ss.accept();
|
||||
new ServerExample(s).start();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Got IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
19
CS2335/lab4/Lab_files/Examples/Networking/TextMessage.java
Normal file
19
CS2335/lab4/Lab_files/Examples/Networking/TextMessage.java
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
public class TextMessage extends AbstractNetworkMessage {
|
||||
|
||||
//////////////////////////////// Data Members ///////////////////////////////
|
||||
private String myMessage;
|
||||
|
||||
//////////////////////////////// Constructors ///////////////////////////////
|
||||
public TextMessage(String msg) {
|
||||
super("TextMessage");
|
||||
|
||||
myMessage = msg;
|
||||
}
|
||||
|
||||
////////////////////////////////// Methods //////////////////////////////////
|
||||
public String tokenize() {
|
||||
return super.tokenize() + ": " + myMessage;
|
||||
}
|
||||
|
||||
}// end of class TextMessage
|
||||
@@ -0,0 +1,25 @@
|
||||
import java.util.*;
|
||||
|
||||
class ThreadExample extends Thread {
|
||||
String s;
|
||||
Random r;
|
||||
public ThreadExample(String s) {
|
||||
this.s = s;
|
||||
this.r = new Random();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for(int i=0; i<5; i++) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
this.sleep(r.nextInt(100));
|
||||
} catch (InterruptedException e)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
new ThreadExample("String One").start();
|
||||
new ThreadExample("String Two").start();
|
||||
}
|
||||
}
|
||||
60
CS2335/lab4/Lab_files/Examples/Threading/SimpleThread.java
Normal file
60
CS2335/lab4/Lab_files/Examples/Threading/SimpleThread.java
Normal file
@@ -0,0 +1,60 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SimpleThread implements Runnable {
|
||||
|
||||
private boolean terminate;
|
||||
|
||||
public SimpleThread() {
|
||||
// super("Sleepy Thread");
|
||||
|
||||
terminate = true;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return !terminate;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
terminate = false;
|
||||
|
||||
while(!terminate) {
|
||||
System.out.println("SimpleThread is alive!");
|
||||
// try {
|
||||
// this.sleep(2000);
|
||||
// } catch(InterruptedException ie) {
|
||||
// System.out.println("SimpleThread's sleep was interrupted!");
|
||||
// break;
|
||||
// }
|
||||
for(int i=1; i<2000000; i+=2) {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\nSimpleThread is dead...");
|
||||
}
|
||||
|
||||
public void kill() {
|
||||
terminate = true;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
SimpleThread st = new SimpleThread();
|
||||
new Thread(st).start();
|
||||
|
||||
String input = null;
|
||||
BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
try {
|
||||
while((input = stdIn.readLine()) != null) {
|
||||
if(input.equals("Kill")) {
|
||||
st.kill();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch(IOException ioe) {
|
||||
System.err.println("Caught IO Exception: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}// end of class SimpleThread
|
||||
25
CS2335/lab4/Lab_files/Examples/Threading/ThreadExample.java
Normal file
25
CS2335/lab4/Lab_files/Examples/Threading/ThreadExample.java
Normal file
@@ -0,0 +1,25 @@
|
||||
import java.util.*;
|
||||
|
||||
class ThreadExample extends Thread {
|
||||
String s;
|
||||
Random r;
|
||||
public ThreadExample(String s) {
|
||||
this.s = s;
|
||||
this.r = new Random();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for(int i=0; i<5; i++) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
this.sleep(r.nextInt(100));
|
||||
} catch (InterruptedException e)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
new ThreadExample("String One").start();
|
||||
new ThreadExample("String Two").start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user