protected float drawBoxedString(Graphics2D g2, String s, Color c1, Color c2, double x) { // Calculate the width of the string. FontRenderContext frc = g2.getFontRenderContext(); TextLayout subLayout = new TextLayout(s, mFont, frc); float advance = subLayout.getAdvance(); // Fill the background rectangle with a gradient. GradientPaint gradient = new GradientPaint((float) x, 0, c1, (float) (x + advance), 0, c2); g2.setPaint(gradient); Rectangle2D bounds = mLayout.getBounds(); Rectangle2D back = new Rectangle2D.Double(x, 0, advance, bounds.getHeight()); g2.fill(back); // Draw the string over the gradient rectangle. g2.setPaint(Color.white); g2.setFont(mFont); g2.drawString(s, (float) x, (float) -bounds.getY()); return advance; }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g.create(); g2.setPaint(Objects.nonNull(c) ? c.getBackground() : Color.WHITE); g2.fillRect(x, y, getIconWidth(), getIconHeight()); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(ELLIPSE_COLOR); g2.translate(x, y); int size = list.size(); for (int i = 0; i < size; i++) { float alpha = isRunning ? (i + 1) / (float) size : .5f; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); g2.fill(list.get(i)); } // g2.translate(-x, -y); g2.dispose(); }
protected void drawBackground(Graphics2D g2) { // Draw circles of different colors. int side = 45; int width = getSize().width; int height = getSize().height; Color[] colors = { Color.yellow, Color.cyan, Color.orange, Color.pink, Color.magenta, Color.lightGray }; for (int y = 0; y < height; y += side) { for (int x = 0; x < width; x += side) { Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side); int index = (x + y) / side % colors.length; g2.setPaint(colors[index]); g2.fill(ellipse); } } }