Google Tag Manager

Showing posts with label TexturePaint. Show all posts
Showing posts with label TexturePaint. Show all posts

2014/02/26

aterai

Translucent JFrame repaint

Code

private final SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
private final JLabel label = new JLabel(df.format(new Date()), SwingConstants.CENTER);
private final TexturePanel tp;
private final Timer timer = new Timer(1000, new ActionListener() {
  @Override public void actionPerformed(ActionEvent e) {
    label.setText(df.format(new Date()));
    Container parent = SwingUtilities.getUnwrappedParent(label);
    if (parent != null && parent.isOpaque()) {
      repaintWindowAncestor(label);
    }
  }
});
private final JToggleButton button = new JToggleButton(new AbstractAction("timer") {
  private JFrame digitalClock;
  @Override public void actionPerformed(ActionEvent e) {
    if (digitalClock == null) {
      digitalClock = new JFrame();
      digitalClock.setUndecorated(true);
      digitalClock.setBackground(new Color(0, 0, 0, 0));
      digitalClock.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
      digitalClock.getContentPane().add(tp);
      digitalClock.pack();
      digitalClock.setLocationRelativeTo(null);
    }
    if (((AbstractButton) e.getSource()).isSelected()) {
      TexturePaints t = (TexturePaints) combo.getSelectedItem();
      tp.setTexturePaint(t.getTexturePaint());
      timer.start();
      digitalClock.setVisible(true);
    } else {
      timer.stop();
      digitalClock.setVisible(false);
    }
  }
});
private void repaintWindowAncestor(JComponent c) {
  JRootPane root = c.getRootPane();
  if (root == null) {
    return;
  }
  Rectangle r = SwingUtilities.convertRectangle(c, c.getBounds(), root);
  root.repaint(r.x, r.y, r.width, r.height);
}

References

2010/06/15

aterai

Translucent JFrame

Code

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
//com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
com.sun.awt.AWTUtilities.setWindowOpaque(frame, false); //6u14
//1.7.0 frame.setBackground(new Color(0,0,0,0));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(new JButton("JButton"));
p.setBackground(new Color(.5f,.8f,.5f,.5f));
frame.getContentPane().add(p);
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

References

2009/08/12

aterai

JMenuBar Background Image

Code

public JMenuBar createMenubar() {
  final TexturePaint texture = makeTexturePaint();
  JMenuBar mb = new JMenuBar() {
    @Override protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      g2.setPaint(texture);
      g2.fillRect(0, 0, getWidth(), getHeight());
    }
  };
  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();
    };
  };
  if("Windows XP".equals(System.getProperty("os.name"))) {
    menu.setBackground(new Color(0,0,0,0)); //XXX Windows XP lnf?
  }
  menu.add("dummy1"); menu.add("dummy2"); menu.add("dummy3");
  return menu;
}

References