highlighting button in java with timer

75 views Asked by At

I wanted to highlight the panel,for a specific time (just like in karaoke apps). each panel will have a specific time for highlighting.How to highlight a panel for an example a 3*3 panel like tic tac toe.I searched a lot on stackoverflow and google,but dead end. and can i take help of java .please help me out. I found on how to highlight a word like kareoke, but I didnt know how to replace the word as button panel

This is the code for highlight word:

public class processingTest {

private int[] timings = {2000, 1000, 4000, 2000, 3000};//char timings
private String[] words = {"H", "e", "l", "l", "o"};//each indiviaul word
private String sentence = "Hello";//entire string for writing to JSCrollPane
private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
private int count = 0;
private boolean fisrTime = true;
private JFrame frame;
private JTextPane jtp;
JButton startButton;

public processingTest() {
    initComponents();
}

private void initComponents() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);

    jtp = new JTextPane();

    jtp.setText(sentence);

    startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            startKaraoke();
        }
    });

    frame.add(jtp, BorderLayout.CENTER);
    frame.add(startButton, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

private void startKaraoke() {
    if (fisrTime) {
        startButton.setEnabled(false);
        fisrTime = false;
    }
    new Thread(new Runnable() 
    {
        @Override
        public void run() 
        {

            Timer t = createAndStartTimer(timings[count], count);

            while (t.isRunning()) {//wait for timer to be done
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            SwingUtilities.invokeLater(new Runnable() 
            {
                @Override
                public void run() {
                    count++;
                    if (count == timings.length) {
                        JOptionPane.showMessageDialog(frame, "Done");
                        startButton.setEnabled(true);
                        count = 0;
                        fisrTime = true;
                    } else {
                        startKaraoke();
                    }
                }
            });

        }
    }).start();
}

private Timer createAndStartTimer(int delay, final int count) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            int sp = 0;
            for (int i = 0; i < count; i++) {
                sp += words[i].length();
            }
            try {
                jtp.getHighlighter().addHighlight(sp, sp + words[count].length(), highlightPainter);
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }
    });
    Timer t = new Timer(delay, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            jtp.getHighlighter().removeAllHighlights();
        }
    });
    t.setRepeats(false);
    t.start();
    return t;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new processingTest();
        }
    });
}

}

1

There are 1 answers

0
Developer Marius Žilėnas On

How about some different approach to highlight a word or a part of a sentence? Use JEditorPane with style sheets like in this example.

Highlight example.

Say you have a sentence "Hello world!". To highlight the part "Hello world!" you could use JTextPane with stylesheets (look for HtmlEditorKit#setStyleSheet) like this:

String htmlString = "<body><h1>Karaoke Beyonce song</h1><p><span class="highlight">Hello</span>world</p></body>";

JEditorPane   pane = new JEditorPane();
HTMLEditorKit kit  = new HTMLEditorKit();

pane.setEditable(false);
pane.setEditorKit(kit);

StyleSheet sh = editorKit.getStyleSheet();
sh.addRule("span.highlight {background-color:yellow}");

Document doc = kit.createDefaultDocument();
pane.setDocument(doc);
pane.setText(htmlString);