Swing exploration: Creating menus with images

Source: Internet
Author: User

On the "Start" menu of windows, an image is displayed on the left side of the menu. Many Windows-based software have similar menus. Can I create menus with similar effects using Java swing? The answer is certainly yes, and it is very simple.

We first scale out the jpopupmenu component of swing to make it accept an image and display it on the left; or accept a string, dynamically generate the image, and then display it on the left. To avoid the trouble of preparing images, we will use dynamic memory image generation as an example to compile a jimagedpopupmenu class.

Jimagedpopupmenu can accept a string during creation to generate a memory image bufferedimage. Then, we need to overwrite the getinsets method of jcomponent, re-calculate the left value of inset, add the width of the slice based on the original value, and then return:

Public insets getinsets (){
Insets = (insets) Super. getinsets (). Clone ();
Insets. Left + = imageicon. geticonwidth ();
Return insets;
}

Finally, overwrite the paintcomponent method and add the image painting on the original base:
Public void paintcomponent (Graphics g ){
Super. paintcomponent (g );
If (imageicon! = NULL ){
Insets = getinsets ();
G. drawimage (imageicon. getimage (),
Insets. Left-imageicon. geticonwidth (),
Insets. Top,
Null );
}
}

The complete code is as follows:

Import java. AWT .*;
Import java. AWT. event .*;
Import java. AWT. Geom .*;
Import java. AWT. image .*;
Import javax. Swing .*;

Public class jimagedpopupmenu extends jpopupmenu {
Private Font font = new font ("dialog", Font. Bold, 13 );
Private imageicon = NULL;

Public jimagedpopupmenu (imageicon ){
This. imageicon = imageicon;
}

Public jimagedpopupmenu (string text ){
This. imageicon = createimage (text );
}

Private imageicon createimage (string text ){
Bufferedimage Bi = new bufferedimage (30,100 0, bufferedimage. type_int_argb );
Imageicon image = new imageicon (BI );
Graphics2d g2d = Bi. creategraphics ();

Gradientpaint paint = new gradientpaint (0, 0, color. Yellow, 30, 10, color. Red, true );
G2d. setpaint (paint );

G2d. fillrect (0, 0, Bi. getwidth (), Bi. getheight ());

Affinetransform at = new affinetransform ();
At. Rotate (-math. PI/2 );

G2d. settransform ();
G2d. setcolor (color. White );
G2d. setfont (font );
G2d. drawstring (text,-180, Bi. getwidth ()/2 );

Return image;
}

Public insets getinsets (){
Insets = (insets) Super. getinsets (). Clone ();
Insets. Left + = imageicon. geticonwidth ();
Return insets;
}

Public void paint (Graphics g ){
Super. Paint (g );
If (imageicon! = NULL ){
Insets = getinsets ();
G. drawimage (imageicon. getimage (),
Insets. Left-imageicon. geticonwidth (),
Insets. Top,
Null );
}
}

Public static void main (string [] ARGs ){
Final jframe frame = new jframe ();
Frame. setsize (600,500 );
Frame. settitle ("imagemenu ");
Final jimagedpopupmenu menu = new jimagedpopupmenu ("Windows XP perfessional ");
Menu. Add (New jmenuitem ("WinZip 8.0 "));
Menu. addseparator ();
Menu. Add (New jmenuitem ("programs "));
Menu. Add (New jmenuitem ("document "));
Menu. Add (New jmenuitem ("Settings "));
Menu. Add (New jmenuitem ("Search "));
Menu. Add (New jmenuitem ("Help and Support "));
Menu. Add (New jmenuitem ("Run ..."));
Menu. addseparator ();
Menu. Add (New jmenuitem ("shut down ..."));
Jlabel label = new jlabel ("right click me to show image popup menu .");
Label. addmouselistener (New java. AWT. event. mouseadapter (){
Public void mousereleased (mouseevent e ){
If (E. ispopuptrigger ()){
Menu. Show (frame, E. getpoint (). X, E. getpoint (). y );
}
}
});
Frame. getcontentpane (). Add (Label, borderlayout. center );
Frame. Show ();
}
}

The running effect is as follows:

Similarly, this method can also be used to extend jmenu to the same effect.

 

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.