Realizar JLabel con color de fondo gradiente.
void paintComponent(Graphics g) de la clase JComponent, recuerda que esta es la clase base para los componentes SWING.Para lograr un color de fondo gradiente horizontal: Hay que implementar este codigo:
JLabel label = new JLabel("...... DatoJava ......") {
@Override
protected void paintComponent(Graphics g) {
Color colorFinal = new Color(255, 1, 1);
Graphics2D graphics2d = (Graphics2D) g.create();
graphics2d.setPaint(new GradientPaint(0, 0, getBackground(),
getWidth(), 0, colorFinal));
graphics2d.fillRect(0, 0, getWidth(), getHeight());
graphics2d.dispose();
super.paintComponent(g);
}
};
Para lograr un color de fondo gradiente vertical:
Hay que implementar este codigo:
JLabel label = new JLabel("...... DatoJava ......") {
@Override
public void setForeground(Color fg) {
super.setForeground(fg.RED);
}
@Override
protected void paintComponent(Graphics g) {
LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
new Point(0, 10), new Point(0, getHeight()),
new float[] { 0.240f, 0.250f }, new Color[] {
Color.LIGHT_GRAY, Color.GRAY });
Graphics2D graphics2d = (Graphics2D) g.create();
graphics2d.setPaint(linearGradientPaint);
graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
graphics2d.dispose();
super.paintComponent(g);
}
};
Con esto podemos jugar y hacer varias cosas interesantes, por ejemplo:
JLabel label = new JLabel("...... DatoJava ......") {
@Override
protected void paintComponent(Graphics g) {
LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
new Point(0, 10), new Point(10, getHeight()),
new float[] { 0.240f, 0.250f }, new Color[] {
Color.BLACK, getBackground() });
Graphics2D graphics2d = (Graphics2D) g.create();
graphics2d.setPaint(linearGradientPaint);
graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
graphics2d.dispose();
super.paintComponent(g);
}
};
JLabel label = new JLabel("...... DatoJava ......") {
@Override
protected void paintComponent(Graphics g) {
LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
new Point(0, 25), new Point(0, getHeight()),
new float[] { 0.240f, 0.250f }, new Color[] {
getBackground(), Color.RED });
Graphics2D graphics2d = (Graphics2D) g.create();
graphics2d.setPaint(linearGradientPaint);
graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
graphics2d.dispose();
super.paintComponent(g);
}
};
JLabel label = new JLabel("...... DatoJava ......") {
@Override
protected void paintComponent(Graphics g) {
Color colorFinal = new Color(0, 0, 0);
Graphics2D graphics2d = (Graphics2D) g.create();
graphics2d.setPaint(new GradientPaint(0, 0, getBackground(),
getWidth(), 0, colorFinal));
graphics2d.fillRect(0, 25, getWidth(), getHeight());
graphics2d.dispose();
super.paintComponent(g);
}
};
No hay comentarios :
Publicar un comentario