10 read java core technology (XI) -swing graphics

JFrame
in Java, the frame is designed to hold the container assembly, may be a menu bar, and other user interface elements placed therein. Under normal circumstances, the information should be drawn on the other component, and this component is added to the framework.
JFrame is one of the very few not drawn on the canvas Swing components.
All Swing components must dispatch thread (event dispatch thread) is configured by the event, mouse clicks and button thread will transfer control to the user interface components.
Toolkit class static method call getDefaultToolkit get an Object Toolkit (Toolkit class contains many methods of dealing with the native windowing system). Then, call getScreenSize method, which returns the size of the screen in the form of a Dimension object. Dimension objects simultaneously with public instance variables width and height holds the width and height of the screen.

Toolkit kit = Toolkit
Dimension screenSize = Kit.getDefaultToolkit() ;
int screenWidth = screenSize Dimension screenSize - kit,getScreenSizeQ;
int screenHeight = screenSize.height;

In Java, all drawing must Graphics object, wherein the method comprises a drawing pattern, images, and text. Whatever the reason, as long as the window needs redrawing, the event handler will be advertising component, causing paintComponent method performs all components.

If you need to force a refresh of the screen, you need to call the repaint method instead paintComponent method. Method paintComponent initiator employed will correspondingly configured Graphics object calls all components
2D graphics
Here Insert Picture Description
Each class contains subclasses Float and Double, but save memory space when using Float
using color
using setPaint method may Graphics2D class as on all subsequent graphical environment select a color drawing operations. java.awt.Color class provides 13 predefined constants, may also be provided red, green and blue components to create a Color object, to achieve the purpose of custom colors.
Font
Here Insert Picture Description
rectangle width getStringBounds method returns the width of the string in the horizontal direction. Height of the rectangle is the slope,
the slope, the sum of the line spacing.

Relative paths are relative terms user.dir this directory is the directory when you start a Java program, the eclipse is the root directory of the project.

You can use getResource method of ClassLoader.
new ImageIcon (getClass () getClassLoader ( ) getResource ( "/ images / log.jpg")..);

Published 15 original articles · won praise 1 · views 126

Guess you like

Origin blog.csdn.net/qq_17236715/article/details/105117494