SMTH Java FAQ (4) 2D Motors and Text Processing

zhaozj2021-02-11  186

1 How to use double buffering in 2D Graphics?

First of all, you need to know, if you are painting on a swings component, swing is automatically for you.

A double buffer is achieved, so there is no need to go to brain on this issue. //Thank God

If you use the AWT component, you can use dual buffer according to the following process:

Create a BufferedImage object using the CreateImage () method of this component

Appropriate Graphics objects with bufferedimage's CreateGraphics () method

Make pictures on the obtained graphics object

The graphic will be displayed on the Graphics object of the component.

The following code snippet implements the above flow, where g is a graphics object of a graphic display component.

BufferedImage Bi = (BufferedImage) CreateImage (W, H);

Graphics big = bi.creategraphics ();

// Draw Somthing

G.drawImage (Bi, 0, 0, this);

For more information on dual buffering technology, you can refer to 2D Graphics in Java Tutorial.

2 How to get a width and height of a character?

GetHeight (), charwidth () or stringwidth () can be used to take advantage of FontMetrics object

The method is to get the height and width required to display a character or the string. For example, in Graphics G:

String info = "test string".

FontMetrics Metrics = GetFontMetrics (g.getfont ());

INT height = metrics.getHeight ();

INT width = metrics.StringWidth (info);

For details on these methods, please refer to the API documentation for the FontMetrics class.

3. How to load and display a large graphic file?

In The Java Tutorial -> Creating a gui with jfc / swing, we see loaded and in one

The steps to display a graphic file on a Graphics object are as follows:

Toolkit Toolkit = Toolkit.getDefaultToolkit ();

Image image1 = Toolkit.GetImage ("Imagefile.gif");

g.drawImage (MyImage, 0, 0, this);

This approach doesn't seem to have any problems when the graphic files you need is not big. But when you

When I first show a big graphic, I saw that your graphical interface flashed for a long time.

? Is it very depressed? Is Java 2D Graphics not default? Is it used in double buffer technology? Why do you still appear?

This flashes?

The reason for this phenomenon is that the home car of the image object is made through a thread, when the virtual machine is just

When performing the Toolkit.GetImage () statement, the newly created image object is actually updated.

Among them, if you call G.DrawImage () to display the graphic, the image object is updated every time.

The system is reagerged once, which is why the graphical interface will flash.

So, I will do something else, then come to display this graph, I should have it. Try the following paragraph

How is the program?

Toolkit Toolkit = Toolkit.getDefaultToolkit (); Image Image1 = Toolkit.GetImage ("ImageFile.gif");

{

// Do Something for 10 second SECONDS

}

g.drawImage (MyImage, 0, 0, this);

Oh, it seems that it is still not good. Even if you are in the water, I was blocked for 3 months, this hate

Where is it? The reason is very simple, responsible for loading graphics is a low priority thread, you occupy there

Holding a CPU time, how can it work? You don't feel 10 seconds thread.sleep (), is

Not awkward?

Obviously, we can't add a graphic file, just 10 seconds, otherwise the rice bowl will be lost.

Lost. What we need to do is to wait for the image loading to complete, this load is completed to start working immediately. because

This, we need a waitforimage () method. A available WaitforImage () method is as follows, where

Component can be your JFrame object, change your feelings for 10 seconds to waitforimage ()

Request to the boss.

Public Static Boolean WaitforImage (Image Image, Component C)

{

Mediatracker Tracker = New MediaTrracker (C);

Tracker.addImage (image, 0);

Try

{

Tracker.waitForl ();

} catch (interruptedexception e) {}

Return (! tracker.iserroorany ());

}

If you are willing to use the least code to achieve a good effect, then the following code is broken, you can meet your

Claim. As for this reason, I will now typing slowly, you think about it.

Toolkit Toolkit = Toolkit.getDefaultToolkit ();

Image image1 = Toolkit.GetImage ("Imagefile.gif");

Jlabel nulllabel = new imageicon (image1));

g.drawImage (MyImage, 0, 0, this);

4 Can I display Chinese characters on a non-Chinese platform?

The Java program can use the fonts provided by the operating system, so as long as you have a support on your operating system.

You can display Chinese characters in your programs. In order to achieve this, first you have to create one

Font object, and specify the graphics object to use the font, the specific practice is as follows:

String chineseinfo = "Chinese information";

Font sysfont = new font ("fangsong_gb2312", font.tribype_font, 16);

Graphics2D G2 = (Graphics2D) g;

g2.setfont (sysfont);

G2.drawstring (ChineseInfo, 10, 10);

In the exemplary program above, "fangsong_gb2312" is a font name that supports Chinese on the operating system.

This is a Chinese font provided by the Chinese Windows 9x / 2000 / NT operating system default, with the system font directory

Simfang.ttf corresponds to it. If the font is installed into non-Chinese Windows 9x / 2000 / NT operating system

On, the Chinese can also be displayed correctly with the above program.

It should be noted that due to the Chinese information in the block, it is compiled on a non-Chinese platform.

Need to specify an encoding option.

5 How to install Chinese fonts on non-Chinese platforms

This is not a Java issue, but it is related to the above Chinese display problem. The following is only the installation of true fonts

Be a brief description:

5.1 Windows 9x / 2000 / NT platform

On the Windows 9x / 2000 / NT platform, start menu -> Configuration -> Control Panel, Select in the Control Panel

"Font", select "Install the new font" in the file menu, enter the directory where the font file is located, select all

The font and click the "Confirm" button.

5.2 Linux platform

To install and enable true fonts on the Linux platform, you need to have root privileges. Copy all font files

Go to the / usr / share / fonts / default / trueType directory. Run the following two commands:

TTMKFDIR> FONTS.DIR

Cat fonts.dir> fonts.scale

Rewind / etc / x11 / xf86config-4 to include the following information:

Section "fontpath"

...

Fontpath "/ usr / share / fonts / default / trueType"

Endsection

Section "Module"

...

LOAD "XTT"

Endsection

Restart the machine or the font server. On some platforms, you may need to close the XFS server.

5.3 Solaris Platform

Run / USR / DT / BIN / SDTFONTADM enters the font management graphical user interface. Select the installation in the font menu.

Select the directory where the true font file is located in the subsequent dialog box, then select the font that you want to install. installation

After the font is confirmed, the installation directory is automatically added to the FontPath.

6 Where to find a letter to support Chinese?

If you have a Chinese Windows 9x / 2000 / NT operating system, then your Java program can be in non-

WIDOWS 9X / 2000 / NT, Linux, Solaris system shows Chinese. In Windows / FONTS

Recorded the following file and follow the prompts to be installed on the target platform.

Simhei.ttf - black body (SIMHEI)

Simfang.ttf - imitation Song (Fangsong_GB2312)

Simkai.ttf - 体 (kaiti_gb2312)

SIMSun.ttf - Song Song (SIMSUN, NSIMSUN)

MSSONG.TTF - Song (MS SONG, not recommended)

7 How to make the text you have different background?

At present, Java's DrawString () method uses the background color of the current Graphics object by default as the back of the text.

The color of the scenery, and does not provide a special method to change this setting. Therefore, we can't draw it directly.

Words as background. A viable method is to obtain the height and width of the text information, draw first in the proper position

Out of the background color, then draw the same position.

A process that implements this method is as follows:

Color backcolor = color.red, forecolor = color.blue;

INT CURRX = 10, curry = 10;

String info = "test string".

FontMetrics Metrics = GetFontMetrics (g.getfont ()); int.com = metrics.getHeight ();

INT width = metrics.StringWidth (info);

G2.SETPAINT (Backcolor);

G2.FillRect (Currx, Curry - Height, Width, Height);

G2.SETPAINT (Forecolor);

g2.drawstring (currx, curry);

8 How do I accept the keyboard input in the mapping area?

A possible way is to use Keylistener to monitor keyboard activities. Get the phase when the keyboard activity is detected

The corresponding character is displayed to the appropriate location of the status area. In order to achieve this, we need to define your own

KeyPressed () event, for example:

Int Currx, Curry;

Public void keypressed (KeyEvent E)

{

CHAR C = E.GetKeychar ();

g.drawstring (" C, CURRX, CURRY)

}

It should be noted that this method cannot be input to the mapping area. If any netizen finds the input

In Chinese, please advise.

9 How to achieve a shadow, hollow, discrete special character?

The following is from

Http://www.javaworld.com/javaworld/javatips/jw-javatip81.html get

Some code snippets that implement special character effects. Interested netizens can go to the original text.

// shadow

G.SetColor (New Color (50, 50, 50));

g.drawstring ("Shadow", Shifteast (x, 2), ShiftSouth (Y, 2));

G.SETCOLOR (New Color (220, 220, 220);

g.drawstring ("shadow", x, y);

//Engrave

G.SETCOLOR (New Color (220, 220, 220);

g.drawstring ("ENGRAVE", Shifteast (x, 1), ShiftSouth (Y, 1));

G.SetColor (New Color (50, 50, 50));

g.drawstring ("ENGRAVE", X, Y);

FILE: // Outline

g.setColor (Color.Red);

g.drawstring ("Outline", Shiftwest (x, 1), Shiftnorth (Y, 1));

g.drawstring ("Outline", ShiftWest (x, 1), ShiftSouth (Y, 1));

g.drawstring ("Outline", Shifteast (x, 1), Shiftnorth (Y, 1));

g.drawstring ("Outline", Shifteast (x, 1), ShiftSouth (Y, 1));

g.setcolor (color.yellow);

g.drawstring ("Outline", X, Y);

File: // hollow

g.setcolor (color.ble);

g.drawstring ("Hollow", Shiftwest (x, 1), Shiftnorth (Y, 1));

g.drawstring ("Hollow", Shiftwest (x, 1), ShiftSouth (Y, 1));

g.drawstring ("Hollow", Shifteast (X, 1), Shiftnorth (Y, 1)); g.drawstring ("Hollow", Shifteast (x, 1), ShiftSouth (Y, 1));

G.SetColor (BG);

g.drawstring ("Hollow", X, Y);

File: // segment

INT W = (g.getFontMetrics ()). StringWidth ("segment");

INT H = (g.getfontmetrics ()). GetHeight ();

INT D = (g.getfontmetrics ()). getdescent ();

G.SETCOLOR (New Color (220, 220, 220);

g.drawstring ("segment", x, y);

G.SetColor (BG);

For (int i = 0; i

g.drawline (x, y d - i, x w, y D - i);

FILE: // 3D Effects

Color Top_color = New Color (200, 200, 0);

Color Side_color = New Color (100, 100, 0);

For (int i = 0; i <5; i )

{

g.setColor; Top_color

g.drawstring ("3-Dimension", Shifteast (X, I), Shiftnorth (ShiftSouth (Y, I), 1);

g.setcolor; Side_Color;

g.drawstring ("3-Dimension", Shiftwest (Shifteast (X, I), 1), ShiftSouth (Y, II));

}

g.setcolor (color.yellow);

g.drawstring ("3-Dimension", Shifteast (x, 5), ShiftSouth (Y, 5));

File: // motion

For (int i = 0; i <20; i )

{

Font_size = 12 i;

G.SetFont (New Font ("Timeesroman", font.plain, font_size);

w = (g.getfontmetrics ()). StringWidth ("motion");

G.SetColor (New Color (0, 65 i * 10, 0);

g.drawstring ("Motion", (Width - W) / 2, ShiftSouth (Y, Speed ​​* i));

}

转载请注明原文地址:https://www.9cbs.com/read-5218.html

New Post(0)