package example; import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*;

public class MainPanel extends JPanel{

   public MainPanel(JFrame frame) {
       super();
       frame.setJMenuBar(createMenubar());
       setPreferredSize(new Dimension(320, 180));
   }
   public JMenuBar createMenubar() {
       final TexturePaint texture = makeTexturePaint();
       JMenuBar mb = new JMenuBar() {
           @Override protected void paintComponent(Graphics g) {
               Graphics2D g2 = (Graphics2D)g;
               g2.setPaint(texture);
               g2.fillRect(0, 0, getWidth(), getHeight());
               super.paintComponent(g);
           }
       };
       mb.setOpaque(false);
       String[] menuKeys = {"File", "Edit", "Help"};
       for(String key: menuKeys) {
           JMenu m = createMenu(key);
           if(m != null) mb.add(m);
       }
       return mb;
   }
   private JMenu createMenu(String key) {
       JMenu menu = new JMenu(key) {
           @Override protected void fireStateChanged() {
               ButtonModel m = getModel();
               if(m.isPressed() && m.isArmed()) {
                   setOpaque(true);
               }else if(m.isSelected()) {
                   setOpaque(true);
               }else if(isRolloverEnabled() && m.isRollover()) {
                   setOpaque(true);
               }else{
                   setOpaque(false);
               }
               super.fireStateChanged();
           };
       };
       menu.setBackground(new Color(0,0,0,0)); //XXX windows lnf?
       menu.add("dummy1"); menu.add("dummy2"); menu.add("dummy3");
       return menu;
   }
   private BufferedImage makeBGImage() {
       Color color = new Color(200,150,100,50);
       int cs = 6;
       int sz = cs*cs;
       BufferedImage img = new BufferedImage(sz,sz,BufferedImage.TYPE_4BYTE_ABGR);
       Graphics2D g2 = img.createGraphics();
       g2.setPaint(color);
       g2.fillRect(0,0,sz,sz);
       for(int i=0;i*cs<sz;i++) {
           for(int j=0;j*cs<sz;j++) {
               if((i+j)%2==0) g2.fillRect(i*cs, j*cs, cs, cs);
           }
       }
       g2.dispose();
       //try{
       //  img = javax.imageio.ImageIO.read(getClass().getResource("test.gif"));
       //}catch(java.io.IOException ioe) {
       //  throw new RuntimeException(ioe);
       //}
       return img;
   }
   private TexturePaint makeTexturePaint() {
       BufferedImage img = makeBGImage();
       int w = img.getWidth(), h = img.getHeight();
       Rectangle2D r2d = new Rectangle2D.Float(0,0,w,h);
       return new TexturePaint(img, r2d);
   }
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }
   public static void createAndShowGUI() {
       try{
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       }catch(Exception e) {
           e.printStackTrace();
       }
       JFrame frame = new JFrame("@title@");
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       frame.getContentPane().add(new MainPanel(frame));
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
   }

}