• 周二. 7 月 16th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Important API of java game programming

King Wang

1 月 5, 2022

java.awt Bag Window class

A form object is a top level (top-level) forms , The form has no borders and menu bars ; The default layout of the form is BorderLayout The way . A form object must have a frame, perhaps Dialog Or another form object . The concept of form object is shown in the figure below :

image-20210414101532301.png

In a multi screen environment , We can construct it in this way Window(Window, GraphicsConfiguration) On different screen devices (screen device) Create a form object in . GraphicsConfiguration Object represents the upper target screen device . In the multi screen environment of a virtual device , The desktop area can span multiple physical screen devices , The boundaries of all devices are relative to the virtual device coordinate system ( See above ). The original coordinates of the virtual coordinate system are higher than those of the upper left corner of the main physical screen . Then the form is positioned according to the coordinates of the home screen in the virtual device .

In this environment , When we call a setLocation When the method is used , A virtual coordinate value must be passed to the method . Again , When we use getLocationOnScreen Method , The form object returns the coordinate value of a virtual device . When we call GraphicsConfiguration Class getBounds When the method is used , We’ll see the source value in the virtual coordinate system . The following code is to set a form in (10,10) Location , This position is relative to the position of the source point of the physical screen . If GraphicsConfiguration I didn’t cross the border , Then the form will be set to (10,10), And this value is the virtual coordinate value , So it will appear on the main physical screen .

The coordinate value is not equal to the specified GraphicsConfiguration Object’s physical screen value . The sample code is as follows :

Window w = new Window(Window owner, GraphicsConfiguration gc);
Rectangle bounds = gc.getBounds();
w.setLocation(10 + bounds.x, 10 + bounds.y);

Be careful : Top level forms ( contain Windows, Frames and Dialogs) The location and size of is controlled by the windows management system of the desktop of the operating system . in other words , We call setLocation、setSize and setBounds When the method is used , We send requests to the window management system of the windows operating system ( Indirect, of course ).

But in some cases , The form management system may ignore such a request , Or modify these geometry requests , So that you can put it in the right place , So as to be able to approach the requirements of desktop settings in the operating system . Because the local event processing of the operating system is asynchronous , therefore getBounds、getLocation、getSize and getLocationOnScreen The returned result may not be on the screen Window The actual geometric value of , Unless all requests are processed . This is because the window management system is processing these sub requests to modify the corresponding request data . For example, we can scale the size of the application form , And the corresponding window management system will adjust the display size value of the window at any time . As a result of this adjustment , There are usually multiple ComponentEvent Event object , To represent the new geometry .Window The following events can occur :

  • WindowOpen
  • WindowClosed
  • WindowGainedFocus
  • WindowLostFocus

This class was created from JDK 1.0 edition .

java.awt Bag Graphics2D class

Inheritance of such kind Graphics class , But it provides more complex control 、 Coordinate transformation 、 Color management and text layout . This class presents two-dimensional space 、 Basic classes of text and pictures .

Coordinate space — All sent to Graphics2D The coordinates of objects are specified in a separate coordinate system , The system is called User Space, The space is used by applications . This class contains AffineTransform object , Used to present a part of the State , This class defines how to convert user coordinates to Device Space The coordinates that the device depends on .

Device space coordinates usually refer to individual device pixels , Then sort and fill between these pixels , To eliminate these gap value . some Graphics2D Objects can be used to capture rendering operations , And then save the graphics metafile file , For playback on unknown physical devices . Because this solution is not known , therefore Graphics2D Realize the transformation of user coordinates into virtual space coordinates . Every Graphics2D Objects are associated with a target , The goal is where it is presented .GraphicsConfiguration Object defines the properties that render the target , Like pixel formats and solutions . stay Graphics2D Use the same rendering target in the object lifecycle . When creating a Graphics2D Object time ,GraphicsConfiguration Specify a default conversion to Graphics2D Object’s target object , such as Component perhaps Image object . This default conversion maps the user’s null coordinate system to the screen or printer device’s coordinate system , among X Increase means that the coordinate moves horizontally to the right , If Y Increase means weight moves straight down . The default conversion unit of the screen is 72 dpi( Pixel values ). Printing is per inch 72 Pixel values . For graphics caching , The same conversion unit is also used .

Rendering process — The process is divided into four stages , The goal is to Graphics2D Render properties to control . The render can be optimized in more steps , Whether it’s caching results , So that it can be used when calling later , You can also decompose an operation into multiple virtual steps . The rendering process is as follows :

  • Decide what to present
  • Constraint rendering operations to the current clip. So-called Clip It’s in user space through a Shape designated , And controlled by the program . Program usage Graphics and Graphics2d Class clip How to present . such user clip adopt Transform Objects are converted to device space . Use of device space device clip And user clip Form a composite clip To decide the final clipping District .User clip Will not be modified by the rendering system , And will only be composite clip The implication is .
  • Decide what colors are presented
  • according to Graphics2D The current of the context Composite attribute , Apply color to the target drawing interface .

There are three rendering operations , Each operation has its own rendering process .

  • Shape operations ( Shape operation )
    • If the operation is a painting (shape) operation , So call method createStokedShape To build a new shape object , This object contains the specified Shape The shape of .
    • The shape comes from user space The switch to device space The shape of the , Use when converting Graphics2d The current context Transform object .
    • The shape of the shape is to use Shape Methods getPathIterator Draw it out , This method returns a PathIterator object , The object is based on Shape We iterate on the boundary of the .
    • If Graphics2D Objects cannot be processed PathIterator Curved section , Then call Shape Of getPathIterator Method .
    • stay Graphics2D In the context of the current Paint need PaintContext object , This context object specifies the color rendering in device space
  • Text operations ( Text operation )
    • The following steps are used to determine the required glyph (glyph) To render the specified string :
    • If the reference is a string , So in Graphics2D The current in context Font Object is required to convert the string of Unicode Characters are a set of glyphs , In order to present the specific glyph together with the shape algorithm of basic layout and character implementation .
    • If it’s a AttributedCharacterIterator object , Then the iterator object is required to transform itself to TextLayout object , Use its built-in font properties when converting .TextLayout Object implements a complex glyph layout algorithm , The algorithm uses Unicode Bi directional layout automatically adjusts multiple Fonts .
    • If it is GlyphVector object , Then the object already contains the corresponding font font code , The code has the position coordinates of each glyph .
    • At present Font Object needs to get the shape of the specified glyph , These shapes are in user space It’s seen as a shape .
    • According to the above Shape operation , The shape of the character is filled into the specified glyph
    • stay PaintContext Object to call the current Paint object , In order to specify device space To present the color of the image .
  • Image Operations ( graphics )
    • Source Image Of bounding box The intersection area is defined , stay Image Space Should be bounding box It’s designated , That is to say Image Object’s local coordinate system
    • If AffineTransform Transmitted to drawImage Method ,AffineTransform Objects are used to extract from image space The switch to user space In the middle . without AffineTransform object , that bounding box Is thought to exist in user space It’s in .
    • Then source Image Of bounding box from user space The switch to device space In the middle , Use the current Transform object . Be careful , stay device space There is no conversion action in the rectangular area of .
    • Image Objects determine which colors are rendered , Then map to the specified current… According to the source and destination coordinates Transform Go to the object , And optimized image transformation .

Default Rendering Attributes—Graphics2D Render property defaults :

  • Paint—Component The color of the Font—Component The font of
  • Stoke— A square pen
  • Transform—Component Of GraphicsConfiguration Object’s getDefaultTransform Method
  • Composite— Express AlphaComposite.SRC_OVER The rules Clip— There’s no presentation Clip when , So it’s time to Component Output. clip value .

Presents compatibility issues —JDK 1.1 The rendering model is based on the pixel model , The model specifies that the coordinates are infinitesimal , That’s the filling between pixels . Drawing is done with a pen one pixel wide , The pen fills pixels one by one according to the drawing path to a specified anchor .JDK 1.1 A rendering pattern is made up of most of the existing classes used by the platform to render , These classes need to convert integer coordinates into discrete pens to complete the specified pixels .Java 2D API Support for rendering technology , One pixel per pen is not fully supported N and N+1 The phenomenon of the gap , So if you set the anti aliasing function , So the render will decide which one to use

Pixels to fill the gap between two pixels .Java 2D API maintain JDK Compatibility of rendering behavior , Like the original legacy Operations and existing rendering behaviors are not in Java 2D API Revision in China .Legacy Methods map the general drawing and filling methods , These methods clearly identify Graphics2D How to inherit Graphic Base class , This class is based on Stroke and Transform attribute . The limited execution step is the default property device , For example, the default Stroke yes BasicStroke class , It’s a 1 The value has no dashed line , And by default Transform The only transfer object drawn to the screen .

Here are the pre rendering behavior rules for aliasing and anti aliasing :

  • The device coordinates are defined to device Between pixels , To avoid inconsistencies in the appearance of anti aliasing that produces aliasing . If the coordinates are defined in the middle of a pixel , Then some parts of the pixel will be covered by a shape , For example, only half of the rectangle is covered . When using sawtooth rendering , Covering half the pixels can shape Inside or outside of . When using antialiasing rendering , Whole shape Half of the border pixels will be covered . On the other hand , Because coordinates are defined between pixels , So a shape Like a rectangle, not half of it is covered , Whether it’s used or not, antialiasing rendering .Line And path usage BasicStoke Objects can be “normalized”( Standardization ) after , Can provide outline Consistency of presentation , Standardization (normalization) The process is through KEY_STROKE_CONTROL To control . And the abstract standardization algorithm is not specified , But its goal is to make sure that line It’s visual consistency . Here’s all legacy The method is based on the default property settings : Filling operation , Include fillRect, fillRoundRect, fillOval, fillArc, fillPolygon and clearRect. such as fill(new Rectangle(x, y, w, h));
  • Same drawing operation , Include drawLine, drawRect, drawRoundRect, drawOval, drawArc, drawPolyline and drawPolygon. draw(new Rectangle(x, y, w, h));
  • Method draw3DRect and fill3DRect Method implemented Graphics Class drawLine and fillRect Method . These methods predefine their behavior , The definition is based on Graphics2D The current in context Stroke and Paint Object is determined by the object . It rewrites the current Paint object , And use fillRect To describe the same abstract behavior .Graphics Class only defines setColor Methods to control the color of inkjet . And in the Java 2D API The package extends Color Object’s , And the new Paint Interface , Existing setColor The method is to set the current Paint Attribute to a Color object ,setColor The method is the same as setPaint Method . Middle class Graphics Two methods are defined to control how color is applied to the target .
    • setPaintMode Method to set the default component , Equate to setComposite (new AlphaComposite) Method
    • setXORMode(Color xorColor) Method is used to set a specific Composite object , And ignore Alpha Source color

Component and set the target color to value :

dstpixel = (PixelOf(scrColor) ^ PixelOf(xorColor) ^ dstpixel);

summary

Java It provides us with a lot of resources for third-party programs API To facilitate the development of , therefore , When we use Java Programming development , There must be a default habit : According to their own development technology needs , Take a serious look first API What can be offered to us , And read these carefully API Function description of .

picture source :
http://www.coubai.com/ Page to swim

发表回复