Google Tag Manager

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

No comments:

Post a Comment