Graphics objects allow applications to perform plotting and plotting rendering) operations. Use the Graphics class to draw the entire screen or a BitmapField. If your application does not contain any field, call Screen. getGraphics) to obtain the drawing context of the entire Screen.
To draw a specified BitmapField, the application passes in a field to the Graphics constructor to obtain a drawing context for a specified field. when a BitmapField is drawn, the field manager transfers a drawing context to the field during field re-painting. To create a custom field, override the Graphics. paint () method when you extend the Field class.
The Graphics class allows you to draw images, such as arcs, straight lines, rectangles, and circles.
Use graphical Context
To draw using the Graphics class, a graphical context is obtained for each field or the entire screen.
To obtain a graphic context for each Field, Graphics constructor is called.
- Bitmap surface = new Bitmap(100, 100);
- BitmapField surfaceField = new BitmapField (surface);
- Graphics graphics = new Graphics(surface);
To obtain a graphical context for the entire Screen, call Screen. getGraphics ).
- Graphics graphics = Screen.getGraphics();
To use any graphical context drawing, you must complete their painting within the boundaries of the field or screen.
- graphics.fillRect(10, 10, 30, 30);
- graphics.drawRect(15, 15, 30, 30);
If your image context is not applied to the entire screen, add BitmapField to the screen.
- mainScreen.add(surfaceField);
Create an interface consistent with the standard BlackBerry UI
The DrawStyle interface provides interfaces for Graphics and Field objects. The implementation of DrawStyle allows you to create an interface consistent with the standard BlackBerry UI. If you are extending the Field class to create a custom Field, your code needs to accept the appropriate style, so it is similar to the standard BlackBerry Application.
DrawStyle is applied to the field as the style parameter, as shown in the following example:
- ButtonField buttonField = new ButtonField(DrawStyle.ELLIPSIS);
You can use the DrawStyle element in the following objects:
◆ BitmapField
◆ ButtonField
◆ DateField
◆ Graphics
◆ LabelField
◆ ObjectListField
Draw with color
Color Rendering is applicable only to BlackBerry devices on the color screen. To determine whether the BlackBerry device supports color display, Call Graphics. isColor (). To determine the color pixels supported by the BlackBerry device, Call Graphics. numColors ).
Set alpha Value
The global alpha value determines the transparency of the pixels in the painting area. 00x0000) is completely transparent and invisible. 2550x00FF) is completely opaque. To set or obtain the global alpha value, Call Graphics. setGlobalAlpha) or Graphics. getGlobalAlpha ).
(Note: BlackBerry uses the alpha value for a specific grating operation. Text and drawing operations are not used .)
Determine the support of grating operations
To determine whether a Graphics object supports a specific grating operation, Call Graphics. isRopSupported (int) and use one of the following constants as the parameter.
Draw a Path)
To draw a set of shadow filling paths, Call Graphics. drawShadedFilledPath ():
- public void
- drawShadedFilledPath(
- int [] xPts,
- int [] yPts,
- byte [] pointTypes,
- int [] colors,
- int [] offsets)
The following example shows a mixed path from blue to red.
- Bitmap surface = new Bitmap(240, 160);
- BitmapField surfaceField = new BitmapField(surface);
- add(surfaceField);
- Graphics graphics = new Graphics(surface);
- int [] X_PTS = { 0, 0, 240, 240 };
- int [] Y_PTS = { 20, 50, 50, 20 };
- int [] drawColors = { 0x0000CC, 0x0000CC, 0xCC0000, 0xCC0000 };
- try {graphics.drawShadedFilledPath(X_PTS, Y_PTS, null , drawColors , null);
- }
- catch (IllegalArgumentException iae){System.out.println("Bad arguments.");}
Use the drawing format
To enable or disable the drawing format, Call Graphics. setDrawingStyle (int drawStyle, Boolean on). Here, on specifies whether to enable true or disable (false) painting format. to determine whether a drawing format has been set, Graphics is called. isDrawingStyleSet (int drawStyle ).
Use a monochrome bitmap field like a print
By submitting opaque areas with colors, the STAMP_MONOCHROME option allows applications to use a monochrome bitmap, just like printing. This option is used for bitmap. This bitmap is 1-bit and has a defined alpha.
- BitmapField field = new BitmapField(original,
- BitmapField.STAMP_MONOCHROME);
Draw an image of data that has never been processed
1. Create an empty bitmap. In this example, both the type and size are copied to an existing bitmap.
2. Use the new bitmap to create a Graphics object as the drawing surface.
3. Call Graphics. rawRGB () and draw a new image using unprocessed data from the source.
- Bitmap restored = new Bitmap(original.getType(), original.getWidth(),
- original.getHeight());
- Graphics graphics = new Graphics(restored);
- try {
- graphics.drawRGB(argb, 0, restored.getWidth(), 0,
- 0, restored.getWidth(),
- restored.getHeight());
- }
- catch (Exception e)
- {
- System.out.println("Error occurred during drawing: " + e);
- }
- }
Use bitmap
(Note: The following information about the bitmap type only provides the type information. The application should not rely on the actual bit format of the bitmap as the format, because the future version of the software on the handheld device may change .)
To determine the Bitmap type, call Bitmap. getType (). This method returns one of the following constants:
◆ On BlackBerry devices with black and white screens, data is stored in columns. Therefore, Bitmap. getType () returns COLUMNWISE_MONOCHROME. The first two bytes represent the first 16 pixels in the first column of the bitmap.
◆ Data is stored in the row on the BlackBerry device of the color screen. Therefore, if itmap. getType () is a black/white image, ROWWISE_MONOCHROME is returned, and ROWWISE_16BIT_COLOR is returned for the color image. In a black-and-white image, the first two bytes represent the first 16 pixels of the first line of the bitmap, from left to right. In a color image, the first two bytes represent the first pixel.
The following two Bitmap constructors allow you to specify a type parameter:
◆ Bitmap (int type, int width, int height)
◆ Bitmap (int type, int width, int height, byte [] data)
To obtain the default bitmap type of the BlackBerry device, call the static method:
Bitmap. getDefaultType ()