What if you call super.paintComponent (g) at the end of the method so that the label draws text after drawing the gradient:
public void paintComponent(Graphics g) { // super.paintComponent(g); // *** commented Graphics2D g2d = (Graphics2D) g; Color color1 = new Color(226, 218, 145); Color color2 = color1.brighter(); int w = getWidth(); int h = getHeight(); GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2); g2d.setPaint(gp); g2d.fillRect(0, 0, w, h); super.paintComponent(g); // *** added }
Also, as irrelevant, I prefer to change this:
Dimension size = new Dimension(70, 80); public DLabel() { this.setPreferredSize(size); this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white, Color.black)); }
:
public static final Dimension PREF_SIZE = new Dimension(70, 80); public DLabel() { this.setBorder(BorderFactory.createBevelBorder(TOP, Color.white, Color.black)); } @Override public Dimension getPreferredSize() { Dimension superDim = super.getPreferredSize(); int width = Math.max(superDim.getWidth(), PREF_SIZE.getWidth()); int height = Math.max(superDim.getHeight(), PREF_SIZE.getHeight()); return new Dimension(width, height); }
source share