Commit 31b7fdbe authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Observer displays guess questions and their results.

parent c71bdc7c
......@@ -81,7 +81,7 @@ public class AnsweredQuestionFragment extends Fragment {
correctAnswerTextView.setText("Correct answer: " + Integer.toString(correct_answer));
TextView ownAnswerTextView = (TextView) getView().findViewById(R.id.OwnAnswerTextView);
int answerInt = playerAnswers.get(own_index).getValue();
if (answerInt != -2) {
if (answerInt >= 0) {
ownAnswerTextView.setText("Your answer: " + Integer.toString(answerInt)
+ " (" + Double.toString(playerAnswers.get(own_index).getTime()) + "s)");
} else {
......@@ -97,9 +97,13 @@ public class AnsweredQuestionFragment extends Fragment {
if (i != own_index) {
final TextView playerAnswerTextView = new TextView(getActivity());
PlayerAnswer answer = playerAnswers.get(i);
playerAnswerTextView.setText(answer.getPlayerName() + ": "
+ Integer.toString(answer.getValue()) + " ("
+ Double.toString(answer.getTime()) + "s)");
if (answer.getValue() >= 0) {
playerAnswerTextView.setText(answer.getPlayerName() + ": "
+ Integer.toString(answer.getValue()) + " ("
+ Double.toString(answer.getTime()) + "s)");
} else {
playerAnswerTextView.setText(answer.getPlayerName() + ": nem válaszolt");
}
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
playerAnswerTextView.setPadding(20,30,20,0);
if (answer.isWinner()) {
......
public interface FontResizable {
public void setFontSize(int fontSize);
public void setFontSize();
}
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class GuessQuestionPanel extends JPanel implements FontResizable {
private JProgressBar progressBar;
private String question;
private int timeOut;
public GuessQuestionPanel(String question, int timeOut) {
this.question = question;
this.timeOut = timeOut;
initialize();
if (timeOut > -1) {
progressBar = new JProgressBar(0, timeOut);
this.add(progressBar, 0);
ProgressBarThread pbt = new ProgressBarThread(progressBar, timeOut);
Thread progressBarThread = new Thread(pbt);
progressBarThread.start();
}
}
private void initialize() {
this.removeAll();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
if (progressBar != null) {
this.add(progressBar);
}
JTextArea questionTextArea = new JTextArea(question);
questionTextArea.setBackground(this.getBackground());
questionTextArea.setBorder(new EmptyBorder(20,20,20,20));
questionTextArea.setEditable(false);
questionTextArea.setWrapStyleWord(true);
questionTextArea.setLineWrap(true);
Font bigFont = questionTextArea.getFont().deriveFont(Main.fontSize);
questionTextArea.setFont(bigFont);
questionTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
this.add(questionTextArea);
}
@Override
public void setFontSize() {
initialize();
this.revalidate();
this.repaint();
}
}
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import model.PlayerAnswer;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GuessQuestionResultPanel extends JPanel implements FontResizable {
private String question;
private int correctAnswer;
private String playerAnswersString;
public GuessQuestionResultPanel(String question, int correctAnswer, String playerAnswersString) {
this.question = question;
this.playerAnswersString = playerAnswersString;
this.correctAnswer = correctAnswer;
initialize();
}
private void initialize() {
this.removeAll();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JTextArea questionTextArea = new JTextArea(question);
questionTextArea.setBackground(this.getBackground());
questionTextArea.setBorder(new EmptyBorder(20,20,20,20));
questionTextArea.setEditable(false);
questionTextArea.setWrapStyleWord(true);
questionTextArea.setLineWrap(true);
Font bigFont = questionTextArea.getFont().deriveFont(Main.fontSize);
questionTextArea.setFont(bigFont);
questionTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
this.add(questionTextArea);
JLabel correctAnswerLabel = new JLabel();
correctAnswerLabel.setFont(bigFont);
correctAnswerLabel.setHorizontalAlignment(JLabel.CENTER);
correctAnswerLabel.setText(Integer.toString(correctAnswer));
this.add(correctAnswerLabel);
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<PlayerAnswer>>(){}.getType();
Collection<PlayerAnswer> playerAnswerCollection = gson.fromJson(playerAnswersString, collectionType);
List<PlayerAnswer> playerAnswers = new ArrayList<PlayerAnswer>(playerAnswerCollection);
for (int i=0;i<playerAnswers.size();i++) {
JLabel playerAnswerLabel = new JLabel();
playerAnswerLabel.setFont(bigFont);
playerAnswerLabel.setMinimumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
playerAnswerLabel.setPreferredSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
playerAnswerLabel.setMaximumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
playerAnswerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
PlayerAnswer answer = playerAnswers.get(i);
if (answer.getValue() >= 0) {
playerAnswerLabel.setText(answer.getPlayerName() + ": "
+ Integer.toString(answer.getValue()) + " ("
+ Double.toString(answer.getTime()) + "s)");
} else {
playerAnswerLabel.setText(answer.getPlayerName() + ": nem válaszolt");
}
if (answer.isWinner()) {
playerAnswerLabel.setForeground(new Color(31, 114, 70));
Font boldFont = bigFont.deriveFont(Font.BOLD);
playerAnswerLabel.setFont(boldFont);
}
this.add(playerAnswerLabel);
}
}
@Override
public void setFontSize() {
initialize();
this.revalidate();
this.repaint();
}
}
......@@ -52,6 +52,8 @@ public class Main {
private static JPanel serverListPanel;
private static FontResizable actualFontResizable = null;
public static float fontSize = 60.0f;
private static class SampleListener implements ServiceListener {
@Override
public void serviceAdded(ServiceEvent event) {
......@@ -270,7 +272,8 @@ public class Main {
null,null);
if (s != null) {
actualFontResizable.setFontSize(Integer.parseInt(s));
fontSize = (float) Integer.parseInt(s);
actualFontResizable.setFontSize();
System.out.println("Új betűméret: " + s);
return;
}
......@@ -343,6 +346,9 @@ public class Main {
break;
case "GameStarted":
exit = false;
mainPanel.removeAll();
mainPanel.revalidate();
mainPanel.repaint();
//majd
break;
case "questionMC":
......@@ -356,6 +362,23 @@ public class Main {
mainPanel.revalidate();
mainPanel.repaint();
break;
case "questionG":
GuessQuestionPanel gqp = new GuessQuestionPanel(params[1], Integer.parseInt(params[2]));
actualFontResizable = gqp;
mainPanel.removeAll();
mainPanel.add(gqp);
mainPanel.revalidate();
mainPanel.repaint();
break;
case "answerG":
GuessQuestionResultPanel gqrp = new GuessQuestionResultPanel(params[1],
Integer.parseInt(params[2]), params[3]);
actualFontResizable = gqrp;
mainPanel.removeAll();
mainPanel.add(gqrp);
mainPanel.revalidate();
mainPanel.repaint();
break;
case "Disconnect":
break;
default:
......
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import javafx.scene.control.ProgressBar;
import model.PlayerAnswer;
import javax.imageio.ImageIO;
......@@ -13,7 +12,6 @@ import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable {
......@@ -22,7 +20,6 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
private static final String RIGHT_ARROW_PATH = "src/images/arrow_right.png";
private JProgressBar progressBar;
private static float fontSize = 60.0f;
private int arrowSize;
private String question;
......@@ -48,42 +45,19 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
initialize();
if (timeOut > -1) {
ProgressBarThread pbt = new ProgressBarThread();
progressBar = new JProgressBar(0, timeOut);
this.add(progressBar, 0);
ProgressBarThread pbt = new ProgressBarThread(progressBar, timeOut);
Thread progressBarThread = new Thread(pbt);
progressBarThread.start();
}
}
class ProgressBarThread implements Runnable {
private long startTime;
public ProgressBarThread() {
startTime = new Date().getTime();
}
@Override
public void run() {
long currentTime = new Date().getTime();
while (currentTime - startTime < timeOut) {
progressBar.setValue((int) (timeOut - currentTime + startTime));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentTime = new Date().getTime();
}
}
}
private void initialize() {
this.removeAll();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
if (timeOut > -1) {
progressBar = new JProgressBar(0, timeOut);
if (progressBar != null) {
this.add(progressBar);
}
JTextArea questionTextArea = new JTextArea(question);
......@@ -92,9 +66,9 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
questionTextArea.setEditable(false);
questionTextArea.setWrapStyleWord(true);
questionTextArea.setLineWrap(true);
Font bigFont = questionTextArea.getFont().deriveFont(fontSize);
Font bigFont = questionTextArea.getFont().deriveFont(Main.fontSize);
questionTextArea.setFont(bigFont);
questionTextArea.setAlignmentX(Component.LEFT_ALIGNMENT);
questionTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
this.add(questionTextArea);
JLabel[] answerLabels = new JLabel[4];
JLabel[] leftArrows = new JLabel[4];
......@@ -104,17 +78,17 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
answers[1] = answer2;
answers[2] = answer3;
answers[3] = answer4;
arrowSize = (int) (192 * fontSize / 200);
arrowSize = (int) (192 * Main.fontSize / 200);
for (int i=0;i<4;i++) {
JPanel answerPanel = new JPanel(new FlowLayout());
answerPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
answerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
leftArrows[i] = new JLabel();
leftArrows[i].setPreferredSize(new Dimension(arrowSize, arrowSize));
answerLabels[i] = new JLabel(answers[i]);
answerLabels[i].setFont(bigFont);
answerLabels[i].setMinimumSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
answerLabels[i].setPreferredSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
answerLabels[i].setMaximumSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
answerLabels[i].setMinimumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
answerLabels[i].setPreferredSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
answerLabels[i].setMaximumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.1 * Main.fontSize)));
answerLabels[i].setHorizontalAlignment(JLabel.CENTER);
rightArrows[i] = new JLabel();
rightArrows[i].setPreferredSize(new Dimension(arrowSize, arrowSize));
......@@ -163,7 +137,7 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
} catch (IOException e) {
e.printStackTrace();
}
if (firstPlayerAnswer != correctAnswer) {
if (secondPlayerAnswer != correctAnswer) {
answerLabels[secondPlayerAnswer].setBackground(Color.RED);
answerLabels[secondPlayerAnswer].setOpaque(true);
}
......@@ -175,17 +149,19 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
for (int i=0;i<playerAnswers.size();i++) {
JLabel playerAnswerLabel = new JLabel();
playerAnswerLabel.setFont(bigFont);
playerAnswerLabel.setMinimumSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
playerAnswerLabel.setPreferredSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
playerAnswerLabel.setMaximumSize(new Dimension((int) (10 * fontSize), (int) (1.1 * fontSize)));
playerAnswerLabel.setHorizontalAlignment(JLabel.CENTER);
playerAnswerLabel.setMinimumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.5 * Main.fontSize)));
playerAnswerLabel.setPreferredSize(new Dimension((int) (10 * Main.fontSize), (int) (1.5 * Main.fontSize)));
playerAnswerLabel.setMaximumSize(new Dimension((int) (10 * Main.fontSize), (int) (1.5 * Main.fontSize)));
playerAnswerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
PlayerAnswer answer = playerAnswers.get(i);
if (answer.getValue() > -1) {
playerAnswerLabel.setText(answer.getPlayerName() + ": "
+ answers[answer.getValue()]);
this.add(playerAnswerLabel);
} else {
playerAnswerLabel.setText(answer.getPlayerName() + ": nem válaszolt");
}
this.add(playerAnswerLabel);
}
}
}
......@@ -203,8 +179,7 @@ public class MultipleChoiceQuestionPanel extends JPanel implements FontResizable
@Override
public void setFontSize(int fontSize) {
this.fontSize = (float) fontSize;
public void setFontSize() {
initialize();
this.revalidate();
this.repaint();
......
import javax.swing.*;
import java.util.Date;
public class ProgressBarThread implements Runnable {
private long startTime;
private JProgressBar progressBar;
private int timeOut;
public ProgressBarThread(JProgressBar progressBar, int timeOut) {
this.progressBar = progressBar;
this.timeOut = timeOut;
startTime = new Date().getTime();
}
@Override
public void run() {
long currentTime = new Date().getTime();
while (currentTime - startTime < timeOut) {
progressBar.setValue((int) (timeOut - currentTime + startTime));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentTime = new Date().getTime();
}
}
}
......@@ -100,6 +100,9 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
Thread newthread = new Thread(commThread);
newthread.start();
}
for (int i=numberOfPlayers;i<outputs.size();i++) {
new SendMessageToClientTask().execute(Integer.toString(i), "GameStarted#normal");
}
}
class CommunicationThread implements Runnable {
......@@ -162,11 +165,12 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
private void calculateGuessQuestionWinner() {
PlayerAnswer bestAnswer = playerAnswers[0];
int indexOfBestAnswer = 0;
for (int i=1;i<playerAnswers.length;i++) {
PlayerAnswer ans = playerAnswers[i];
if (Math.abs(ans.getValue() - correctAnswer) <= Math.abs(bestAnswer.getValue() - correctAnswer)
&& ans.getTime() < bestAnswer.getTime()) {
if (ans.getValue() >= 0 && ((bestAnswer.getValue() < 0) || (Math.abs(ans.getValue() - correctAnswer) <= Math.abs(bestAnswer.getValue() - correctAnswer)
&& ans.getTime() < bestAnswer.getTime()))) {
bestAnswer = ans;
indexOfBestAnswer = i;
}
......@@ -227,6 +231,9 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##-1##" + Integer.toString(timeLimit);
new SendMessageToClientTask().execute(Integer.toString(i), message);
} else {
String message = "questionG##" + questionString + "##" + Integer.toString(timeLimit);
new SendMessageToClientTask().execute(Integer.toString(i), message);
}
}
......@@ -241,6 +248,9 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
@Override
public void correctAnswerButtonClicked() {
gameControlFragment.setCorrectAnswerButtonEnabled(false);
if (answers.size() == 1) {
calculateGuessQuestionWinner();
}
for (int i=0;i<numberOfPlayers;i++) {
if (answers.size() == 4) {
String playerAnswersString = gson.toJson(playerAnswers);
......@@ -249,7 +259,6 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer) + "##-1";
new SendMessageToClientTask().execute(Integer.toString(i), message);
} else {
calculateGuessQuestionWinner();
String playerAnswersString = gson.toJson(playerAnswers);
String message = "answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString + "##" + Integer.toString(i);
......@@ -263,6 +272,11 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(correctAnswer) + "##-1";
new SendMessageToClientTask().execute(Integer.toString(i), message);
} else {
String playerAnswersString = gson.toJson(playerAnswers);
String message = "answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString;
new SendMessageToClientTask().execute(Integer.toString(i), message);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment