Java plotting mechanism)

Source: Internet
Author: User
JAVA supports a wide range of plotting functions, including fonts, colors, and graphics. We will discuss the following in technical topics.
1. Drawing Mechanism of JAVA.
Any graphical component in JAVA, ranging from text box and label to FRAME and DIALOG, has a function dedicated to displaying its interface. The function name is fixed: paint. Its prototype is:
Public void paint (Graphics g)
{
......
}
This function is used to generate a new graphic interface whenever the component size, position, and content change. Because this function can be inherited by the quilt class, the inherited subclass has the ability to modify this function. If this function does not appear in the subclass, it indicates that its behavior completely inherits from the parent class. No matter whether new content is added to the component, whether the size has changed, or whether the displacement has occurred, the parent class must have a dedicated thread, to describe the Component Interface after the change. The paint function is automatically maintained by the parent class. If the child class reloads the function, it must maintain all interface displays by itself. Ii. Set the paint brush color
1. Color knowledge
Any color is composed of three primary colors (RGB). JAVA supports 224-bit color, that is, the red, green, and blue components can be between 0 and 255. Next we will first learn a JAVA class Color
Constants in Color:
Public final static Color black = new Color (0, 0 );
Public final static Color blue = new Color (0, 0, 255 );
.....
Color constructor:
Public Color (int r, int g, int B );

Example: To construct a gray object, use the following sentence:
Color gray = new color (205,205,205 );

2. Set the paint brush color syntax
G. setcolor (color); // color is a color object.
Each time the color is modified, all the following plot statements are affected. The setcolor function is replaced by a new color.
3. Use the jcolorchooser component to select the color
In Java, there is a color selector that has been defined. with simple syntax, we can call this window and select the color we like from it. The following example uses the color selector to select a color and uses the selected color as the background color of the form.
(1) Introduction to jcolorchooser
The showdialog () method of the jcolorchooser component allows you to select a color from the pop-up window and pass it to the color object. The call syntax is as follows:

Color = JColorChooser. showDialog (this, "color selection", color );
The first parameter specifies the parent form that calls the color selector, the second parameter specifies the color selector window title, and the last one is the color object of the received color.
4. How to draw a graph (with a file, such as JPG or GIF) to the canvas of the form
In fact, the drawImage function of the drawing board is actually called to place images in the drawing board. The general idea is as follows:
First, obtain an ImageIcon object, which reads the relevant image information from the specified file. It supports basic image formats such as GIF, JPG, and BMP. Syntax:
ImageIcon icon = new ImageIcon (GraExp5.class. getResource ("1.gif "));
After obtaining the image icon, you can obtain the actual image drawn to the canvas from the icon:
Image img = icon. getImage ();
With this image object, we can use the drawImage function of the canvas to draw a picture.
G. drawImage (img, 0, 0, null );
The two parameters in the middle are the starting point coordinates of the drawing board when the image is drawn.
For example:

import java.awt.*;import java.awt.event.*;import javax.swing.*; public class GraExp5 extends JFrame{   ImageIcon icon;   Image img;    public GraExp5()   {      icon=new ImageIcon(GraExp5.class.getResource("1.gif"));      img=icon.getImage();      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);      setSize(new Dimension(400,300));      show();   }   public void paint(Graphics g)   {      g.drawImage(img,0,0,null);   }   public static void main(String[] args)   {      GraExp5 ge=new GraExp5();   }}
Program Analysis:
It should be noted that the image file should be located in the same directory as the class, so as not to cause errors. 6. system expansion, comprehensive application, how to set a background image for a form.
To add a background image to a form, you must know the process of drawing the jcomponent component. The drawing of swing lightweight components is the result of cooperation between components and component UI representatives.
Jcomponent. Paint first draws the component, then draws the border of the component, and then draws the child component of the component. Make sure that the component, border, and sub-component are visible in the call sequence. If a component has a UI, jcomponent. paintcomponent calls the update method. This method is used to erase the background of an opaque component and then draw the component.
Cinstead is an opaque component. If the paint method is reloaded, its background image cannot be erased. Therefore, even if all the contained components of the component are updated, it cannot be seen on the interface. Therefore, you must reload the paintcomponent method and erase the background before creating the child component.
For the dual-Cache component, the painting method is responsible for drawing the component into the off-screen cache, and then copying the off-screen cache to the on-screen representation of the component, because of this, we do not recommend that you reload the painting for the swing component. If you need to redefine how to draw the component, You need to reload the paintcomponent ().

7. Modify the text with the available font, style, and font size
(1) Function Description:
The Font class is used to standardize the Font size, style, and Font used by components. Its constructor:
Public Font (String name, int style, int size );
Name indicates that the font name can be obtained locally.
Style indicates the Font style, including Font. PLAIN, Font. BOLD, and Font. ITALIC, which are flattened, BOLD, and ITALIC.

A useful method to obtain available local Fonts
GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment ();
String [] fa = ge. getAvailableFontFamilyNames ();
You can obtain an array of available local fonts from the drawing environment.

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.