Google Tag Manager

2011/05/30

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

2011/05/24

Change border of focused row in JTable

Code

class DotBorder extends EmptyBorder {
  public enum Type { START, END; }
  private static final BasicStroke dashed = new BasicStroke(
    1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
    10.0f, (new float[] {1.0f}), 0.0f);
  private static final Color dotColor = new Color(200,150,150);
  public DotBorder(int top, int left, int bottom, int right) {
    super(top, left, bottom, right);
  }
  public EnumSet type = EnumSet.noneOf(Type.class);
  @Override public boolean isBorderOpaque() {
    return true;
  }
  @Override public void paintBorder(
        Component c, Graphics g, int x, int y, int w, int h) {
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(x,y);
    g2.setPaint(dotColor);
    g2.setStroke(dashed);
    if(type.contains(Type.START)) {
      g2.drawLine(0,0,0,h);
    }
    if(type.contains(Type.END)) {
      g2.drawLine(w-1,0,w-1,h);
    }
    if(c.getBounds().x%2==0) {
      g2.drawLine(0,0,w,0);
      g2.drawLine(0,h-1,w,h-1);
    }else{
      g2.drawLine(1,0,w,0);
      g2.drawLine(1,h-1,w,h-1);
    }
    g2.translate(-x,-y);
  }
}

//......
UIManager.put("Table.focusCellHighlightBorder", new DotBorder(2,2,2,2));
JTable table = new JTable(model) {
  private final DotBorder dotBorder = new DotBorder(2,2,2,2);
  private final Border emptyBorder  = BorderFactory.createEmptyBorder(2,2,2,2);
  private void updateBorderType(DotBorder border, int column) {
    border.type = EnumSet.noneOf(DotBorder.Type.class);
    if(column==0) border.type.add(DotBorder.Type.START);
    if(column==getColumnCount()-1) border.type.add(DotBorder.Type.END);
  }
  @Override public Component prepareRenderer(
      TableCellRenderer tcr, int row, int column) {
    Component c = super.prepareRenderer(tcr, row, column);
    if(row==getSelectionModel().getLeadSelectionIndex()) {
      ((JComponent)c).setBorder(dotBorder);
      updateBorderType(dotBorder, column);
    }else{
      ((JComponent)c).setBorder(emptyBorder);
    }
    return c;
  }
  @Override public Component prepareEditor(
      TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    if(c instanceof JCheckBox) {
      JCheckBox b = (JCheckBox)c;
      //System.out.println(b.getBorder());
      //b.setBorder(dotBorder);
      updateBorderType((DotBorder)b.getBorder(), column);
      b.setBorderPainted(true);
      b.setBackground(getSelectionBackground());
    }
    return c;
  }
};

References