Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/08/2010, 11:15
gjbr
 
Fecha de Ingreso: noviembre-2009
Mensajes: 23
Antigüedad: 14 años, 6 meses
Puntos: 0
Pregunta Problemas con setBackground..

Hola a todos, tengo una aplicación bastante simple en la que me despliega una degradación de colores. El problema consiste en que el fondo del JFrame se me presenta transparente. He intentado colocar el método setBackground(Color c) en diversos lugares sin resultados algunos. Me podrían ayudar en este problemita por favor.

Agradezco de antemano la ayuda brindada. Trabajo en netbeans 6.9.


import java.awt.*;
import javax.swing.*;
import java.awt.geom.Rectangle2D;

public class EJEMPLOCOLORES extends JFrame {
Container contenedor = getContentPane();

public EJEMPLOCOLORES() {
setTitle("**** Bloques de colores. ****");
} // cierre del constructor

public void paint(Graphics g) {
int limit, rojo, verde, azul, alfa;
float s, x, y, ratio;
Graphics2D gd2 = (Graphics2D) g;
// contenedor.setBackground(Color.white);
Dimension dim = getSize();
gd2.translate(dim.width/2, dim.height/2);
Color[] colors = {Color.WHITE, Color.LIGHT_GRAY, Color.GRAY,
Color.DARK_GRAY, Color.BLACK, Color.RED, Color.PINK,
Color.ORANGE, Color.YELLOW, Color.GREEN, Color.MAGENTA,
Color.CYAN, Color.BLUE};
limit = colors.length;
s = 20;
x = -s * limit/2;
y = -s * 3/2;

// Se muestran los colores predefinidos
for(int i = 0; i < limit; i++) {
Rectangle2D rect = new Rectangle2D.Float(x + s * i, y, s, s);
gd2.setColor(colors[i]);
gd2.fill(rect);
}

// Se muestra un gradiente lineal
y += s;
Color c1 = Color.YELLOW;
Color c2 = Color.blue;
for(int i = 0; i < limit; i++) {
ratio = (float)i/(float)limit;
rojo = (int)(c2.getRed() * ratio + c1.getRed() * (1.0 - ratio));
verde = (int)(c2.getGreen() * ratio + c1.getGreen() * (1.0 - ratio));
azul = (int)(c2.getBlue() * ratio + c1.getBlue() * (1.0 - ratio));
Color color = new Color(rojo, verde, azul, 150);
Rectangle2D rect = new Rectangle2D.Float(x + s * i, y, s, s);
gd2.setColor(color);
gd2.fill(rect);
}

// Se muestra un gradiente alfa
y += s;
c1 = Color.RED;
for(int i = 0; i < limit; i++) {
alfa = (int)(255 * (float)i/(float)limit);
Color color = new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), alfa);
Rectangle2D rect = new Rectangle2D.Float(x + s * i, y, s, s);
gd2.setColor(color);
gd2.fill(rect);
}

// Dibujamos un marco alrededor de los cuadrados
gd2.setColor(Color.BLACK);
gd2.draw(new Rectangle2D.Float(x, y - s * 2, s * limit, s * 3));
} // cierre del método paint.

public static void main(String[] args) {
EJEMPLOCOLORES aplicacion = new EJEMPLOCOLORES();
aplicacion.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
// aplicacion.setBackground(Color.yellow);
aplicacion.setSize(300, 200);
aplicacion.setLocationRelativeTo(null);
aplicacion.setVisible(true);
} // cierre del método main.
} // CIERRE DE EJEMPLOCOLORES