Java about color additions to paint tools common color reserve areas

Source: Internet
Author: User

To deepen my understanding of object-oriented programming, I want to deepen the concept of object-oriented by instantiating objects with classes of swing packages and AWT packages. So I wanted to write a simple drawing tool.

The currently implemented function is relatively simple, the toolbar has a total of five functional panels, namely: Graphic selection, selection of foreground color, selection of background colors, common color selection, editing colors.

As we all know, the bottom line of the common color selection panel with Windows comes with a blank space that you can customize to add colors.

This line can add the desired color by the color selector that pops up on the right side of the edit Color button, adding a color every time you add it

However, there are only 10 reserved locations, so what happens if you add them again after filling?

At this point, the reserve area has been added to 10, and we add it again

We found that the first color to be added was squeezed out, and 10 of the box is our second add color, the second box is our third color, and so on, the tenth box is our 11th time, that is, the latest color added. So how is this going to happen? (Of course, now that you have contacted the data structure and found that this problem can be solved with a good queue)

First of all

We have built an array of JLabel types for the reserve area, for storing 10 reserved areas

jlabel[] Arraylabel = new jlabel[10];

In the first 10 times we just need to get the color of the color editor and set the background color of the label to that color.

We add a listener to the Edit Color button, which implements ActionListener, and when the button is clicked, the JColorChooser static method ShowDialog is called to get a color (the method returns the selected color)

Color c = jcolorchooser.showdialog (null, "color Editor", Color.Black);

When you get the color, you set the background color.

1 int i = 0; Defines a count variable i, used to record the label array subscript index 2if (i <) {3            Array [I].setbackground (c); // set the label background color to C 4            Array[i].setopaque (true); // set Label to background color visible 5            i++; // Each setting is added one at a time, making the next one set to the next 6 }

And after 10 times, we need to make some changes.

That's what I thought when I wrote this program.

The figure shows an array of colors for storing

Taking into account the size of the picture, we present a 5-length array.

We represent the first 5 colors in 12345 and the new color with 6.

Because we need to use the previous color, so we actually set the previous color, you can add a line of code, the color into a color array

So, we changed the previous code to:

1 if (I <) {2             cfinal[i]=c;//array for storing colors 3            Array[i].setbackground (c) ; 4             Array[i].setopaque (true); 5             i++; 6         }

When adding a new color, we first need to assign the previous array element to a new intermediate array, then reassign the element one by one of the intermediate array to the color array, and change the last one to the newly acquired color, and the code implementation is

1  for (int j = 0; j<9; j + +) {2                 creplace[j]=cfinal[j+1]; 3                 cfinal[j]=Creplace[j]; 4             }5             cfinal[9]=c;

So we get the final color array that sets the background color for the label

After that, it's easy.

1  for (int n = 0; n<10;n++) {2                array[n].setbackground (Cfinal[n]); 3             }

So we re-set the new background color for the tag array.

After adding color to the new

In fact, after understanding the concept of the queue, I think this method is very stupid, but at the beginning of some ideas, I still want to record it.

Java about color additions to paint tools common color reserve areas

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.