-1

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);
}
}  
itro
  • 7,006
  • 27
  • 78
  • 121
michdraft
  • 556
  • 3
  • 11
  • 31
  • 2
    You mean, being typed? Or it is about handwriting? – Edward Ruchevits Aug 09 '12 at 17:07
  • 2
    Also, how about trying to explain at least a bit, what you have already tried and why hasn't it worked or why isn't it the desired result. Just coming here to get ideas/examples without providing yours might make some people think you are lazy/uninterested, so why should others do the smallest thing even you can't/won't do? Not trying to be rude here or anything, just trying to help you out get better answers and, at the same time, provide good questions. – Acapulco Aug 09 '12 at 17:38
  • 2
    Try explaining what have you thought about, why hasn't it worked (can't find the right method, don't know how to paint in java, etc), and where specifically is the issue you are facing. – Acapulco Aug 09 '12 at 17:38
  • i have updated my question, please check out? – itro Aug 10 '12 at 11:25
  • +1 for showing your effort :-), though again in this your Character will come up in one go, where as what you should be aiming at, is drawing only a part of it and then another part, hence you have to use many methods like [drawLine(...)](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawLine(int,int,int,int)), [drawArc(...)](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawArc(int,int,int,int,int,int)) together to accomplish this. – nIcE cOw Aug 10 '12 at 11:50

3 Answers3

4

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();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Thanks for your reply, your class is very good for typing a word or sentence but not suitable for a letter like 'A'. i want just one alphabet like A with size 500 be typed as you do with your hand. – michdraft Aug 09 '12 at 18:12
  • Ahha, if I got you write, you want as you write on a piece of a paper, the very same way the Character must come on the screen slowly , instead of a single character coming in one go, only a part of it must come like as if you writing it with a pen or a pencil. Am I right now, as to what your expectations are ? – nIcE cOw Aug 09 '12 at 18:20
  • Yes indeed, you explained it exactly what i means. – itro Aug 10 '12 at 11:03
  • @itro : Though the idea is wonderful, but I am sorry, since it's beyond my knowledge yet, how to accomplish that or my head is really not been able to figure out how to do this. I might can take a bit of time to really put that in action, I hope you get the reply in the meantime, else I will try to provide some idea real soon :-) – nIcE cOw Aug 10 '12 at 11:06
  • thanks for your fast replay. I have created a class and i will put it to my question, may it be a good start.Check it out above. – itro Aug 10 '12 at 11:21
1

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

reggaemuffin
  • 1,188
  • 2
  • 11
  • 26
  • How do you means make a video. do you means capturing a video when i'm writing a alphabet A with my hand? – itro Aug 10 '12 at 11:06
1

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

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
Ewen
  • 1,008
  • 2
  • 16
  • 24