Add Dialog class a showung on Interaction
This commit is contained in:
parent
1054f548a2
commit
23c12ab711
60
src/main/java/Dialog.java
Normal file
60
src/main/java/Dialog.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -2,6 +2,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class InteractionCell extends Cell {
|
public class InteractionCell extends Cell {
|
||||||
private String message;
|
private String message;
|
||||||
|
private Dialog dialog;
|
||||||
|
|
||||||
InteractionCell(){
|
InteractionCell(){
|
||||||
super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
|
super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
|
||||||
@ -12,8 +13,16 @@ public class InteractionCell extends Cell {
|
|||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InteractionCell(int x, int y, Dialog dialog) {
|
||||||
|
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
|
||||||
|
this.dialog = dialog;
|
||||||
|
}
|
||||||
|
|
||||||
public void startEvent() {
|
public void startEvent() {
|
||||||
|
if (message != null)
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
|
if (dialog != null)
|
||||||
|
dialog.showText();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -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.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.setBackground(Color.BLACK);
|
||||||
dialogBar.setVisible(false);
|
dialogBar.setVisible(false);
|
||||||
|
dialogBar.setLayout(null);
|
||||||
|
|
||||||
inputTimer = new Timer(10, new ActionListener() {
|
inputTimer = new Timer(10, new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -150,7 +151,7 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
|
|
||||||
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
|
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
|
||||||
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, 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"));
|
m1.addInteractionEvent(new InteractionCell(5, 5, "druhá zpráva"));
|
||||||
|
|
||||||
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
|
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user