Skip to content

Commit

Permalink
Added Load From Database Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Virginia Ar�valo Rivara authored and Eva Virginia Ar�valo Rivara committed Jun 7, 2016
1 parent d5b8733 commit d05fc66
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 11 deletions.
10 changes: 8 additions & 2 deletions ZOOM_and_BOOM/src/main/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import object.client.UpdateDatabaseWindow;
import object.server.Answer;
import object.server.LoadFromDatabaseWindow;
import object.server.Player;
import object.server.ServerUpdateDatabaseWindow;
import processing.core.PImage;
Expand All @@ -43,17 +44,21 @@ public class Server extends JFrame {
private String path = new String("src/resource/pic_rsc");
private String[] list;

//Update database
//Update/Load database
private ServerUpdateDatabaseWindow updateWindow;
private LoadFromDatabaseWindow loadWindow;


// Constructor
public Server(int portNum) {

//create update database window
//create update database load/update windows
updateWindow = new ServerUpdateDatabaseWindow(this);
updateWindow.init();
updateWindow.start();
loadWindow = new LoadFromDatabaseWindow(this);
loadWindow.init();
loadWindow.start();

// set up of server's frame
setSize(400, 200);
Expand Down Expand Up @@ -358,6 +363,7 @@ public static void main(String[] args) {

// create server
Server server = new Server(8000);
server.loadWindow.runFrame();
server.runForever();

}
Expand Down
16 changes: 16 additions & 0 deletions ZOOM_and_BOOM/src/object/server/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ public class Answer {
float x, y, width, height;
float x2, y2, w2, h2;

//Default Constructor
public Answer(){
//empty
}

public Answer(float width, float height, float x, float y){
this.width=width;
this.height=height;
this.x=x;
this.y=y;
}

public Answer(float width, float height, float x, float y, int datanum){
this(width,height,x,y);
this.dataNum=datanum;
}

// compare answers
public boolean check(float x, float y, float width, float height){
Expand Down
211 changes: 211 additions & 0 deletions ZOOM_and_BOOM/src/object/server/LoadFromDatabaseWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package object.server;

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import main.Server;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

public class LoadFromDatabaseWindow extends PApplet
{
//Database data
private static final String jdbcDriver = "com.mysql.jdbc.Driver";
private static final String sqlDriver = "jdbc:mysql://db4free.net:3306/player_database";
private static final String sqlUser = "ssnthuac_final";
private static final String sqlPass = "ssnthuac";

//GUI
private static final int width = 600;
private static final int height = 350;
private PImage backgroundImg;
private PFont loginFont;

//Wait
private int rand=0;
private Color[] splashColors;

//Update successful
boolean updateSuccessful;

private Server server;

//Constructor
public LoadFromDatabaseWindow(Server server){
updateSuccessful=false;
this.server=server;
}

//Processing Setup
public void setup() {
background(255);
frameRate(500);
size(width, height);
smooth();
noStroke();
loginFont = createFont("../resource/fonts/HappyGiraffe.ttf",32);
textFont(loginFont);
//loadImages
try {
backgroundImg = loadImage("resource/paintBackground.png");
}
catch(Exception ex){
System.err.println("Unable to load cursor or Backgroun Images");
ex.printStackTrace();
}
//Wait animation
setColors();
rand = (int) random(12);
tint(255, 130);
//image(backgroundImg,0,0);
tint(255, 255);
}

//Processing Draw
public void draw(){

if (frameCount % 30 == 0){
rand = (int) random(12);
text("LOADING DATABASE...",(width-textWidth("UPDATING DATABASE..."))/2,300);
}
if (frameCount % 2 == 0) {
fill(splashColors[rand].getRed(),splashColors[rand].getGreen(),splashColors[rand].getBlue());
pushMatrix();
translate(300, 175);
rotate(radians(frameCount/2 % 360));
rect(0, 25, 50, 2);
popMatrix();
}
}


//update player data in database
public void loadDatabase(){

Thread databaseThread = new Thread(new Runnable() {
public void run(){
//Money and Color will be set to 0 by default as per database's settings

Connection sqlConn = null;
Statement sqlState = null;
String selectQuery = null;
ResultSet sqlResult = null;

try{

Class.forName(jdbcDriver);


sqlConn = DriverManager.getConnection(sqlDriver,sqlUser,sqlPass);
sqlState = sqlConn.createStatement();
selectQuery = "SELECT picture,width,height,x,y,datanum FROM answer_table";
sqlResult = sqlState.executeQuery(selectQuery);

//while query returns results
while (sqlResult.next() ){

String resultPicture;
float resultWidth=0, resultHeight=0, resultX=0, resultY=0;
int resultDatanum=0;

resultPicture = sqlResult.getString("picture");
resultWidth = sqlResult.getFloat("width");
resultHeight = sqlResult.getFloat("height");
resultX = sqlResult.getFloat("x");
resultY = sqlResult.getFloat("y");
resultDatanum = sqlResult.getInt("datanum");

Answer resultAnswer = new Answer(resultWidth,resultHeight,resultX,resultY);

server.answer.put(resultPicture, resultAnswer);

//DEBUG
System.out.println("Loading picture: " + resultPicture);

updateSuccessful = true;
}

}

catch (SQLException ex){
System.err.println("SQLException: " + ex.getMessage());
System.err.println("VendorError: " + ex.getErrorCode());
ex.printStackTrace();
}

catch (ClassNotFoundException ex){
System.err.println("Unable to locate Driver");
ex.printStackTrace();
}
//close connection
finally{
try{
if(sqlState!=null && sqlConn!=null)
sqlConn.close();
}
catch(SQLException ex){
System.err.println("Unable to close SSL connection to database");
ex.printStackTrace();
}
}
}
});
databaseThread.start();
}

private void setColors(){
splashColors = new Color[12];
splashColors[0] = new Color(255,0,128);//PINK
splashColors[1] = new Color(255,0,0); //RED
splashColors[2] = new Color(255,128,0); //ORANGE
splashColors[3] = new Color(255,255,0);//YELLOW
splashColors[4] = new Color(128,255,0); //LIME GREEN
splashColors[5] = new Color(0,255,0);//GREEN
splashColors[6] = new Color(0,255,128);//AQUA
splashColors[7] = new Color(0,255,255);//CYAN
splashColors[8] = new Color(0,128,255);//LIGHT BLUE
splashColors[9] = new Color(0,255,255);//BLUE
splashColors[10] = new Color(128,0,255);//PURPLE
splashColors[11] = new Color(255,0,255);//MAGENTA
}

public void runFrame(){
JFrame loadFrame;

loadFrame = new JFrame("ZOOM and BOOM - UPDATING SERVER DATABASE");
loadFrame.setContentPane(this);
loadFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
loadFrame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(loadFrame,
"Are you sure you don't want to save new answers?", "Really Closing?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
loadFrame.dispose();
}
}
});
loadFrame.setSize(600, 400);
loadFrame.setLocation(400,200);
loadFrame.setVisible(true);

loadDatabase();

while(!updateSuccessful){
System.out.print("*");
}

loadFrame.dispose();

return;
}
}
25 changes: 16 additions & 9 deletions ZOOM_and_BOOM/src/object/server/ServerUpdateDatabaseWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public void setup() {
textFont(loginFont);
//loadImages
try {
backgroundImg = loadImage("resource/other_images/paintBackground.png");
backgroundImg = loadImage("../resource/other_images/paintBackground.png");
}
catch(Exception ex){
System.err.println("Unable to laod cursor or Backgroun Images");
System.err.println("Unable to laod cursor or Background Images");
ex.printStackTrace();
}
//Wait animation
Expand Down Expand Up @@ -115,15 +115,22 @@ public void run(){
float entryHeight = server.answer.get(key).height;
float entryX = server.answer.get(key).x;
float entryY = server.answer.get(key).y;
int entryDatanum = server.answer.get(key).dataNum;

newEntry = "INSERT INTO answer_table (picture,width,height,x,y) VALUES ('" + key
+ "', " + entryWidth + ", " + entryHeight + ", " + entryX + ", "
+ entryY + ")";

sqlState.executeUpdate(newEntry);
//CREATE
//newEntry = "INSERT INTO answer_table (picture,width,height,x,y,datanum) VALUES ('"
//+ key + "', " + entryWidth + ", " + entryHeight + ", " + entryX + ", "
//+ entryY + "', " + entryDatanum + ")";

//DEBUG
System.out.println("updateEntry: " + newEntry);
//UPDATE
if(! (entryWidth==0 && entryHeight==0 && entryX==0 && entryY==0 && entryDatanum==0)){
newEntry = "UPDATE answer_table SET width=" + entryWidth + ", height="
+ entryHeight + ", x=" + entryX + ", y=" + entryY + ", datanum=" +
+ entryDatanum + " WHERE picture='" + key + "'";
sqlState.executeUpdate(newEntry);
//DEBUG
System.out.println("updateEntry: " + newEntry);
}
}

updateSuccessful=true;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d05fc66

Please sign in to comment.