package highscores;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
public class HighScoresPane extends TitledPane{
private TextArea scoresText;
private Button closeButton;
public HighScoresPane(){
setText("High Scores");
setContent(createBodyPane());
setCollapsible(false);
}
private Pane createBodyPane(){
scoresText= new TextArea();
scoresText.setEditable(false);
scoresText.setPrefWidth(200);
scoresText.setPrefHeight(170);
closeButton= new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
setVisible(false);
}
});
VBox vbox= new VBox();
vbox.setPadding(new Insets(10,10,10,10));
vbox.setSpacing(10);
HBox hbox= new HBox();
hbox.setAlignment(Pos.CENTER_RIGHT);
hbox.getChildren().add(closeButton);
vbox.getChildren().addAll(scoresText, hbox);
return vbox;
}
public void init(){
try{
String host= "http://www.barvaz.net/";
String urlString= host+"highscores.php";
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder= new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
in.close();
scoresText.setText(stringBuilder.toString());
}
catch(Exception e){
scoresText.setText("Exception: "+e.getMessage());
}
}
}