private void init() { this.setLayout(new GridLayout(getNumRows(), getNumColumns())); colors.forEach((label, color) -> { this.add(createLabel(label, color)); }); }
/** * Creates a new {@code ColorLegend} with the specified colors and * orientation. * * @param colors the map of labels and colors. * @param orientation the {@code Orientation} of the legend. */ public ColorLegend(Map<String, Color> colors, Orientation orientation) { this.colors = colors; this.orientation = orientation; this.init(); }
private int getNumRows() { return isHorizontal() ? 0 : this.colors.size(); } }
public static void main(String[] args) { ColorLegend colorLegend = new ColorLegend(getColors(), VERTICAL); showComponent( createPanelAndCenterComponent(colorLegend), "ColorLegend demo" ); }
private int getNumColumns() { return isHorizontal() ? this.colors.size() : 0; }
private JLabel createLabel(String text, Color color) { JLabel label = new JLabel(text, new ColorIcon(16, 16, color), LEFT); if (isHorizontal()) { label.setBorder(createEmptyBorder(0, 0, 0, 5)); } return label; }