package ruscz.game.gameOfProd.Dialog; import ruscz.game.gameOfProd.Image.ImageDialog; 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 ImageDialog imageDialog; private Timer timer; private DialogBar dialogBar; private int charNumber; private boolean timerIsRunning; Dialog(String text, DialogBar dialogBar) { this.text = text; textLength = text.length() + 1; this.dialogBar = dialogBar; charNumber = 0; } Dialog(String text, DialogBar dialogBar, ImageDialog imageDialog) { this.text = text; this.imageDialog = imageDialog; textLength = text.length() + 1; this.dialogBar = dialogBar; charNumber = 0; } public Dialog(DialogBar dialogBar) { this.dialogBar = dialogBar; charNumber = 0; } public 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(); if (imageDialog != null) { text.setBounds(120, 20, size.width, size.height); dialogBar.setImageDialog(imageDialog); } else { text.setBounds(20, 20, size.width, size.height); dialogBar.unsetImageDialog(); } 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(20, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showByChar(); dialogBar.repaint(); timerIsRunning = timer.isRunning(); } }); timer.start(); } public void skipShowText() { timer.stop(); timerIsRunning = false; dialogBar.remove(0); dialogBar.repaint(); dialogBar.add(prepareDialogBar(text)); dialogBar.repaint(); } public void hideText() { dialogBar.setVisible(false); dialogBar.repaint(); } public void setText(String text) { this.text = text; this.textLength = text.length() + 1; } public void setImageDialog(ImageDialog imageDialog) { this.imageDialog = imageDialog; } public boolean getTimerIsRunning() { return timerIsRunning; } }