Java imitates kugou to show its lyrics

Source: Internet
Author: User

For the runable jar package and source code, visit this: http://neighborwolf.iteye.com/admin/blogs/1170037

Kugou lyrics show

My simulated results.

After the mouse is selected

Note:

1. If there is no "window", draw the lyrics directly on the desktop.
2. The lyrics are color and gradient. The gradient of singing words is different from that of non-singing words. There is a black border between the lyrics and text, so that the surrounding background can be clearly distinguished.
3. The lyrics can be dragged. When you move the mouse up, the lyrics can be dragged.

The following technical points are available for implementation in Java:
1. transparent window
This needs to be implemented using JNA, through
System. setproperty ("Sun. java2d. noddraw", "true ");
Windowutils. setwindowtransparent (this, true );
Make the window transparent

2. gradient color text. Fill a bufferedimage with gradientpaint. the gradient of bufferedimage is the gradient of the lyrics. Then get the shape of the lyrics,
Use this bufferedimage to fill in. The trouble is the black border of the text. The last method is to offset one pixel to the upper, lower, and lower sides respectively.
Black lyrics, and then draw the normal color gradient lyrics on them, so that the final superposition is exactly what we need.
 

Import Java. AWT. alphacomposite; import Java. AWT. color; import Java. AWT. cursor; import Java. AWT. font; import Java. AWT. fontmetrics; import Java. AWT. gradientpaint; import Java. AWT. graphics; import Java. AWT. graphics2d; import Java. AWT. renderinghints; import Java. AWT. texturepaint; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. mouseadapter; import Java. AWT. event. mousee Vent; import Java. AWT. event. mouselistener; import Java. AWT. event. mousemotionlistener; import Java. AWT. font. fontrendercontext; import Java. AWT. font. textlayout; import Java. AWT. geom. rectangle2d; import Java. AWT. geom. roundrectangle2d; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. filereader; import Java. io. inputstreamreader; import Java. io. linenumberreader; import Java. util. using list; Import Java. util. list; import javax. swing. *; import COM. sun. JNA. native; import COM. sun. JNA. pointer; import COM. sun. JNA. examples. windowutils; import COM. sun. JNA. examples. win32.user32; import COM. sun. JNA. examples. win32.w32api; import COM. sun. JNA. PTR. intbyreference;/*** click it to show the lyrics on the kugou desktop * @ author Li Tao * @ version V1.0 2011-09-11 **. Note the following details: * 1. If there is no "window", draw the lyrics directly on the desktop * 2. The lyrics are color and gradient. The gradient of singing words is different from that of non-singing words. There is a black border for the lyrics and text, so that the surrounding background can be clearly differentiated * 3. The lyrics can be dragged. When the mouse moves up, it will become a scalable shape ** implemented in Java, there are the following technical points: * 1. transparent window * needs to be implemented using JNA, through * system. setproperty ("Sun. java2d. noddraw "," true "); * windowutils. setwindowtransparent (this, true); * Make the window transparent ** 2. Fill in a bufferedimage with gradientpaint, and the gradient of bufferedimage is the gradient of the lyrics. Then, get the shape of the lyrics. * use this bufferedimage to fill in. What is troublesome is the black border of the text. At last, I thought of a method to draw the * Black lyrics by offset to the top, bottom, left, and right, and then draw the normal color gradient lyrics on it, in this way, the final superposition is exactly what we need. */Public class lyncwin extends jdialog {/*****/Private Static final long serialversionuid = 1l; private jlabel infolabel; private myclosebutton closebutton; static list <string> msglist = new shortlist <string> (); Public lyncwin () {settitle ("Karaoke lyrics Demo"); setbounds (300,200,800,110 ); final contentpane Panel = new contentpane (); this. setcontentpane (panel); mymouselistener M = new mymouselistener (this, panel); panel. addmouselistener (m); panel. addmousemotionlistener (m); getcontentpane (). setlayout (null); this. getrootpane (). setopaque (false); closebutton = new myclosebutton (this); closebutton. setopaque (false); closebutton. setbounds (0, 0, 18, 18); closebutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {system. exit (0) ;}}); closebutton. setvisible (false); add (closebutton); setresizable (false); this. setundecorated (true); setdefaultcloseoperation (windowconstants. dispose_on_close); // COM. sun. AWT. awtutilities. setwindowopacity (this, 0.93f); // COM. sun. AWT. awtutilities. setwindowshape (this, new ellipse2d. double (0, 0, getwidth (), getheight (); this. setalwaysontop (true); initmsg (); new timer (30, new actionlistener () {public void actionreceivmed (actionevent e) {lyncwin. this. repaint (); // infolabel. settext (msglist. get (I ++ ));}}). start (); panel. addmouselistener (New mouseadapter () {@ overridepublic void mouseentered (mouseevent e) {closebutton. animateshow (); panel. setcursor (cursor. getpredefinedcursor (cursor. move_cursor);} @ overridepublic void mouseexited (mouseevent e) {closebutton. animatehide () ;}}); system. setproperty ("Sun. java2d. noddraw "," true "); windowutils. setwindowtransparent (this, true); this. setvisible (true);} // Private void makewintransparent () {// pointer winpointer = native. getcomponentpointer (this); // w32api. hwnd = new w32api. hwnd (); // hwnd. setpointer (winpointer); // intbyreference color = new intbyreference (this. getbackground (). getrgb (); // user32.instance. setwindowlong (hwnd, user32.gwl _ exstyle, user32.instance. getwindowlong (hwnd, user32.gwl _ exstyle) | user32.ws _ ex_layered); // user32.instance. setlayeredwindowattributes (hwnd, this. getbackground (). getrgb (), (byte) 220, user32.lwa _ colorkey); // This. setvisible (false); // This. setvisible (true); //} void initmsg () {try {linenumberreader LNR = new linenumberreader (New inputstreamreader (class. class. getresourceasstream ("/MSG. ini "); While (LNR. ready () {msglist. add (LNR. readline () ;}} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} class contentpane extends jpanel {Private Static final long serialversionuid = 1l; int I = 0; // which line of Song text int length = 0; // string MSG = NULL; bufferedimage; // gradient color private color gradientstart = new color (238,254,218 ); private color gradientcenter = new color (153,254, 17); Private color gradientend = new color (232,254, 3 ); // gradient color of unsung text private color gradientendu = new color (14,104, 0); Private color gradientstartu = new color (134,242, 32); Public contentpane () {setfont (new font ("", Font. bold, 40); this. setopaque (false); this. setforeground (this. getbackground () ;}@ overrideprotected void paintcomponent (Graphics g) {If (MSG = NULL) {MSG = msglist. get (0);} graphics2d g2 = (graphics2d) g. create (); renderinghints hints = new renderinghints (renderinghints. key_antialiasing, renderinghints. value_antialias_on); hints. put (renderinghints. key_interpolation, renderinghints. value_interpolation_bilinear); hints. put (renderinghints. key_rendering, renderinghints. value_render_quality); g2.setrenderinghints (hints); g2.setcolor (new color (0, 0); g2.setfont (getfont (); fontmetrics fm = getfontmetrics (getfont ()); rectangle2d rect = FM. getstringbounds (MSG, G2); If (length> rect. getwidth () {length = 0; if (I> = msglist. size () {I = 0; MSG = msglist. get (I ++); fm = getfontmetrics (getfont (); rect = FM. getstringbounds (MSG, G2);} else {MSG = msglist. get (++ I); fm = getfontmetrics (getfont (); rect = FM. getstringbounds (MSG, G2) ;}} int x = 0; int y = 48; // when the close button is visible, it indicates that the mouse is moved up and a translucent shading is drawn, to facilitate user operations (otherwise, the mouse event if (closebutton. isvisible () {graphics2d newg = (graphics2d) g. create (); newg. setcolor (color. gray); newg. setcomposite (alphacomposite. srcover. derive (float) (closebutton. alpha * 0.5); newg. fillroundrect (X, Y-(INT) rect. getheight ()-FM. getdescent () * 3), (INT) rect. getwidth (), (INT) rect. getheight (), 10, 10); newg. dispose ();} // the upper and lower sides of each deviation 1 pixel draw black lyrics, after the color lyrics behind the cover, that is, into the text of the Black contour g2.drawstring (MSG, x-1, y ); g2.drawstring (MSG, x + 1, Y); g2.drawstring (MSG, X, Y + 1); g2.drawstring (MSG, X, Y-1 ); // draw the gradient color lyrics createbufferedimage (FM, rect, length ++); texturepaint TP = new texturepaint (bufferedimage, rect); fontrendercontext FRC = g2.getfontrendercontext (); textlayout TL = new textlayout (MSG, getfont (), FRC); g2.setpaint (TP); g2.translate (0, Y); g2.fill (TL. getoutline (null); g2.translate (0,-y); // g2.drawimage (bufferedimage, null, X, Y + 14); g2.dispose ();} // use gradient color to draw the text protected void createbufferedimage (fontmetrics FM, rectangle2d rectstr, int length) {int width = (INT) rectstr. getwidth (); int Height = (INT) rectstr. getheight ()-FM. getdescent () * 2; bufferedimage = new bufferedimage (width, height, bufferedimage. type_int_rgb); // draw graphics2d g2 = bufferedimage. creategraphics (); gradientpaint painter = new gradientpaint (0, 0, gradientstart, 0, height/2, gradientcenter); g2.setpaint (painter); rectangle2d rect = new rectangle2d. double (0, 0, length, height/2.0); g2.fill (rect); painter = new gradientpaint (0, height/2, gradientcenter, 0, height, gradientend ); g2.setpaint (painter); rect = new rectangle2d. double (0, height/2.0, length, height); g2.fill (rect); painter = new gradientpaint (0, height/2-2, gradientcenter, 0, height, gradientcenter ); g2.setpaint (painter); rect = new rectangle2d. double (0, (height/2.0)-2, length, 4); g2.fill (rect); // draw unsung text painter = new gradientpaint (0, 0, gradientstartu, 0, height, gradientendu); g2.setpaint (painter); rect = new rectangle2d. double (length, 0, width-length, height); g2.fill (rect); g2.dispose () ;}} public static void main (string [] ARGs) {try {uimanager. setlookandfeel (uimanager. getsystemlookandfeelclassname ();} catch (exception e) {e. printstacktrace () ;}new lyncwin ();}}

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.