Draw 3D text -- Java

Source: Internet
Author: User

Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. graphicsenvironment;
Import java. AWT. image;
Import java. AWT. Panel;
Public class text3dpanel extends panel implements runnable {

Private image; // import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. graphicsenvironment;
Import java. AWT. image;
Import java. AWT. Panel;
Public class text3dpanel extends panel implements runnable {

Private image; // The image object for drawing text
Private graphics; // graphics object for drawing text
Private thread; // display 3D text threads
Private int width, height; // display width and height
Private string message; // display information
Private int fontsize; // text size
Private Font font; // font
Private string [] fonts;
Public text3dpanel (string message, int width, int height ){
This. Message = message;
This. width = width;
This. Height = height;
Init ();
Start ();
}
Public void Init (){
Image = createimage (width, height); // obtain the image instance
// Graphics = image. getgraphics (); // obtain the grahpics instance.
If (Message = NULL) {// if the information is null
Message = "3D text"; // set the default information
}
Fontsize = 30; // set the font size
Graphicsenvironment eq = graphicsenvironment. getlocalgraphicsenvironment ();
Fonts = Eq. getavailablefontfamilynames ();
// Fonts = toolkit. getdefatooltoolkit (). getfontlist ();

}

Public void start (){
If (thread = NULL ){
Thread = new thread (this); // instantiate the thread
Thread. Start (); // The Running thread.
}
}

Public void run () {// thread running entity
While (thread! = NULL ){

Try {
Thread. Sleep (300l); // thread sleep
} Catch (interruptedexception ex ){
}
Repaint (); // repaint the screen
}
}

Public void Update (Graphics g ){
Int Index = (INT) (math. Random () * fonts. Length );
Font = new font (fonts [Index], 1, fontsize); // get the font instance
// Font = new font ("timesroman", 1, fontsize); // get the font instance
G. setfont (font); // set the font.
Int J = (INT) (255 * Math. Random (); // variable used to generate gradient colors
Int K = (INT) (255 * Math. Random ());
Int L = (INT) (255 * Math. Random ());
// Int J = 200;
// Int K = 192;
// Int L = 200;
System. Out. println (J + "," + K + "," + L + "," + index );
G. setcolor (color. Black); // sets the current color.
G. fillrect (0, 0, width, height); // fill the background
// Draws a 6-layer background color string
For (INT I = 0; I <6; I ++) {// 3D depth
G. setcolor (// set the gradient color
New Color (
255-(255-j) * I)/10,
255-(255-k) * I)/10,
255-(255-l) * I)/10 ));
G. drawstring (message, 15-I, height-15-i); // draw a string
}
// Overwrite the string to the screen
G. drawimage (image, 0, 0, this); // draw the image to the screen
}

Public void paint (Graphics g ){
Update (g );
}
} Image object
Private graphics; // graphics object for drawing text
Private thread; // display 3D text threads
Private int width, height; // display width and height
Private string message; // display information
Private int fontsize; // text size
Private Font font; // font
Private string [] fonts;
Public text3dpanel (string message, int width, int height ){
This. Message = message;
This. width = width;
This. Height = height;
Init ();
Start ();
}
Public void Init (){
Image = createimage (width, height); // obtain the image instance
// Graphics = image. getgraphics (); // obtain the grahpics instance.
If (Message = NULL) {// if the information is null
Message = "3D text"; // set the default information
}
Fontsize = 30; // set the font size
Graphicsenvironment eq = graphicsenvironment. getlocalgraphicsenvironment ();
Fonts = Eq. getavailablefontfamilynames ();
// Fonts = toolkit. getdefatooltoolkit (). getfontlist ();

}

Public void start (){
If (thread = NULL ){
Thread = new thread (this); // instantiate the thread
Thread. Start (); // The Running thread.
}
}

Public void run () {// thread running entity
While (thread! = NULL ){

Try {
Thread. Sleep (300l); // thread sleep
} Catch (interruptedexception ex ){
}
Repaint (); // repaint the screen
}
}

Public void Update (Graphics g ){
Int Index = (INT) (math. Random () * fonts. Length );
Font = new font (fonts [Index], 1, fontsize); // get the font instance
// Font = new font ("timesroman", 1, fontsize); // get the font instance
G. setfont (font); // set the font.
Int J = (INT) (255 * Math. Random (); // variable used to generate gradient colors
Int K = (INT) (255 * Math. Random ());
Int L = (INT) (255 * Math. Random ());
// Int J = 200;
// Int K = 192;
// Int L = 200;
System. Out. println (J + "," + K + "," + L + "," + index );
G. setcolor (color. Black); // sets the current color.
G. fillrect (0, 0, width, height); // fill the background
// Draws a 6-layer background color string
For (INT I = 0; I <6; I ++) {// 3D depth
G. setcolor (// set the gradient color
New Color (
255-(255-j) * I)/10,
255-(255-k) * I)/10,
255-(255-l) * I)/10 ));
G. drawstring (message, 15-I, height-15-i); // draw a string
}
// Overwrite the string to the screen
G. drawimage (image, 0, 0, this); // draw the image to the screen
}

Public void paint (Graphics g ){
Update (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.