Implementation of verbatim display of dialogs in Java graphics programs

Source: Internet
Author: User

In the modern era, the traditional mode of conversation is abandoned. Many graphic advertisements or games use [print words] to display text. Of course, this is nothing more than to highlight content or attract attention.

However, this implementation method is worth learning from. below, I will provide a simple JAVA implementation case, which can be expanded by the compiler.

Code implementation:

Package org. test;

Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Frame;
Import java. awt. Graphics;
Import java. awt. Image;
Import java. awt. Panel;
Import java. awt. image. BufferedImage;

Import org. loon. framework. game. helper. ImageHelper;

/***//**
* <P>
* Title: LoonFramework
* </P>
* <P>
* Description: Implements wordwise printing of conversations in JAVA (provided by Loonframework)
* </P>
* <P>
* Copyright: Copyright (c) 2007
* </P>
* <P>
* Company: LoonFramework
* </P>
*
* @ Author chenpeng
* @ Email: ceponline@yahoo.com.cn
* @ Version 0.1
*/

Public final class WriterType extends Panel implements Runnable ...{
// Maximum number of dialogs
Private int _ MAX_COUNT = 10;

// Interval mark
Private char _ flag = |;

Private Image _ screen;

Private Graphics _ bg;

Private Thread _ L_WriterType;

Private static Color _ L_fClr;

Private static Color _ L_bClr;

Private static String _ M_Text [];

Private int _ S_COUNT;

Private Font _ L_TTF;

Private int _ L_Speed;

Private int _ L_Delay;

Private String _ C_Text [];

Private static int _ L_COUNT;

Private static int _ F_COUNT;

Private static int _ T_COUNT;

Private static int _ N_Text;

Private String _ FontName;

Private final static int _ WIDTH = 500;

Private final static int _ HEIGHT = 375;

Private static int _ FontStyle;

Private static int _ FontSize;

Private static Image _ back = null;

/***//**
*
*/
Private static final long serialVersionUID = 1L;

Private boolean _ isRun;

/***//**
* Destructor, injection of background images, array of text displayed cyclically, speed, and latency
* @ Param img
* @ Param strContent
* @ Param speed
* @ Param delay
*/
Public WriterType (Image img, String [] strContent, int speed, int delay )...{
Init (img, strContent, speed, delay );
}

Private WriterType ()...{

}

Public void init (Image img, String [] strContent, int speed, int delay )...{
// Initial background color
SetBackground (Color. BLACK );
// Original dialog information
_ M_Text = new String [_ MAX_COUNT];
// Cache conversation Information
_ C_Text = new String [_ MAX_COUNT];

_ Back = img;
_ FontName = "";

_ FontStyle = 0;

_ FontSize = 15;

_ L_TTF = new Font (_ FontName, _ FontStyle, _ FontSize );
_ L_bClr = Color. BLACK;
_ L_fClr = new Color (250,250,250 );

Int length = strContent. length;

// Number of cycles
_ N_Text = strContent. length-1;

// Directly copy the String Array
System. arraycopy (strContent, 0, _ M_Text, 0, length );

// Set the speed and latency
_ L_Speed = speed;
_ L_Delay = delay;

// Cache Region
_ Screen = new BufferedImage (_ WIDTH, _ HEIGHT, 1 );
_ Bg = _ screen. getGraphics ();

// Enable the thread
_ IsRun = true;
_ L_WriterType = new Thread (this );
_ L_WriterType.start ();

}

/***//**
* Stop a thread
*
*/
Public void stop ()...{
_ S_COUNT = 0;
_ L_COUNT = 0;
_ F_COUNT = 0;
If (_ L_WriterType! = Null )...{
_ IsRun = false;
_ L_WriterType = null;
}
}

/***//**
* Drawing Images
*/
Public void paint (Graphics g )...{
_ Bg. setColor (_ L_bClr );
_ Bg. fillRect (0, 0, _ WIDTH, _ HEIGHT );
_ Bg. setColor (_ L_fClr );
_ Bg. setFont (_ L_TTF );
_ Bg. drawImage (_ back, 0, 0, this );
For (int I = 0; I <= _ L_COUNT; I ++ )...{
// Convert the top position of the current font
Double top = new Double (double) _ FontSize
* (3.0D + 2.0D * (double) I ));
If (_ C_Text [I]! = Null )...{
_ Bg. drawString (_ C_Text [I], 50, top. intValue ());
}
}

G. drawImage (_ screen, 0, 0, this );
}

/***//**
* Graphic change
*/
Public void update (Graphics g )...{

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.