RGB color, in the natural world can see the naked eye any color can be from the red (R), Green (G), Blue (B) These three kinds of color mixed together, so we just increment and decrement to modify its specific value can get the corresponding gradient effect.
Operating effect: (Fig. 1)
After 5 Seconds of operation: (Figure 2)
Java source code:
1 ImportJava.awt.Color;2 Importjava.awt.Dimension;3 ImportJava.awt.Graphics;4 ImportJava.awt.image.BufferedImage;5 6 ImportJavax.swing.JFrame;7 ImportJavax.swing.JPanel;8 9 /**Ten * Picture Display panel class <br> One * Color Gradient effect Test A * - * @authorWQL - * the */ - Public classImagepanelextendsJPanel { - Private Static Final LongSerialversionuid = 1L; - Private intHeight = 350;//Height + Private intwidth = 600;//width - PrivateBufferedImage bufimg =NULL;//draw a color in the BufferedImage object + A /** at * Construction Method - */ - PublicImagepanel () { - This. Setpreferredsize (NewDimension (width, height));//Set Preferred Size - } - in /** - * Initialize Color to */ + Private voidInitcolor () { -Bufimg =NewBufferedImage (width, height, bufferedimage.type_4byte_abgr);//Instantiate BufferedImage theGraphics g = bufimg.getgraphics ();//get the graphics of a picture * intW = WIDTH/6;//divided into six parts to draw $ for(inti = 0; i < W; i++) {Panax Notoginseng intx = 0; - intD = (int) (I * (255.0/w));//increase d from 0 to 255, which may actually be just close to 255 the //draw the first part of the color---red to yellow +G.setcolor (NewColor (255, D, 0));//Set Color AG.drawline (i + w * x, 0, i + W × x + +, height);//Draw Straight Lines---a single color vertical line the //draw the second part of the color---yellow to green +G.setcolor (NewColor (255-d, 255, 0)); -G.drawline (i + w * ×, 0, i + w * x + +), height); $ //draw the third part of the color---green to cyan $G.setcolor (NewColor (0, 255, D)); -G.drawline (i + w * ×, 0, i + w * x + +), height); - //Painting Part IV color---cyan to blue theG.setcolor (NewColor (0, 255-d, 255)); -G.drawline (i + w * ×, 0, i + w * x + +), height);Wuyi //painting part Five color---blue to magenta theG.setcolor (NewColor (d, 0, 255)); -G.drawline (i + w * ×, 0, i + w * x + +), height); Wu //painting Part VI color---magenta to red -G.setcolor (NewColor (255, 1, 255-d)); AboutG.drawline (i + w * ×, 0, i + w * x + +), height); $ } -Repaint ();//Redraw - Try { -System.out.println ("Draw black divider after 5 seconds ..."); AThread.Sleep (5000);//Thread Rest +}Catch(interruptedexception e) { the e.printstacktrace (); - } $ //draw a black line to separate six sections theSystem.out.println ("Start drawing black divider ..."); theG.setcolor (Color.Black);//Set Black the for(inti = 1; i < W; i++) { theG.drawline (i * w, 0, I * w, height);//Draw a line - } inRepaint ();//Redraw the } the About /** the * Draw Pictures the */ the Public voidPaint (Graphics g) { +G.drawimage (bufimg, 0, 0,NULL);//Painting Pictures - } the Bayi /** the * Main Method the */ - Public Static voidMain (string[] args) { -JFrame f =NewJFrame ("Color gradient effect");//Instantiate a form theF.setdefaultcloseoperation (Jframe.exit_on_close);//To set the form off when the program exits theImagepanel Imgpanel =NewImagepanel ();//materialized picture display panel theF.getcontentpane (). Add (Imgpanel);//Add to form theF.pack ();//adjust based on the preferred size of the subform's sub-components -F.setlocationrelativeto (NULL);//set up the form to be centered in the screen theF.setvisible (true);//Show Form theImgpanel.initcolor ();//Draw Color the }94}
Java implementation color Gradient effect