Advertisement
brilliant_moves

TypewriterJFrame.java

Sep 7th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.47 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TypewriterJFrame extends JFrame implements ActionListener {
  6.  
  7.     /** Application:    TypewriterJFrame.java by Chris Clarke, 07 September 2016.
  8.       * Purpose:    Example of text "typed" automatically in a JFrame TextArea.
  9.       **/
  10.  
  11.  
  12.     Panel       userControls;
  13.     TextArea    ta1;
  14.     Button      btnSet;
  15.     boolean     paint=false;
  16.     final String    txt = "Entering the Dragon's lair,\nI watched as ..."; // or put own text here
  17.  
  18.     TypewriterJFrame() {
  19.         setTitle("Typewriter by Chris Clarke");
  20.         setLayout(new BorderLayout());
  21.         userControls = new Panel();
  22.         ta1=new TextArea("", 2, 35);
  23.         userControls.add(ta1);
  24.  
  25.         btnSet=new Button("Press me!");
  26.         btnSet.addActionListener(this);
  27.         userControls.add(btnSet);
  28.  
  29.         add("North",userControls);
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         TypewriterJFrame frame = new TypewriterJFrame();
  34.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         // Set to a reasonable size.
  36.         frame.setSize(1024, 768);
  37.         frame.setVisible(true);
  38.     }
  39.  
  40.     public void paint(Graphics g) {
  41.         if (!paint) {
  42.             g.setColor(Color.WHITE);
  43.             g.fillRect(0,0,1024,768);
  44.             return;
  45.         }
  46.         String output="";
  47.         for (int c=0; c<txt.length(); c++) {
  48.             output += txt.charAt(c);
  49.             ta1.setText(output);
  50.             for (long num=0; num<50000000; num++); // try other values
  51.         }
  52.     }
  53.  
  54.     public void actionPerformed(ActionEvent e) {
  55.         if (e.getSource() == btnSet) {
  56.             paint=true;
  57.             repaint();
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement