Jfc/swing Ingenious's JLabel writing magic

Source: Internet
Author: User
Tags add constructor
Objective

This article describes how to help jfc/swing jlabel components create more colorful applications with Java 2D's "magic".

As one of the basic components of the jfc/swing architecture, JLabel is widely used. When you want to draw non-editable text, JLabel is the only option that Jfc/swing offers us. In general, change the font, font size, font color, and even add icons. You can even add effects such as underscores by applying an HTML language to your component. This is good enough for most applications. But sometimes you may have a further request, such as when you need to drop shadow effects or emboss effects? At this point the standard JLabel is powerless, we have to combine the powerful Java 2D application, extend the function of JLabel, to complete the drop shadow, contour line, even 3D effects such as special effects. Luckily, JLabel's good extensibility makes all of this possible and easy.

Most text effects can be done in two simple ways. First, you can create special effects like drop shadows and reliefs by repeatedly drawing text, a little bit at a time, or a different color at a time. The second can be achieved by adjusting the spacing of characters in a word (which is called a character gap (tracking) in a word-processing system). The character gap is usually added to the default gap of the font. As a result, the character Gap plus 1 means that the gap between each character in a word is added by 1 on the default basis. If you assign the value to 0, the default gap between characters is maintained.

In order to implement the functionality described above, we must overload the JLabel dimension correlation and draw related code, where we create a new extended JLabel class Richjlabel, referring to the following sample code: (For detailed code, please refer to the appendix)

Example code 1:

public class Richjlabel extends JLabel
{
/**
* Character Gap
*/
private int tracking;
/**
* Constructor
*
* @param text
* @param tracking character Gap
*/
Public Richjlabel (String text, int tracking)
{
Super (text);
THIS.TRACKING = tracking;
}

Positioning information for text
private int left_x, left_y, right_x, right_y;

Color information for text
Private Color Left_color, Right_color;

/**
* Set left Shadow
*
* @param x location information
* @param y positioning information
* @param color
*/
public void Setleftshadow (int x, int y, color color)
{
left_x = x;
left_y = y;
Left_color = color;
}

/**
* Set Right Shadow
*
* @param x location information
* @param y positioning information
* @param color
*/
public void Setrightshadow (int x, int y, color color)
{
right_x = x;
right_y = y;
Right_color = color;
}
}


Richjlabel extends the standard Javax.swing.JLabel and adds tracking parameters to the constructor. Next, it adds two methods to draw the left and right shadows. These are called shadows because they are drawn underneath the body text, but they look like shadows, depending on its color, and the X and Y offsets.

JLabel automatically notifies the layout manager that its optimal size depends on the size of the font. When you add custom tracking, the dimensions will become inaccurate, causing the jlabel to be too small to fit the displayed font. For small fonts This is not easy to draw attention to, but for some special display effects of the font (such as advertising words such as fonts are magnified, one word may occupy half a piece of paper or more), we have to find ways to improve.

[1] [2] [3] Next page



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.