Add Dialog class a showung on Interaction

This commit is contained in:
iryupnik 2020-03-12 00:54:04 +01:00
parent 1054f548a2
commit 23c12ab711
3 changed files with 72 additions and 2 deletions

60
src/main/java/Dialog.java Normal file
View File

@ -0,0 +1,60 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Dialog {
private String text;
private int textLength;
private Timer timer;
private JPanel dialogBar;
private int charNumber;
Dialog(String text, JPanel dialogBar) {
this.text = text;
textLength = text.length() + 1;
this.dialogBar = dialogBar;
charNumber = 0;
}
private JLabel prepareDialogBar(String string) {
Font f1 = new Font("Times New Roman", Font.PLAIN, 20);
JLabel text = new JLabel();
text.setFont(f1);
text.setForeground(Color.red);
text.setVisible(true);
text.setText(string);
Dimension size = text.getPreferredSize();
text.setBounds(20, 20, size.width, size.height);
return text;
}
private void showByChar() {
String string = text.substring(0, charNumber);
JLabel textToShow = prepareDialogBar(string);
if (dialogBar.getComponentCount() != 0)
dialogBar.remove(0);
dialogBar.add(textToShow);
charNumber++;
if (charNumber == textLength) {
timer.stop();
}
}
public void showText() {
charNumber = 0;
dialogBar.setVisible(true);
timer = new Timer(17, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showByChar();
dialogBar.repaint();
}
});
timer.start();
}
}

View File

@ -2,6 +2,7 @@ import java.awt.*;
public class InteractionCell extends Cell {
private String message;
private Dialog dialog;
InteractionCell(){
super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
@ -12,8 +13,16 @@ public class InteractionCell extends Cell {
this.message = message;
}
InteractionCell(int x, int y, Dialog dialog) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
this.dialog = dialog;
}
public void startEvent() {
System.out.println(message);
if (message != null)
System.out.println(message);
if (dialog != null)
dialog.showText();
}
@Override

View File

@ -71,6 +71,7 @@ public class WindowOfFreeMove extends JFrame {
dialogBar.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4);
dialogBar.setBackground(Color.BLACK);
dialogBar.setVisible(false);
dialogBar.setLayout(null);
inputTimer = new Timer(10, new ActionListener() {
@Override
@ -150,7 +151,7 @@ public class WindowOfFreeMove extends JFrame {
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this));
m1.addInteractionEvent(new InteractionCell(3, 5, "první zpráva"));
m1.addInteractionEvent(new InteractionCell(3, 5, new Dialog("Prvni zprava", dialogBar)));
m1.addInteractionEvent(new InteractionCell(5, 5, "druhá zpráva"));
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));