0

Source code (There's many files, can't paste them all here)

GIF of what happens

I am making a quick test game, and am making new commands. One of my commands, "clear", is supposed to be a bit fancy by first making the text dimmer, waiting for awhile, and then clearing it.

My Thread.sleep(1000), which I'm using for the "waiting", seems to prevent me from seeing the text go dimmer. I just immediately hang for a second, and the text disappears. Is there something wrong with my approach?

import java.awt.event.*;

public class Listener implements ActionListener {

Primaire runtime;

Listener(Primaire gRuntime) { runtime = gRuntime; }

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == runtime.input) {
        String command = runtime.input.getText().toLowerCase();

        if (command.equals("look"))
        { runtime.say(runtime.moniteur.surroundings); }

        else if (command.equals("repeat"))
        { runtime.say(runtime.moniteur.levelInfo); }

        else if (command.equals("clear")) {

            try {
                runtime.input.setText("");
                runtime.output.setForeground(new java.awt.Color(80, 80, 80));
                Thread.sleep(1000);
                runtime.output.setText("");
                runtime.output.setForeground(new java.awt.Color(160, 160, 160));
            }   

            catch (InterruptedException ez) { }
        }

        else if (command.equals("")) {

        }

        else if (command.equals("")) {

        }

        runtime.input.setText("");
        runtime.input.requestFocus();
    }

}

}
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    `"My Thread.sleep(1000), which I'm using for the "waiting", seems to prevent me from seeing the text go dimmer. I just immediately hang for a second, and the text disappears. Is there something wrong with my approach?"` -- Indeed. Search on that for many similar questions. Solution: use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Hovercraft Full Of Eels Jul 24 '15 at 00:04
  • 2
    You should never block threads this way when working with UI technologies. Each framework has its own way to approach this, Hovercraft Full Of Eels pointed you to the one for Swing – Dici Jul 24 '15 at 00:07
  • Alright, I'll look it up. Thanks. – SusuKacangSoya Jul 24 '15 at 00:10
  • Also please look at [using sleep() for a single thread](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) as well as [Thread.sleep() to swing Timer conversion](http://stackoverflow.com/questions/24659229), and [How to create a delay in Swing](http://stackoverflow.com/questions/7251625/how-to-create-a-delay-in-swing), and [java thread.sleep puts swing ui to sleep too](http://stackoverflow.com/questions/4215968/java-thread-sleep-puts-swing-ui-to-sleep-too), and [Can't understand Java Swing Timers. How do I make a 1 time delay?](http://stackoverflow.com/q/28209786/522444) – Hovercraft Full Of Eels Jul 24 '15 at 00:12
  • Just learned the timer for swing. Worked perfectly on first try, really easy. Thanks, to both of you. – SusuKacangSoya Jul 24 '15 at 00:16

0 Answers0