Java - Problema con setBackground

 
Vista:

Problema con setBackground

Publicado por gjbr (11 intervenciones) el 08/08/2010 13:22:35
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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problema con setBackground

Publicado por mayrita (163 intervenciones) el 08/08/2010 22:59:04
lo que pasa es que al frame no le puedes dar color
pero puedes ponerle un panel y en el panel pones todo
y al panel si le pones color
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar