Challenge programming Programming Competition Training Manual-1.6.5 Graphical Editor)

Source: Internet
Author: User
Tags getcolor

Challenge programming Programming Competition Training Manual-1.6.5Graphical Editor(Graphical Editor)

Public class problem_graphical_editor {/*** @ Param ARGs * // In my opinion, the difficulty of this program lies in * 1. Operation character F, that is, filling in a region, which requires algorithm support, in this example, you need to pay attention to cross-border processing and full coverage processing. * 2. Variables between code blocks cannot be transmitted to each other. You need to design a global variable to store updates. * (for the first time, I wrote all the methods in the case, and then the result of the calculation is that I cannot wear them out, it hurts a lot! * After the code is changed, only the case operator is used to determine whether the operation is completed immediately and transferred to the method. * add global variables as appropriate to ensure the successful transfer of values) */public static void main (string [] ARGs) {// todo auto-generated method stubstring [] STR = {"I 5 6", // set the string "C ", "l 2 3 A", // "K 4 5 4 5 I", // test K operations "s one.bmp", // "C ", // test the clearing operation "G 2 3 J", // It is not in the Command range and should not be operated./* "H 3 5 2 Z", "L 1 2 Z ", // Test F operations */"F 3 3 J", // "F 4 4 K", // Test F operations again "V 2 3 4 W ", "H 3 4 2 Z", "s two.bmp", "x"}; string get = ""; // defines the string of the truncated character array The variable is blank. Int ROW = 0 is waiting for the input content to be intercepted; // defines the number of image columns int line = 0; // defines the number of image rows. Char [] [] PIC = NULL; // create a two-dimensional empty image for (INT I = 0; I <Str. length; I ++) {// extract string variables one by one get = STR [I]; char r = get. substring (0 ). charat (0); // intercept the first letter and judge the switch (r) operator {// judgment operator. By the way, prepare the variable 'I': system. out. println ("Create a Line X row image -- all pixels are white (-null sign-)");/* trash column and row size */ROW = integer. parseint (get. substring (get. indexof ('') + 1, get. lastindexof (''); line = integer. P Arseint (get. substring (get. lastindexof ('') + 1, get. length ();/* Create an empty image */PIC = new char [Line] [row]; break; Case 'C': system. out. println ("Clear all images -- all pixels are painted white (o), the size remains unchanged");/* clear image = create an empty image, the length and width are the updated line and row */PIC = new char [Line] [row]; break; Case 'l': system. out. println ("pixel (x, y) color (c)");/* capture the X, Y coordinate and color C */INT Ly = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1);/* handle The first space position is the initial position, the ending position is the first space starting from the next position of the first space * and then the truncation character when the first space is removed, continue the above operation until the complete string is intercepted. */Get = get. substring (get. indexof ('') + 1, get. length (); // truncation string, which is 2 3, you can write a method call ~~~~~~~~~~~~~~ Too lazy to update */INT Lx = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); get = get. substring (get. indexof ('') + 1, get. length (); // truncate the string again, which is 3 Achar lc = get. substring (get. indexof ('') + 1, get. length ()). tochararray () [0];/* The last character represents the color. After truncation, you can use tochararray () to convert it to a character array. * Because there is only one character, tochararray () [0] * since the number followed by the operator and the number of characters (not the number of digits) are limited, therefore, we can use the above methods to intercept and determine in sequence * the following methods are all in this way, and we will not repeat the description */system. out. println (Ly + "" + lx + "" + Lc);/* color the image and return the image */PIC = paintlg (PIC, Ly, lx, LC ); break; Case 'V': system. out. println ("draw a vertical line segment-the color is (c), the column number is X, from line Y1 to line Y2 (including lines Y1 and Y2 )"); /* intercept the X, Y, and color C of the colored vertical line. For the intercept algorithm, see Case 'l' */INT Vy = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1 )));// Row X is get = get on the Y axis. substring (get. indexof ('') + 1, get. length (); // continue the second int vx1 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); int vx= integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); char Vc = get. subs Tring (get. indexof ('') + 1, get. length ()). tochararray () [0]; system. out. println (Vy + "" + vx1 + "" + vx+ "" + Vc);/* color the vertical line segment and return the image */PIC = paintvg (PIC, vy, vx1, VX, VC); break; Case 'H': system. out. println ("draw a horizontal line segment-the color is (c), the row number is Y, from row X1 to row X2 (including lines X1 and x2 )"); /* intercept the X, Y, and color C of the colored horizontal line segments. For the intercept algorithm, see Case 'l' */INT hy1 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // get = get on the Y axis of X. substring (Ge T. indexof ('') + 1, get. length (); // or two int hy2 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); int HX = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); char Hc = get. substring (get. indexof ('') + 1, get. Length ()). tochararray () [0]; system. out. println (hy1 + "" + hy2 + "" + HX + "" + HC);/* color the vertical line segment and return the image */PIC = painthg (PIC, hy1, hy2, HX, HC); break; Case 'K': system. out. println ("draw a solid rectangle-the color is (c), the upper left corner is (x1, Y1), and the lower right corner is (X2, Y2 )"); /* intercept the X, Y, and color C of the colored solid rectangle. For the intercept algorithm, see Case 'l' */INT ky1 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // get = get on the Y axis of X. substring (get. indexof ('') + 1, get. length ()); //.................. It's so weak. If you have time, rewrite it into a method ...... There are so many duplicate int ky2 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); int kx1 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); int kx2 = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', Get. indexof ('') + 1); // The y column is on the X axis and get = get. substring (get. indexof ('') + 1, get. length (); char kc = get. substring (get. indexof ('') + 1, get. length ()). tochararray () [0]; system. out. println (ky1 + "" + ky2 + "" + kx1 + "" + kx2 + "" + Kc);/* color the solid rectangle, and return the image */PIC = paintkg (PIC, ky1, ky2, kx1, kx2, KC); break; Case 'F': system. out. println ("fill area R with color (c) -- R is defined as follows: If pixel (x, y) belongs to R, all and (x, y) pixels with common edges and the same color also belong to R ");/* capture the colored X, Y coordinate and color C */int fy = int Eger. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); get = get. substring (get. indexof ('') + 1, get. length (); int FX = integer. parseint (get. substring (get. indexof ('') + 1, get. indexof ('', get. indexof ('') + 1); get = get. substring (get. indexof ('') + 1, get. length (); char fc = get. substring (get. indexof ('') + 1, get. length ()). tochararray () [0]; system. out. println (FY + "" + FX + "" + FC );/ * Fill in area X and Y and return the image */Char color = getcolor (PIC, fy, FX); PIC = paintfg (PIC, fy, FX, FC, row, line, color); break; Case's ': String picname = get. substring (2, get. length (); // capture the file name system. out. println ("Original output file name and pixel matrix of the entire image"); system. out. println (picname); // directly output file name/* output image */showg (PIC); break; Case 'X': system. out. println ("Exit program"); break; default: Break ;}}/ * Get the specified position character (color, the following character is the color) * input a two-dimensional char image, specify the location line number and the position column number * return color character * /Private Static char getcolor (char [] [] PIC, int fy, int FX) {// todo auto-generated method stubchar color = PIC [fy-1] [fx-1]; return color;}/* naming rules for the following methods: paint + operation character code + G * // * This method is used to input a two-dimensional char image, specify the location row number, and specify the location column number, replace character, number of columns, number of lines, character waiting for transformation * returns an updated image composed of two-dimensional characters */Private Static char [] [] paintfg (char [] [] PIC, int fy, int FX, char FC, int row, int line, char color) {// todo auto-generated method stub // define -- if the pixel (x, y) belongs to R, then all and (x, y) have a public Pixels with common edges and the same color also belong to R/* using recursive algorithms: * general idea: * starting from determining the specified position, * the color to be changed is recorded by getcolor, * after changing the character at the current position, * use the character recorded by getcolor to determine the upper, lower, and left adjacent four characters * If the character at a position is the same as that obtained by getcolor, * recursion, similarly, the ** ------------ split line -------- * is complicated because it is out of the border. * If the characters around a certain position exceed the image range, * cannot be modified. Otherwise, an out-of-border error is reported. * My program completely stops recursion at the four corners of the image * because it can completely overwrite the image * For details, see the code below limit */If (FY <= 1 | FY> = line) {// If the append and bottom are attached (in fact, it is enough to directly equal the value, and the program cannot reach the out-of-bounds range. // I just get used to adding a value greater than or less than the range judgment. I wonder if it will affect performance ??) If (FX <= 1 | FX> = row) {// If you paste the left and right sides, that is, to the top, directly judge this box, stop recursion if (PIC [fy-1] [fx-1] = color) {// Update (character) color PIC [fy-1] [fx-1] = FC ;}} else {// otherwise move left and right (just sticking to the upper and lower sides, recursion is performed on the left and right positions) if (PIC [fy-1] [fx-1] = color) {// Update (character) color PIC [fy-1] [fx-1] = FC; paintfg (PIC, fy, fx-1, FC, row, line, color); // left recursion paintfg (PIC, fy, FX + 1, FC, row, line, color ); // recursive to the right} else {// if there is no above and below if (FX <= 1 | FX> = row) {// If the left and right sides are pasted, move up and down if (PIC [fy-1] [fx-1] = color) {// Update (character) color PIC [fy-1] [fx-1] = FC; paintfg (PIC, fy-1, FX, FC, row, line, color); // recursive upward paintfg (PIC, FY + 1, FX, FC, row, line, color ); // recursive down} else {// otherwise move up and down left if (PIC [fy-1] [fx-1] = color) {// Update (character) color PIC [fy-1] [fx-1] = FC; paintfg (PIC, fy, fx-1, FC, row, line, color); // recursive paintfg (PIC, fy, FX + 1, FC, row, line, color); paintfg (PIC, fy-1, FX, FC, row, line, color); paintfg (PIC, FY + 1, FX, FC, row, line, color) ;}} return PIC ;}/ * the following algorithms are very simple, no more comments */Private Static char [] [] paintkg (char [] [] PIC, int ky1, int ky2, int kx1, int kx2, char Kc) {// todo auto-generated method stubfor (INT I = ky1-1; I <= ky2-1; I ++) {for (Int J = kx1-1; j <= ky2-1; j ++) {PIC [I] [J] = KC ;}} return PIC;} Private Static char [] [] painthg (char [] [] PIC, int hy1, int hy2, int HX, char HC) {// todo auto-generated method stubfor (INT I = hy1-1; I <= hy2-1; I ++) {PIC [hx-1] [I] = HC;} return PIC;} Private Static char [] [] paintvg (char [] [] PIC, int Vy, int vx1, int VX, char Vc) {// todo auto-generated method stubfor (INT I = vx1-1; I <= vx2-1; I ++) {PIC [I] [vy-1] = VC;} return PIC;} Private Static char [] [] paintlg (char [] [] PIC, int line, int row, char c) {// todo auto-generated method stubpic [Line-1] [row-1] = C; return PIC ;} private Static void showg (char [] [] pic) {// todo auto-generated method stubfor (INT I = 0; I <PIC. length; I ++) {for (Int J = 0; j <PIC [I]. length; j ++) {system. out. print (PIC [I] [J]);} system. out. println ("");}}}

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.