Google Tag Manager

Showing posts with label OverlayLayout. Show all posts
Showing posts with label OverlayLayout. Show all posts

2015/01/29

aterai

Show/Hide PasswordField

Code

JPasswordField pf = new JPasswordField(24);
pf.setText("abcdefghijklmn");
pf.setAlignmentX(Component.RIGHT_ALIGNMENT);
AbstractDocument doc = (AbstractDocument) pf.getDocument();
doc.setDocumentFilter(new ASCIIOnlyDocumentFilter());

AbstractButton b = new JToggleButton(new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    AbstractButton c = (AbstractButton) e.getSource();
    Character ec = c.isSelected() ? 0
        : (Character) UIManager.get("PasswordField.echoChar");
    pf.setEchoChar(ec);
  }
});
b.setFocusable(false);
b.setOpaque(false);
b.setContentAreaFilled(false);
b.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
b.setAlignmentX(Component.RIGHT_ALIGNMENT);
b.setAlignmentY(Component.CENTER_ALIGNMENT);
b.setIcon(new ColorIcon(Color.GREEN));
b.setRolloverIcon(new ColorIcon(Color.BLUE));
b.setSelectedIcon(new ColorIcon(Color.RED));
b.setRolloverSelectedIcon(new ColorIcon(Color.ORANGE));

JPanel panel = new JPanel() {
  @Override public boolean isOptimizedDrawingEnabled() {
    return false;
  }
};
panel.setLayout(new OverlayLayout(panel));
panel.add(b);
panel.add(pf);

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/12/09

aterai

Combine five JButtons to make CompoundButton

Code

private static JComponent makeCompoundButton(final Dimension d) {
  JPanel p = new JPanel() {
    @Override public Dimension getPreferredSize() {
      return d;
    }
  };
  p.setLayout(new OverlayLayout(p));
  p.add(new CompoundButton(d, ButtonLocation.NOTH));
  p.add(new CompoundButton(d, ButtonLocation.SOUTH));
  p.add(new CompoundButton(d, ButtonLocation.EAST));
  p.add(new CompoundButton(d, ButtonLocation.WEST));
  p.add(new CompoundButton(d, ButtonLocation.CENTER));
  return p;
}

class CompoundButton extends JButton {
  protected final Color fc = new Color(100,150,255,200);
  protected final Color ac = new Color(230,230,230);
  protected final Color rc = Color.ORANGE;
  protected Shape shape;
  protected Shape base = null;
  private final ButtonLocation bl;
  private final Dimension dim;
  public CompoundButton(Dimension d, ButtonLocation bl) {
    super();
    this.dim = d;
    this.bl = bl;
    setIcon(new Icon() {
      @Override public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                  RenderingHints.VALUE_ANTIALIAS_ON);
        if(getModel().isArmed()) {
          g2.setColor(ac);
          g2.fill(shape);
        }else if(isRolloverEnabled() && getModel().isRollover()) {
          paintFocusAndRollover(g2, rc);
        }else if(hasFocus()) {
          paintFocusAndRollover(g2, fc);
        }else{
          g2.setColor(getBackground());
          g2.fill(shape);
        }
        g2.dispose();
      }
      @Override public int getIconWidth()  {
        return dim.width;
      }
      @Override public int getIconHeight() {
        return dim.height;
      }
    });
    setFocusPainted(false);
    setContentAreaFilled(false);
    setBackground(new Color(250, 250, 250));
    initShape();
  }
  @Override public Dimension getPreferredSize() {
    return dim;
  }
  protected void initShape() {
    if(!getBounds().equals(base)) {
      base = getBounds();
      float ww = getWidth() * 0.5f;
      float xx = ww * 0.5f;
      Shape inner = new Ellipse2D.Float(xx, xx, ww, ww);
      if(ButtonLocation.CENTER==bl) {
        shape = inner;
      }else{
        Shape outer = new Arc2D.Float(
          1, 1, getWidth()-2, getHeight()-2,
          bl.getStartDegree(), 90f, Arc2D.PIE);
        Area area = new Area(outer);
        area.subtract(new Area(inner));
        shape = area;
      }
    }
  }
  private void paintFocusAndRollover(Graphics2D g2, Color color) {
    g2.setPaint(new GradientPaint(0, 0, color,
                    getWidth()-1, getHeight()-1, color.brighter(), true));
    g2.fill(shape);
    g2.setColor(getBackground());
  }
  @Override protected void paintComponent(Graphics g) {
    initShape();
    super.paintComponent(g);
  }
  @Override protected void paintBorder(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(getForeground());
    g2.draw(shape);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_OFF);
  }
  @Override public boolean contains(int x, int y) {
    //initShape();
    return shape.contains(x, y);
  }
}

References

2011/05/30

aterai

Translucent image caption using JTextArea, OverlayLayout, EaseInOut

Code

class ImageCaptionLabel extends JLabel implements HierarchyListener {
  private float alpha = 0.0f;
  private javax.swing.Timer animator;
  private int yy = 0;
  private JTextArea textArea = new JTextArea() {
    @Override protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setPaint(getBackground());
      g2.fillRect(0, 0, getWidth(), getHeight());
      super.paintComponent(g);
    }
    //@Override public boolean contains(int x, int y) {
    //  return false;
    //}
  };
  public ImageCaptionLabel(String caption, Icon icon) {
    setIcon(icon);
    textArea.setFont(textArea.getFont().deriveFont(11f));
    textArea.setText(caption);
    textArea.setOpaque(false);
    textArea.setEditable(false);
    //textArea.setFocusable(false);
    textArea.setBackground(new Color(0,0,0,0));
    textArea.setForeground(Color.WHITE);
    textArea.setBorder(BorderFactory.createEmptyBorder(2,4,4,4));

    MouseAdapter ma = new MouseAdapter() {
      @Override public void mouseEntered(MouseEvent e) {
        dispatchMouseEvent(e);
      }
      @Override public void mouseExited(MouseEvent e) {
        dispatchMouseEvent(e);
      }
      private void dispatchMouseEvent(MouseEvent e) {
        Component src = e.getComponent();
        Component tgt = ImageCaptionLabel.this;
        tgt.dispatchEvent(SwingUtilities.convertMouseEvent(src, e, tgt));
      }
    };
    textArea.addMouseListener(ma);

    setBorder(BorderFactory.createCompoundBorder(
      BorderFactory.createLineBorder(new Color(222,222,222)),
      BorderFactory.createLineBorder(Color.WHITE, 4)));
    setLayout(new OverlayLayout(this) {
      @Override public void layoutContainer(Container parent) {
        //Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();
        if(ncomponents == 0) return;
        int width = parent.getWidth(); // - (insets.left + insets.right);
        int height = parent.getHeight(); // - (insets.left + insets.right);
        int x = 0; //insets.left; int y = insets.top;
        //for(int i=0;i < ncomponents;i++) {
        Component c = parent.getComponent(0); //= textArea;
        c.setBounds(x, height-yy, width, c.getPreferredSize().height);
        //}
      }
    });
    add(textArea);

    addMouseListener(new MouseAdapter() {
      private int delay = 4;
      private int count = 0;
      @Override public void mouseEntered(MouseEvent e) {
        if(animator!=null && animator.isRunning() ||
           yy==textArea.getPreferredSize().height) return;
        final double h = (double)textArea.getPreferredSize().height;
        animator = new javax.swing.Timer(delay, new ActionListener() {
          @Override public void actionPerformed(ActionEvent e) {
            double a = easeInOut(++count/h);
            yy = (int)(.5d+a*h);
            textArea.setBackground(new Color(0f,0f,0f,(float)(0.6*a)));
            if(yy >= textArea.getPreferredSize().height) {
              yy = textArea.getPreferredSize().height;
              animator.stop();
            }
            revalidate();
            repaint();
          }
        });
        animator.start();
      }
      @Override public void mouseExited(MouseEvent e) {
        if(animator!=null && animator.isRunning() ||
           contains(e.getPoint()) && yy==textArea.getPreferredSize().height) {
            return;
        }
        final double h = (double)textArea.getPreferredSize().height;
        animator = new javax.swing.Timer(delay, new ActionListener() {
          @Override public void actionPerformed(ActionEvent e) {
            double a = easeInOut(--count/h);
            yy = (int)(.5d+a*h);
            textArea.setBackground(new Color(0f,0f,0f,(float)(0.6*a)));
            if(yy <= 0) {
              yy = 0;
              animator.stop();
            }
            revalidate();
            repaint();
          }
        });
        animator.start();
      }
    });
    addHierarchyListener(this);
  }
  @Override public void hierarchyChanged(HierarchyEvent e) {
    if((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED)!=0 &&
       animator!=null && !isDisplayable()) {
      animator.stop();
    }
  }
  //http://www.anima-entertainment.de/math-easein-easeout-easeinout-and-bezier-curves
  //coders≫ Blog Archive ≫ Math: EaseIn EaseOut, EaseInOut and Bezier Curves
  public double easeInOut(double t) {
    if(t < 0.5d) {
      return 0.5d*Math.pow(t*2d, 3d);
    }else{
      return 0.5d*(Math.pow(t*2d-2d, 3d) + 2d);
    }
  }
}

References