To reverse the color of a certain area of the screen

Source: Internet
Author: User

If you use a low-level UI, for example, using a button on the canvas, you need to handle the color change after pressing the OK key, for example, reversing the color.

The graphics class has the drawrgb method. This method can be used to set the specified region as a specific color data.

The statement for this method is as follows:

void drawRGB(int[] rgbData,
int offset,
int scanlength,
int x,
int y,
int width,
int height,
boolean processAlpha);

The meanings of parameters are as follows:

Rgbdata: valid data, that is, the color data to be written to a region.

Offset: Start position of valid data in rgbdata

Scanlength: the width of the row to be scanned, that is, the width of the data to be written. This value is generally the same as the following width.

X: The X coordinate at the beginning of the region.

Y: Y coordinate at the beginning of the Region

Width: the width of the area.

Height: the height of the region.

Processalpha: Indicates whether to use Alpha. (If this parameter is set to true or false, no effect is displayed. I don't know what the problem is)

Therefore, after the original data on the screen is retrieved, use 0x00ffffff for XOR to obtain the reverse data, then, you can use drawrgb to re-write the data to the screen to reverse the data.

 

But how to get the original data from the screen is still a little troublesome.

 

Using double buffering is one of the solutions. In fact, you only need to place the image on a global image. You can first create an image and then use the getgraphics method to obtain graphics, then, all the images in the paint are painted on the graphics. Finally, when the paint method is to return, the global image is painted using the drawimage method of graphics at one time.

In this way, the graphics corresponding to the global image can be used elsewhere. There is a getrgb method in the image. Its parameters are similar to the drawrgb in graphics. This getrgb method is used to obtain the original data, reverse the color data, and then write it back, in this way, the color of a region can be reversed.

 

The following is a simple sample code:

Graphics globalg; <br/> image bufferimage; <br/> bufferimage = image. createimage (getwidth (), getheight (); <br/> globalg = bufferimage. getgraphics (); // set globalg to the graphics associated with the image <br/> // The processing in the paint is as follows: <br/> protected void paint (Graphics g) {<br/> // something code here <br/> G. drawimage (bufferimage, 0, 0, graphics. top | graphics. left); // If bufferimage is changed elsewhere, repaint it, then this function will be called <br/>}< br/> // processing code for reversing data <br/> private void reverseprocess (int sel) {<br/> // todo auto-generated method stub <br/> int rgbdata [] = new int [rtwidth * rtheight]; // rtwidth and rtheight are used to change the width and height of the region <br/> bufferimage. getrgb (rgbdata, 0, rtwidth, RTX, rty, rtwidth, rtheight); </P> <p> // reverse each color, then write it back <br/> int num = rtwidth * rtheight; <br/> for (INT I = 0; I <num; I ++) {<br/> rgbdata [I] = rgbdata [I] ^ 0x00ffffff; // reverse the subsequent RGB bits <br/>}</P> <p> // write back <br/> globalg. drawrgb (rgbdata, 0, rtwidth, RTX, rty, rtwidth, rtheight, true); <br/> repaint (); <br/>}

 

BTW ......

Related Article

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.