How to create writing simulation with java?

1.4k Views Asked by At

I need to come up with a way to make three big letters look like they're being typed/written. I can create Letters with paintComponent();

I need ideas/example on how to accomplish this?

This is what i already done.

public class LetterWriter extends JPanel {
   private String[] alphabets;
private Font font;

public LetterWriter() {
    createComponents();
    layoutComponents();
}

public void createComponents() {
    alphabets = new String[]{"A","B","C"};
    String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    font = new Font(fonts[7],1,500);
}

public void layoutComponents() {
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setFont(font);
    g2d.drawString(alphabets[0],getWidth()/7,getHeight()-50);
}

public static void main(String[] args) {
    LetterWriter demo = new LetterWriter();
    JFrame frame = new JFrame();
    Container cp = frame.getContentPane();
    cp.add(demo);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(500, 500);
    frame.setVisible(true);
}
}  
3

There are 3 best solutions below

1
On

Some ideas:

  • make a video and play it (same with a .gif for example)

  • split the Letter in small pieces and paint these Pieces one after another

  • define a path and let a "pencil" paint itself on a n dynamically created image

1
On

Option 1:

  1. Draw a image on a piece of paper
  2. Take a picture of it and save it in to your computer
  3. Use g.drawImage("TheImageYouTook",0,0,sizex,sizey);

Option 2:

You can choose java fonts but it would be kind of complicated, see this page if the above option does not work for you, http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html

5
On

If you really wanted to display text, which simulates like as if someone is typing, you can use javax.swing.Timer, for this purpose and simply use a JLabel to display the text on the JPanel instead of painting it on the JPanel, since while painting, you have to worry about the Font Metrics and the placement of the said character at a given location, which can be cumbersome. Though if you intend to do something different from what I presented here, please reply likewise.

Here is one example code for your help :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TypingLetters
{
    private String text;
    private JLabel letterLabel;
    private int counter = 0;
    private Timer timer;
    private ActionListener timerAction = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (counter <= text.length())
            {
                letterLabel.setText(text.substring(0, counter));
                counter++;
            }
            else
                timer.stop();
        }
    };

    public TypingLetters()
    {
        text = "A long text that I want to" +
                    " appear as being TYPED :-)";
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Typing Letters Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        letterLabel = new JLabel();
        contentPane.add(letterLabel);

        frame.setContentPane(contentPane);
        frame.setSize(500, 200);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(250, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TypingLetters().displayGUI();
            }
        });
    }
}