Mostrar imagen en un JPanel

Estoy creando un JavaBean que herada de JPanel. Le he añadido unos botones. Así tendré mi propia barra de botones en la paleta de componentes. La cuestión es que quiero mostrar una imagen (un simple fondo azul degradado) y no soy capaz.
He probado lo siguiente y no me funciona:
1.-
JPanel1. GetGraphics(). DrawImage(
Toolkit. GetDefaultToolkit()
.createImage ( BeanBotonesBase
.class.getResource
( "ImagendeFondo.jpg" )) , 0, 0,
this );
2.-
He intentado dibujar un simple rectangulo y tampoco.
3.-
He intentado modificar el método paint del panel. En esta caso dibujo un rectangulo o la imagen, pero no dibuja los botones que había insertado.

1 respuesta

Respuesta
1
¿Los botones tienen que ir sobre la imagen?
Si, los botones tienen que mostrarse por encima de la imagen.
Si lo que haces es afirmar, me pregunto si se pueden añadir componentes a una imagen. ¿Como se añaden los botones sobre la imagen?
Atentamente,
Alvaro
Lo que intentas hacer es muy dificil, yo en todos los años que he trabajado con Java nunca lo he podido lograr. Si he creado mis propios objetos paneles, botones, etc, cion imagenes de fondo. Esto a partir del opbjeto Canvas.
Pero tan dificil es mostrar una imagen de fondo en un JPanel y añadir al panel otros componentes como botones, campos de texto, etc...
Yo lo he logrado con un JFrame. Cargo una imagen de fondo en el método paint del frame y luego hago un repaint() de todos los componentes para que se vean por encima de la imagen de fondo.
El problema que tengo con el JPanel es que no me funciona y creo que tiene algo que ver con el contentPane que añade JBuilder. No lo sé, la verdad.
¿Por cierto, como usas el Canvas para mostrar la imagen?
Un saludo y gracias por tu ayuda.
Alvaro
Incluso puedes crear botones circulares o con cualquier forma!. Te mando un ejemplo de un boton hecho por mí:
import java.awt.*;
import java.awt.image.*;
class BBoton extends Canvas {
private Image C0[];
private String C1;
public boolean C2;
private boolean C3;
Color fondo = new Color(216,217,202);
public boolean estado = false;
BBoton()
{
C1 = "";
}
BBoton(Image image, int i, int j) {
C1 = "";
C3 = true;
C0 = new Image[2];
C0[0] = image;
C0[1] = filtrar(image);
setBackground(fondo);;
resize(i, j);
}
BBoton(Image image, String s, int i, int j) {
this(image);
C1 = s;
resize(i, j);
}
BBoton(Image image, String s) {
this(image);
C1 = s;
}
BBoton(Image image) {
C1 = "";
C3 = true;
C0 = new Image[2];
C0[0] = image;
C0[1] = filtrar(image);
setBackground(fondo);
}
public void paint(Graphics g) {
Dimension dimension = size();
FontMetrics fontmetrics = g.getFontMetrics();
int i = 0;
int j = 0;
if(C0 != null) {
i = C0[0].getWidth(this);
j = C0[0].getHeight(this);
}
int k;
if(C1.length() > 0)
k = i + fontmetrics.stringWidth(C1) + 8;
else
k = i;
int l = j;
int i1 = (dimension.width - k) / 2;
int j1 = (dimension.height - l) / 2;
int k1 = (dimension.height + fontmetrics.getHeight()) / 2 - fontmetrics.getMaxDescent();
g.setColor(getBackground());
g.fillRect(1, 1, dimension.width - 2, dimension.height - 2);
if(C2 || !C3) {
g.setColor(Color.black);
g.drawLine(0, 0, dimension.width - 1, 0);
g.drawLine(0, 0, 0, dimension.height - 1);
g.setColor(Color.gray);
g.drawLine(1, 1, dimension.width - 2, 1);
g.drawLine(1, 1, 1, dimension.height - 2);
g.setColor(fondo);
g.drawLine(1, dimension.height - 2, dimension.width - 2, dimension.height - 2);
g.drawLine(dimension.width - 2, dimension.height - 2, dimension.width - 2, 1);
g.setColor(Color.white);
g.drawLine(0, dimension.height - 1, dimension.width - 1, dimension.height - 1);
g.drawLine(dimension.width - 1, dimension.height - 1, dimension.width - 1, 0);
g.clipRect(2, 2, dimension.width - 4, dimension.height - 4);
if(C0 != null)
if (estado)
if(C1.length() > 0)
g.drawImage(C0[0], 6, j1 + 2, this);
else
g.drawImage(C0[0], i1 + 2, j1 + 2, this);
else
if(C1.length() > 0)
g.drawImage(C0[1], 6, j1 + 2, this);
else
g.drawImage(C0[1], i1 + 2, j1 + 2, this);
if(C1.length() > 0) {
g.setFont(new Font("Dialog", 1, 12));
g.setColor(SystemColor.textHighlight);
g.drawString(C1, i1 + i + 6, k1 + 2);
return;
}
} else {
g.setColor(Color.white);
g.drawLine(0, 0, dimension.width - 1, 0);
g.drawLine(0, 0, 0, dimension.height - 1);
g.setColor(Color.black);
g.drawLine(0, dimension.height - 1, dimension.width - 1, dimension.height - 1);
g.drawLine(dimension.width - 1, dimension.height - 1, dimension.width - 1, 0);
g.setColor(Color.gray);
g.drawLine(1, dimension.height - 2, dimension.width - 2, dimension.height - 2);
g.drawLine(dimension.width - 2, dimension.height - 2, dimension.width - 2, 1);
g.clipRect(2, 2, dimension.width - 4, dimension.height - 4);
if(C0 != null)
if (estado)
if(C1.length() > 0)
g.drawImage(C0[0], 4, j1, this);
else
g.drawImage(C0[0], i1, j1, this);
else
if(C1.length() > 0)
g.drawImage(C0[1], 4, j1, this);
else
g.drawImage(C0[1], i1, j1, this);
if(C1.length() > 0) {
g.setFont(new Font("Dialog", 1, 12));
g.setColor(SystemColor.textHighlight);
g.drawString(C1, i1 + i + 4, k1);
}
}
}
public void update(Graphics g) {
paint(g);
}
public void setRaised(boolean flag) {
C3 = flag;
repaint();
}
public void setEnabled(boolean estado)
{
this.estado = estado;
repaint();
}
public Image filtrar(Image img)
{
if (!estado)
{
try
{
int ancho = img.getWidth(this);
int alto = img.getHeight(this);
int pixeles[] = new int[alto*ancho];
PixelGrabber pg = new PixelGrabber(img,0,0,ancho,alto,pixeles,0,alto);
pg.grabPixels();
for(int i = 0; i < ancho;i ++)
{
for(int j = 0; j < alto;j ++)
{
pixeles[j*ancho + i] = filPixel(pixeles[j*ancho + i]);
}
}
img = createImage(new MemoryImageSource(ancho,alto,pixeles,0,alto));
}
catch(Exception e){
System.out.println(e.toString());
};
}
return img;
}
public int filPixel(int pixel)
{
if ((pixel!=16777215))
{
// pixel = Invertir(pixel);
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
int k = (int)(.56 * g + .33 * r + .11 * b);
pixel = (0xff000000 | k << 16 | k << 8 | k);
}
return pixel;
}
public int Invertir(int pixel)
{
int r = 0xff - (pixel >> 16) & 0xff;
int g = 0xff - (pixel >> 8) & 0xff;
int b = 0xff - pixel & 0xff;
return (0xff000000 | r << 16 | g << 8 | b);
}
public void active(boolean cond)
{
C2 = cond;
repaint();
}
}
import java.awt.*;
class BBotonConexion extends BBoton
{
GrafTendencias padre;
boolean cond = true;
Thread t;
BBotonConexion(Image image, int i, int j, GrafTendencias padre) {
super(image,i,j);
this.padre = padre;
}
public boolean handleEvent(Event event)
{
if(estado)
{
switch(event.id) {
case 504: // Event.MOUSE_EVENT
// C2 = false;
break;
case 505: // Event.MOUSE_EXIT
/* if(C2) {
C2 = false;
repaint();
}*/
break;
case 501: // Event.MOUSE_DOWN
repaint();
break;
case 502: // Event.MOUSE_UP
if(C2)
{
active(false);
}
else
{
active(true);
}
repaint();
break;
default:
return super.handleEvent(event);
}
}
return true;
}
public void active(boolean cond)
{
super.active(cond);
if (cond)
{
t = new Thread( new Ciclo() );
t.start();
}
else
{
if (t!=null)
{
cond = false;
t.stop();
}
}
}
class Ciclo implements Runnable
{
Ciclo()
{
}
public void run()
{
while(cond)
{
try
{
padre.buscarHistorico();
Thread.sleep(5000);
}
catch(Exception e){};
}
}
}
}

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas