-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more than one splash when transmitting the snapthot (but the time they dissapper is still asynchronous QQ) dynamically update the money and the scoreboard
- Loading branch information
1 parent
a224a36
commit 5c6c3fe
Showing
6 changed files
with
1,480 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
package main; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
import java.util.ArrayList; | ||
import javax.swing.JFrame; | ||
import javax.swing.JOptionPane; | ||
|
||
import object.client.Splash; | ||
import object.server.Database; | ||
import object.server.Player; | ||
import object.server.WaitWindow; | ||
import object.tool.AudioPlayer; | ||
|
||
@SuppressWarnings("serial") | ||
public class Client extends JFrame { | ||
|
||
// information of server | ||
private String IPAddress; | ||
private int portNum; | ||
|
||
// connection between server and client | ||
private Socket socket; | ||
private ObjectInputStream objIn; | ||
private ObjectOutputStream objOut; | ||
|
||
// content | ||
private MainApplet applet; | ||
|
||
// resources | ||
private static JFrame window; | ||
private static AudioPlayer audio; | ||
|
||
// Constructor | ||
public Client(String IPAddress, int portNum) { | ||
|
||
// set IP & port | ||
this.IPAddress = IPAddress; | ||
this.portNum = portNum; | ||
|
||
// create PApplet | ||
applet = new MainApplet(); | ||
applet.init(); | ||
applet.start(); | ||
applet.setFocusable(true); | ||
|
||
// create Login PApplet | ||
Login loginApplet = new Login(this); | ||
loginApplet.init(); | ||
loginApplet.start(); | ||
loginApplet.runFrame(); | ||
|
||
} | ||
|
||
// connect to server | ||
public void connect() { | ||
|
||
try { | ||
|
||
// create server's socket | ||
socket = new Socket(IPAddress, portNum); | ||
|
||
// bind I/O between server and client | ||
objOut = new ObjectOutputStream(socket.getOutputStream()); | ||
objIn = new ObjectInputStream(socket.getInputStream()); | ||
ClientThread connection = new ClientThread(); | ||
connection.start(); | ||
connection.send(applet.getPlayer()); | ||
applet.setClientThread(connection); | ||
|
||
} catch (IOException e) { | ||
|
||
JOptionPane.showMessageDialog(null, "Server did not exist.\nPlease try again."); | ||
System.exit(0); | ||
return; | ||
|
||
} | ||
|
||
} | ||
|
||
// Inner class: connection with server | ||
class ClientThread extends Thread { | ||
|
||
// the program process of client | ||
public void run() { | ||
|
||
while (true) { | ||
|
||
// read | ||
Object message = receive(); | ||
|
||
// identify which object is transmit | ||
if (message instanceof String) { | ||
|
||
// identify which flag is | ||
switch ((String) message) { | ||
|
||
// send answer is correct | ||
case "Correct": | ||
|
||
applet.getGame().answerCorrect(); | ||
break; | ||
|
||
// send answer is wrong | ||
case "Wrong": | ||
|
||
applet.getGame().answerWrong(); | ||
break; | ||
|
||
// being attacked by other players | ||
case "be attacked": | ||
System.out.println("be attacked"); | ||
int color = (int) receive(); | ||
applet.beAttacked(color); | ||
break; | ||
|
||
case "showPic": | ||
System.out.println("SHOWPIC"); | ||
String picName = (String) receive(); | ||
//Splash splash = (Splash) receive(); | ||
ArrayList<Splash> splash = (ArrayList<Splash>) receive(); | ||
applet.showPic(picName, splash); | ||
break; | ||
|
||
// server is closing | ||
case "terminate": | ||
|
||
JOptionPane.showMessageDialog(null, "Server did not respond.\nThe window will be closed."); | ||
closeClient(); | ||
break; | ||
|
||
default: | ||
break; | ||
|
||
} | ||
|
||
} else if (message instanceof ArrayList<?>) { | ||
applet.resetReference((ArrayList<Player>) message); | ||
System.out.println("update score board"); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
// transmit object to server | ||
public void send(Object o) { | ||
|
||
try { | ||
objOut.reset(); | ||
objOut.writeObject(o); | ||
objOut.flush(); | ||
|
||
} catch (IOException e) { | ||
|
||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
|
||
} | ||
|
||
} | ||
|
||
// receive object from server | ||
public Object receive() { | ||
|
||
Object message = null; | ||
|
||
try { | ||
|
||
message = objIn.readObject(); | ||
System.out.println("receive: " + message); | ||
|
||
} catch (IOException e) { | ||
|
||
JOptionPane.showMessageDialog(null, "Server did not respond.\nThe window will be closed."); | ||
closeClient(); | ||
|
||
} catch (Exception e) { | ||
|
||
} | ||
|
||
return message; | ||
} | ||
|
||
} | ||
|
||
// set player | ||
public void setPlayer(Player player) { | ||
this.applet.setPlayer(player); | ||
} | ||
|
||
// close client | ||
public void closeClient() { | ||
|
||
applet.dispose(); | ||
window.dispose(); | ||
Database database = new Database(); | ||
WaitWindow waitWindow = new WaitWindow("UPDATING"); | ||
waitWindow.init(); | ||
waitWindow.start(); | ||
waitWindow.runFrame(); | ||
database.updateUserDatabase(applet.getPlayer()); | ||
waitWindow.closeFrame(); | ||
System.exit(0); | ||
|
||
} | ||
|
||
// main | ||
public static void main(String[] args) { | ||
|
||
// create client & Run login app | ||
Client client = new Client("127.00.00.01", 8000); | ||
client.connect(); | ||
|
||
// set sound | ||
audio = new AudioPlayer(new File("src/resource/bgm.wav")); | ||
audio.setPlayCount(0); | ||
audio.play(); | ||
|
||
// create frame and connect to server | ||
window = new JFrame("ZOOM and BOOM"); | ||
window.setContentPane(client.applet); | ||
window.addWindowListener(new java.awt.event.WindowAdapter() { | ||
@Override | ||
public void windowClosing(java.awt.event.WindowEvent windowEvent) { | ||
if (JOptionPane.showConfirmDialog(window, "Are you sure you want to close this game?", | ||
"Really Closing?", JOptionPane.YES_NO_OPTION, | ||
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { | ||
client.closeClient(); | ||
} | ||
} | ||
}); | ||
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
window.setSize(1117, 690); | ||
window.setLocation(120, 30); | ||
window.setVisible(true); | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package object.client; | ||
|
||
import java.awt.Color; | ||
import java.util.ArrayList; | ||
|
||
//import java.util.ArrayList; | ||
import processing.core.PApplet; | ||
import processing.core.PImage; | ||
import object.tool.Button; | ||
|
||
public class ConfirmWindow extends PApplet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
// content | ||
private PImage img; | ||
private Button btn; | ||
private ArrayList<Splash> splash = new ArrayList<Splash>(); | ||
//private Splash splash; | ||
|
||
// Constructor | ||
public ConfirmWindow(PImage img, ArrayList<Splash> list) { | ||
|
||
this.img = img; | ||
this.btn = new Button(this, 200, 200, 50, new Color(0, 0, 0).getRGB()); | ||
//this.splash = new Splash(this, spl.getX(), spl.getY(), 10, spl.getColor()); | ||
|
||
for(Splash spl : list){ | ||
this.splash.add(new Splash(this, spl.getX(), spl.getY(),spl.getTrans(), spl.getColor())); | ||
System.out.println("trans= "+spl.getTrans()); | ||
} | ||
} | ||
|
||
// update screen content | ||
public void draw() { | ||
|
||
image(img, 0, 0, 800, 450); | ||
btn.display_circle(); | ||
//splash.display(); | ||
for(Splash s:splash) s.display(); | ||
// draw splash | ||
} | ||
|
||
} |
Oops, something went wrong.