Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/11/2014, 06:18
dr4
 
Fecha de Ingreso: noviembre-2014
Mensajes: 28
Antigüedad: 9 años, 5 meses
Puntos: 0
tener 2 imagenes en 2 jpanels distintos una de fondo y la otra en medio

muy buenas, veréis estoy tratando de hacer un pequeño trabajo por mi cuenta sobre todo con el fin de aprender y llevo días atascado, lo que intento es crear una imagen de fondo que crezca y se encoja con la ventana, conseguí hacerlo sin problemas usando un borderlayout y asignandole center pero es problema es que luego quiero agregarle otra imagen que esté en el centro pero no ocupe toda la pantalla(por encima del fondo de pantalla) y que esa imagen también crezca y se encoja con la ventana, he usado un listener que me avise de la anchura y la altura de la ventana en todo momento y he tratado de asignarle la altura y la anchura del jframe al jpanel,pero me da error, agradecería mucho la ayuda ya que he llegado a un punto en el que soy incapaz de avanzar, tengo 3 clases,la del jframe,la del jpanel(con una imagen) y la principal, quiero que ese jpanel vaya de fondo y luego agregar otro con una imagen que vaya en medio


lo que quiero hacer es esto (enseño el ejemplo con paint porque obviamente no lo consigo hacer en java. tener 2 imagenes en 2 jpanel distintos, y que al darle a run salgan las dos y que si amplio la ventana las dos sigan en el mismo sitio ocupando el mismo espacio:
que este así:
http://imagizer.imageshack.us/a/img673/8572/c1KNyD.jpg
y que cuando amplie la ventana siga en el mismo sitio todo
http://imagizer.imageshack.us/a/img904/9180/Ci8dZ6.jpg


clase del JFrame:


import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.Timer;



public class PantallaInicio2 extends JFrame implements ComponentListener{
Imagenes imagenes;
Timer time;
TimerTask tiempo;
int width;
int height;
public PantallaInicio2(){

this.setBounds(0,0,450,450);
this.setTitle("ventana");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.setVisible(true);

Imagenes imagenes=new Imagenes();


Container contener=getContentPane();
contener.add(imagenes);

Dimension windowSize = this.getContentPane().getSize();
width = size().width;
height = size().height;


contener.addComponentListener(this);



System.out.println("Width "+width+("Height "+height));
this.add(imagenes);
if (width<450){

System.out.println("chivato if "+width);







}


}
public void componentResized(ComponentEvent e) {

if (e.getComponent().getWidth() > 450){

width=e.getComponent().getWidth();
height=e.getComponent().getHeight();
System.out.println("chivato listener "+width);
System.out.println("chivato listener "+height);
imagenes.alturap=width;
imagenes.anchurap=height;
}

}

@Override
public void componentMoved(ComponentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void componentShown(ComponentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void componentHidden(ComponentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}



}




clase del jpanel:


import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Imagenes extends JPanel{

JLabel fondo;
public int ancho,alto,alturap,anchurap;
PantallaInicio2 pantallainicio;

public Imagenes(){



//imagen de fondo

fondo=new JLabel();
ancho=0;
alto=0;
alturap=450;
anchurap=450;

this.add(fondo);

this.setSize(anchurap,alturap);
this.setLayout(new FlowLayout());

fondo.setIcon(ajustarImagen("fondopez.jpg"));
fondo.setBorder(BorderFactory.createEmptyBorder(0, 0,0,0));

}

private ImageIcon ajustarImagen(String ico){

ImageIcon auxfondo=new ImageIcon(ico);
//escalar imagen
ancho=this.getWidth();
alto=this.getHeight();

ImageIcon tauxfondo=new ImageIcon(auxfondo.getImage().getScaledInstance(an cho,alto,Image.SCALE_DEFAULT));

return tauxfondo;

}
}






clase principal:

public class Principal2{

public static void main(String[]args){

PantallaInicio2 ventana=new PantallaInicio2();



}


}