Studyon Minte9.com
Java SE




Text Animation



Write text char by char:

package testexample; import java.awt.Font; import java.awt.event.*; import javax.swing.*; import javax.swing.border.EmptyBorder; class Test extends JFrame { private JTextField textField; private int i = 0; private String str = "I idly turn the leaves"; private javax.swing.Timer timer; public Test() { textField = new JTextField(12); textField.setEditable(false); textField.setFont(new Font("sansserif", Font.PLAIN, 30)); textField.setBorder(new EmptyBorder(0,0,0,0)); JPanel panel = new JPanel(); panel.add(textField); add(panel); // Use full qualification to avoid potential conflicts with java.util.Timer timer = new javax.swing.Timer(100, new ClockListener()); // --- Look Here --- // timer.start(); } public static void main(String[] args) { JFrame test = new Test(); test.setBounds(200, 200, 400, 100); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); } class ClockListener implements ActionListener { // --- Look Here --- // @Override public void actionPerformed(ActionEvent e) { if (++i >= str.length()) timer.stop(); textField.setText(str.substring(0, i)); } } }
  Color one word:
package testexample; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.*; import javax.swing.*; import javax.swing.border.EmptyBorder; class Test extends JFrame { private JLabel label; private int i = 0; private String str = "I idly turn the leaves"; private String s = "idly"; private javax.swing.Timer timer; private int timerStep = 100; public Test() { str = str.replace(s, "<font color='red'>" + s + "</font>"); label = new JLabel(); label.setFont(new Font("sansserif", Font.PLAIN, 30)); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // label align to left panel.setBorder(new EmptyBorder(10,10,10,10)); // padding panel.add(label); add(panel); timer = new javax.swing.Timer(timerStep, new ClockListener()); timer.start(); } public static void main(String[] args) { JFrame test = new Test(); test.setBounds(200, 200, 400, 200); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); } class ClockListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (++i >= str.length()) timer.stop(); String s = str.substring(0, i); String last = s.substring(s.length()-1); if (last.equals("<")) timer.setDelay(0); // --- Look Here --- // if (last.equals(">")) timer.setDelay(timerStep); label.setText("<html><body>" + s + "</body></html>"); // --- Look Here --- // } } }
 


http://stackoverflow.com/questions/5235401/split-string-into-array-of-character-strings