Google Tag Manager

Showing posts with label Translucent. Show all posts
Showing posts with label Translucent. Show all posts

2015/03/30

aterai

Create a translucent JScrollBar

Code

public JComponent makeTranslucentScrollBar(JComponent c) {
  JScrollPane scrollPane = new JScrollPane(c) {
    @Override public boolean isOptimizedDrawingEnabled() {
      return false; // JScrollBar is overlap
    }
  };
  scrollPane.setVerticalScrollBarPolicy(
      ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  scrollPane.setHorizontalScrollBarPolicy(
      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

  scrollPane.setComponentZOrder(scrollPane.getVerticalScrollBar(), 0);
  scrollPane.setComponentZOrder(scrollPane.getViewport(), 1);
  scrollPane.getVerticalScrollBar().setOpaque(false);

  scrollPane.setLayout(new ScrollPaneLayout() {
    @Override public void layoutContainer(Container parent) {
      JScrollPane scrollPane = (JScrollPane) parent;

      Rectangle availR = scrollPane.getBounds();
      availR.x = availR.y = 0;

      Insets insets = parent.getInsets();
      availR.x = insets.left;
      availR.y = insets.top;
      availR.width  -= insets.left + insets.right;
      availR.height -= insets.top  + insets.bottom;

      Rectangle vsbR = new Rectangle();
      vsbR.width  = 12;
      vsbR.height = availR.height;
      vsbR.x = availR.x + availR.width - vsbR.width;
      vsbR.y = availR.y;

      if (viewport != null) {
        viewport.setBounds(availR);
      }
      if (vsb != null) {
        vsb.setVisible(true);
        vsb.setBounds(vsbR);
      }
    }
  });
  scrollPane.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
    private final Color defaultColor  = new Color(220, 100, 100, 100);
    private final Color draggingColor = new Color(200, 100, 100, 100);
    private final Color rolloverColor = new Color(255, 120, 100, 100);
    private final Dimension d = new Dimension();
    @Override protected JButton createDecreaseButton(int orientation) {
      return new JButton() {
        @Override public Dimension getPreferredSize() {
          return d;
        }
      };
    }
    @Override protected JButton createIncreaseButton(int orientation) {
      return new JButton() {
        @Override public Dimension getPreferredSize() {
          return d;
        }
      };
    }
    @Override protected void paintTrack(Graphics g, JComponent c, Rectangle r) {}
    @Override protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
      Color color;
      JScrollBar sb = (JScrollBar) c;
      if (!sb.isEnabled() || r.width > r.height) {
        return;
      } else if (isDragging) {
        color = draggingColor;
      } else if (isThumbRollover()) {
        color = rolloverColor;
      } else {
        color = defaultColor;
      }
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                          RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setPaint(color);
      g2.fillRect(r.x, r.y, r.width - 1, r.height - 1);
      g2.setPaint(Color.WHITE);
      g2.drawRect(r.x, r.y, r.width - 1, r.height - 1);
      g2.dispose();
    }
    @Override protected void setThumbBounds(int x, int y, int width, int height) {
      super.setThumbBounds(x, y, width, height);
      //scrollbar.repaint(x, 0, width, scrollbar.getHeight());
      scrollbar.repaint();
    }
  });
  return scrollPane;
}

References

2014/11/27

aterai

Make a transparent JTree and translucent selection background

Code

class TransparentTreeCellRenderer extends DefaultTreeCellRenderer {
  @Override public Component getTreeCellRendererComponent(
          JTree tree, Object value, boolean isSelected, boolean expanded,
          boolean leaf, int row, boolean hasFocus) {
    JComponent c = (JComponent) super.getTreeCellRendererComponent(
          tree, value, isSelected, expanded, leaf, row, hasFocus);
    c.setOpaque(false);
    return c;
  }
  private final Color ALPHA_OF_ZERO = new Color(0, true);
  @Override public Color getBackgroundNonSelectionColor() {
    return ALPHA_OF_ZERO;
  }
  @Override public Color getBackgroundSelectionColor() {
    return ALPHA_OF_ZERO;
  }
}

class TranslucentTreeCellRenderer extends TransparentTreeCellRenderer {
  private final Color backgroundSelectionColor = new Color(100, 100, 255, 100);
  @Override public Color getBackgroundSelectionColor() {
    return backgroundSelectionColor;
  }
}

References

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

2013/01/08

aterai

make a translucent JButton

Code

class TranslucentButton extends JButton{
  private static final Color TL = new Color(1f,1f,1f,.2f);
  private static final Color BR = new Color(0f,0f,0f,.4f);
  private static final Color ST = new Color(1f,1f,1f,.2f);
  private static final Color SB = new Color(1f,1f,1f,.1f);
  private Color ssc;
  private Color bgc;
  private int r = 8;
  public TranslucentButton(String text) {
    super(text);
  }
  public TranslucentButton(String text, Icon icon) {
    super(text, icon);
  }
  @Override public void updateUI() {
    super.updateUI();
    setContentAreaFilled(false);
    setFocusPainted(false);
    setOpaque(false);
    setForeground(Color.WHITE);
  }
  @Override protected void paintComponent(Graphics g) {
    int x = 0;
    int y = 0;
    int w = getWidth();
    int h = getHeight();
    Graphics2D g2 = (Graphics2D)g.create();
    g2.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
    Shape area = new RoundRectangle2D.Float(x, y, w-1, h-1, r, r);
    ssc = TL;
    bgc = BR;
    ButtonModel m = getModel();
    if(m.isPressed()) {
      ssc = SB;
      bgc = ST;
    }else if(m.isRollover()) {
      ssc = ST;
      bgc = SB;
    }
    g2.setPaint(new GradientPaint(x, y, ssc, x, y+h, bgc, true));
    g2.fill(area);
    g2.setPaint(BR);
    g2.draw(area);
    g2.dispose();
    super.paintComponent(g);
  }
}
private static String makeTitleWithIcon(URL u, String t, String a) {
  return String.format(
    "<html><p align='%s'><img src='%s' align='%s' />&nbsp;%s", a, u, a, t);
}
private static AbstractButton makeButton(String title) {
  return new JButton(title) {
    @Override public void updateUI() {
      super.updateUI();
      setVerticalAlignment(SwingConstants.CENTER);
      setVerticalTextPosition(SwingConstants.CENTER);
      setHorizontalAlignment(SwingConstants.CENTER);
      setHorizontalTextPosition(SwingConstants.CENTER);
      setBorder(BorderFactory.createEmptyBorder());
      //setBorderPainted(false);
      setContentAreaFilled(false);
      setFocusPainted(false);
      setOpaque(false);
      setForeground(Color.WHITE);
      setIcon(new TranslucentButtonIcon());
    }
  };
}
JLabel label = new JLabel("JLabel", icon, SwingConstants.CENTER);
label.setForeground(Color.WHITE);
label.setAlignmentX(.5f);
b = makeButton("");
b.setAlignmentX(.5f);
JPanel p = new JPanel();
p.setLayout(new OverlayLayout(p));
p.setOpaque(false);
p.add(label);
p.add(b);
add(p);

References

2012/07/04

aterai

Translucent JPopupMenu

Code

class TranslucentPopupMenu extends JPopupMenu{
  private static final Color ALPHA_ZERO = new Color(0, true);
  private static final Color POPUP_BACK = new Color(250,250,250,200);
  private static final Color POPUP_LEFT = new Color(230,230,230,200);
  private static final int LEFT_WIDTH = 24;
  @Override public boolean isOpaque() {
    return false;
  }
  @Override public void updateUI() {
    super.updateUI();
    //Test:
    boolean isNimbus = UIManager.getBorder("PopupMenu.border")==null;
    if(isNimbus) {
      //XXX: NimbusLnF not work???
      setBorder(new BorderUIResource(BorderFactory.createLineBorder(Color.GRAY)));
    }
  }
  @Override public JMenuItem add(JMenuItem menuItem) {
    menuItem.setOpaque(false);
    return super.add(menuItem);
  }
  @Override public void show(Component c, int x, int y) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        Window p = SwingUtilities.getWindowAncestor(TranslucentPopupMenu.this);
        if(p!=null && p instanceof JWindow) {
          System.out.println("Heavy weight");
          JWindow w = (JWindow)p;
          //To be exact: 1.6.0_10 <= java.version < 1.7.0
          //if(System.getProperty("java.version").startsWith("1.6.0")) {
          //  w.dispose();
          //  if(com.sun.awt.AWTUtilities.isWindowOpaque(w)) {
          //    com.sun.awt.AWTUtilities.setWindowOpaque(w, false);
          //  }
          //  w.setVisible(true);
          //}else{
          w.setBackground(ALPHA_ZERO);
          //}}
        }else{
          System.out.println("Light weight");
        }
      }
    });
    super.show(c, x, y);
  }
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g.create();
    g2.setPaint(POPUP_LEFT);
    g2.fillRect(0,0,LEFT_WIDTH,getHeight());
    g2.setPaint(POPUP_BACK);
    g2.fillRect(LEFT_WIDTH,0,getWidth(),getHeight());
    g2.dispose();
  }
}

References

2011/02/03

aterai

Translucent JInternalFrame (Nimbus)

Code

class MySynthStyleFactory extends SynthStyleFactory {
  private SynthStyleFactory wrappedFactory;
  public MySynthStyleFactory(SynthStyleFactory factory) {
    this.wrappedFactory = factory;
  }
  public SynthStyle getStyle(JComponent c, Region id) {
    SynthStyle s = wrappedFactory.getStyle(c, id);
    //if(id==Region.INTERNAL_FRAME_TITLE_PANE||id==Region.INTERNAL_FRAME) {
    if(id==Region.INTERNAL_FRAME) {
      s = new TranslucentSynthSytle(s);
    }
    return s;
  }
}
class TranslucentSynthSytle extends SynthStyle {
  private final SynthStyle style;
  public TranslucentSynthSytle(SynthStyle s) {
    style = s;
  }
  public SynthPainter getPainter(final SynthContext context) {
    return new SynthPainter() {
      public void paintInternalFrameBackground(SynthContext context,
                           Graphics g, int x, int y, int w, int h) {
        g.setColor(new Color(100,200,100,100));
        g.fillRoundRect(x,y,w-1,h-1,15,15);
      }
    };
  }
  public boolean isOpaque(SynthContext context) {
    if(context.getRegion()==Region.INTERNAL_FRAME) {
      return false;
    }else{
      return style.isOpaque(context);
    }
  }
  public Color getColorForState(SynthContext context, ColorType type) {
    return null; //Color.RED;
  }
  public Font getFontForState(SynthContext context) {
    return null; //new Font("MONOSPACE", Font.ITALIC, 24);
  }
  //...

References

2010/08/02

aterai

Transparent, Translucent JTable

Code

JScrollPane scroll = new JScrollPane(table) {
   private final TexturePaint texture = makeImageTexture();
  @Override protected JViewport createViewport() {
    return new JViewport() {
      @Override public void paintComponent(Graphics g) {
        if(texture!=null) {
          Graphics2D g2 = (Graphics2D)g;
          g2.setPaint(texture);
          g2.fillRect(0,0,getWidth(),getHeight());
        }
        super.paintComponent(g);
      }
    };
  }
};
final Color alphaZero = new Color(0, true);
table.setOpaque(false);
table.setBackground(alphaZero);
scroll.getViewport().setOpaque(false);
scroll.getViewport().setBackground(alphaZero);

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