A new class of Java 7-jlayer: decorated swing Components

Source: Internet
Author: User

A new class in Java 7, jlayer, has been added to the improved Java swing capability. Jlayer is mainly used for decorative swing components. You can use it to change how components are drawn and how they respond to events. The following is a simple example of this class:

Check the running result first:

1. When the mouse enters the login box:Spotlight)


2. Click the logon button:

Import Java. AWT. awtevent; import Java. AWT. alphacomposite; import Java. AWT. basicstroke; import Java. AWT. color; import Java. AWT. composite; import Java. AWT. font; import Java. AWT. graphics; import Java. AWT. graphics2d; import Java. AWT. image; import Java. AWT. point; import Java. AWT. radialgradientpaint; import Java. AWT. renderinghints; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. aw T. event. focusadapter; import Java. AWT. event. focusevent; import Java. AWT. event. itemevent; import Java. AWT. event. itemlistener; import Java. AWT. event. mouseevent; import Java. beans. propertychangeevent; import javax. swing. imageicon; import javax. swing. jbutton; import javax. swing. jcheckbox; import javax. swing. jcomponent; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jlayer; import javax. SW Ing. jpanel; import javax. swing. jpasswordfield; import javax. swing. jtextfield; import javax. swing. swingutilities; import javax. swing. timer; import javax. swing. uimanager; import javax. swing. plaf. layerui;/*** @ author http://blog.csdn.net/Cannel_2020 ***/public class testjlayer {Private Static jframe loginframe; private loginlayerui layerui; // a subclass of layerui public static void main (string [] ARGs) {try {uimanag Er. setlookandfeel ("com. sun. java. swing. plaf. nimbus. nimbuslookandfeel ");} catch (exception e) {e. printstacktrace ();} javax. swing. swingutilities. invokelater (New runnable () {public void run () {New testjlayer (). createui () ;}}) ;}public void createui () {loginframe = new jframe ("from cannel_2020's blog (csdn)"); loginframe. setsize (340,260); loginframe. setdefaclocloseoperation (jframe. exit_on_close); loginframe. Setlocationrelativeto (null); jpanel = createpanel (); layerui = new loginlayerui ();/** jlayer constructor: jlayer (V view, layerui <v> UI ), * The first parameter view is a subclass of component, which is the swing component to be decorated * The second parameter is an instance of layerui <v>, specify */jlayer <jpanel> jlayer = new jlayer <jpanel> (panel, layerui); // note that this add is an instance loginframe of jlayer <jpanel>. add (jlayer); loginframe. setvisible (true);} private jpanel createpanel () {final image background = New imageicon ("image/background.jpg "). getimage (); // background image jpanel = new jpanel () {Private Static final long serialversionuid = 6246862165441423926l; Public void paintcomponent (Graphics g) {super. paintcomponent (g); graphics2d g2d = (graphics2d) g; g2d. drawimage (background, 0, 0, loginframe. getwidth (), loginframe. getheight (), this) ;}}; panel. setlayout (null); font = new font ("", Font. plain, 14 ); Jlabel accountlabel = new jlabel ("account:"); accountlabel. setfont (font); accountlabel. setbounds (40, 60, 40, 15); jlabel pswlabel = new jlabel ("Password:"); pswlabel. setfont (font); pswlabel. setbounds (40, 98, 40, 15); Final jtextfield username = new jtextfield (); username. setfont (font); username. setbounds (88, 53,155, 33); username. addfocuslistener (New focusadapter () {public void focusgained (focusevent e) {username. sel Ectall () ;}}); jpasswordfield Password = new jpasswordfield (); password. setbounds (88, 90,155, 33); jlabel registerlabel = new jlabel ("register"); registerlabel. setfont (font); registerlabel. setbounds (260, 60, 28, 19); jlabel findbacklabel = new jlabel ("retrieve"); findbacklabel. setfont (font); findbacklabel. setbounds (260, 98, 28, 19); jcheckbox rmbcbox = new jcheckbox ("Remember password"); rmbcbox. setselected (true); rmbcbox. setbounds (90,140, 77, 14); rmbcbox. additemlistener (New itemlistener () {public void itemstatechanged (itemevent e) {If (E. getstatechange () = itemevent. deselected) {system. out. println ("rmbcbox (jcheckbox) is selected! ") ;}}); Jcheckbox autocbox = new jcheckbox (" Automatic Logon "); autocbox. setbounds (175,140, 77, 14); jbutton setbutton = new jbutton ("set"); setbutton. setbounds (50,170, 75, 27); jbutton loginbutton = new jbutton ("login"); loginbutton. setfont (font); loginbutton. setbounds (90,170, 75, 27); // after 5 seconds, stop final timer stopper = new timer (5000, new actionlistener () {public void actionreceivmed (actionevent AE) {layerui. stop () ;}}); s Topper. setrepeats (false); // set the timer to be triggered only for the first time and then stop loginbutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent AE) {layerui. start (); // If stopper is not running, start stopper. If (! Stopper. isrunning () {stopper. start () ;}}); jbutton exitbutton = new jbutton ("exit"); exitbutton. setfont (font); exitbutton. setbounds (170,170, 75, 27); exitbutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {system. exit (0) ;}}); panel. add (username); panel. add (password); panel. add (accountlabel); panel. add (pswlabel); panel. add (registerlabel); panel. add (findbacklabel); panel. ad D (rmbcbox); panel. add (autocbox); panel. add (loginbutton); panel. add (exitbutton); Return Panel;} class loginlayerui extends layerui <jpanel> implements actionlistener {Private Static final long serialversionuid = 1l; private Boolean misrunning = false; // whether the thread triggered after clicking the button is running private Boolean misfadingout = false; // stop the control thread and process private timer mtimer; // trigger one or more actionevents at a specified interval. Private int mangle = 0; // used to calculate the rotation angle when rotating private int mfadecount = 0; // use mfadelimit to set the gradient effect of the entire needle-shaped rotating pattern private int mfadelimit = 30; private Boolean mactive; // whether to enter private int MX, my In the window; // record the coordinates of the mouse. During the rendering process, the paint */Public void paint (Graphics g, jcomponent c) {int W = C. getwidth (); // get the width of component C int H = C. getheight (); super. paint (G, C); // call the painting method of the parent class. The painting component cgraphics2d g2 = (graphics2d) g. create (); // set the spotlight (spotli Ght) effect if (mactive) {// create a radial gradient, transparent in the middle. Java. AWT. geom. point2d center = new Java. AWT. geom. point2d. float (MX, my); float radius = 72; float [] Dist = {0.0f, 1.0f}; color [] colors = {new color (0.0f, 0.0f, 0.0f, 0.0f), color. black}; // The radialgradientpaint class provides radialgradientpaint P = new radialgradientpaint (center, radius, DIST, colors) by filling a shape in the circular radiation color gradient mode; // setpaint: set the paint attribute g2.setpaint (p) for the graphics2d context;/* Set composite for the graphics2d context. * Composite is used in all painting methods, such as drawimage, drawstring, draw, and fill * Here we use the alphacomposite class to implement some basic Alpha synthesis Rules * combine the source and target colors, blending and transparency in graphics and images */g2.setcomposite (alphacomposite. getinstance (alphacomposite. src_over ,. 6f); // fill is the painting g2.fillrect (0, 0, C. getwidth (), C. getheight ();} If (! Misrunning) {return;} float fade = (float) mfadecount/(float) mfadelimit; // different fade forms: the gradient effect of the entire 12 needles, the gradient effect g2.setcomposite (alphacomposite. getinstance (alphacomposite. src_over ,. 5f * Fade); g2.fillrect (0, 0, W, H); int S = math. min (W, h)/5; int Cx = W/2; int Cy = H/2;/* sets a single preference value for the rendering algorithm. * Renderinghints. key_antialiasing: Specifies the anti-aliasing prompt key. * Renderinghints. value_antialias_on: Specifies the value of the error code returned when the image is displayed in the anti-aliasing mode */g2.setrenderinghint (renderinghints. key_antialiasing, renderinghints. value_antialias_on);/** setstroke: set the graph * basicstroke class to be drawn during rendering to define a basic set of Graphic Element Profile rendering attributes */g2.setstroke (New basicstroke (S/4, basicstroke. cap_round, basicstroke. join_round); // sets the painting color g2.setpaint (color. white);/* rotate: the first parameter of the rotation conversion link * Math. pI * mangle/180: rotation angle, in radians * second parameter CX: X-x coordinate of the rotation origin * second parameter Cy: Y coordinate of the Y-rotation origin */g2.rotate (math. pI * mangle/180, CX, CY); // 12 with the needle, the color of each needle is different, the gradient effect between the needle and the needle for (INT I = 0; I <12; I ++) {float scale = (11.0f-(float) I)/11.0f; g2.drawline (cx + S, Cy, cx + S * 2, CY ); g2.rotate (-math. PI/6, CX, CY); // set alphacomposite. the second parameter of getinstance () is different to realize different g2.setcomposite (alphacomposite. getinstance (alphacomposite. src_over, scale * Fade);} // release Place the context of the image and all system resources it uses. G2.dispose ();} public void installui (jcomponent c) {super. installui (c); jlayer = (jlayer) C; // start the mouse event and move the mouse event jlayer. setlayereventmask (awtevent. mouse_event_mask | awtevent. mouse_motion_event_mask);} public void uninstallui (jcomponent c) {jlayer = (jlayer) C; jlayer. setlayereventmask (0); super. uninstallui (c);} // A jlayer object-specific mouse event, which starts an event in installui and disables the event protected void processmouseevent in uninstallui (Mouseevent E, jlayer L) {If (E. GETID () = mouseevent. mouse_entered) mactive = true; If (E. GETID () = mouseevent. mouse_exited) mactive = false; L. repaint ();} // A jlayer object-specific mouse movement event to start the event in installui, disable events in uninstallui // record the mouse coordinates in MX and my protected void processmousemotionevent (mouseevent E, jlayer L) {// determine the coordinates of the mouse movement event relative to the layer. Point P = swingutilities. convertpoint (E. getcomponent (), E. getpoint (), L); MX = P. x; my = P. y; L. repaint ();} public void actionreceivmed (actionevent e) {If (misrunning) {// firepropertychange (): Update firepropertychange ("tick", 0, 1); mangle + = 3; if (mangle> = 360) {mangle = 0;} If (misfadingout) {If (-- mfadecount = 0) {misrunning = false; mtimer. stop ();} // prevents alphacomposite. the second parameter value of getinstance () is out of range} else if (mfadecount <mfadelimit) {mfadecount ++ ;}} public void start () {If (misrunning) {return;} misrunning = true; misfadingout = false; mfadecount = 0; int FPS = 24; int tick = 1000/FPs; // call the actionreceivmed method mtimer = new timer (tick, this); mtimer every tick millisecond. start () ;}public void stop () {misfadingout = true;} // constant repaint () window public void applypropertychange (propertychangeevent PCE, jlayer L) {If ("tick ". equals (PCE. getpropertyname () {L. repaint ();}}}

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.