Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
jllarraz
Posts: 69
Registered: ‎08-13-2008
My Device: Not Specified
Accepted Solution

Problem background image Field

Hi i am trying to put an image background to my app but when i do that the first time (just started) i run fine but when i select the one field and this lost the focus the background dont repaint. this is my source code:

 

 

 

 

 

 

 

//this is where i go to put the button

VerticalFieldManager panel= new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH |VerticalFieldManager.USE_ALL_HEIGHT)
{
protected void paint(Graphics graphics)
{
try
{
graphics.drawBitmap(0, 0, graphics.getScreenWidth(), graphics.getScreenHeight(),
ComunicadorCore.getGestorImagenes().getImagen("fondo.png").getBitmap(), 0, 0);
}
catch (ImagenException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
super.paint(graphics);
}
};

 

 

 

////////////////////////////////////////////////////////////////////

 

this is the button

 

 

public class BotonContactos extends Field implements DrawStyle
{
private boolean focus=false;

private Font fuente;
private int anchuraLabels;

private int margenAnchura=10;
private int margenAltura=2;

private String labelNombre;
private XYRect posLabelNombre;



private String labelEstado;
private XYRect posLabelEstado;



private EncodedImage imagenEstado;
private Bitmap bitMapEstado;
private XYRect posImagenEstado;

private EncodedImage imagenFormaContacto;
private XYRect posImagenFormaContacto;
private Bitmap bitMapFormaContacto;

private int anchuraBoton;
private int alturaBoton;

//formato del color 0x00RRGGBB
private final static int COLOR_FONDO_FOCO=0x008CAFDC;

private final static int COLOR_LETRAS_FOCO=0x00000066;

private final static int COLOR_LETRAS_NO_FOCO=0x00F0F4F9;

//Transparecia seleccion

private final static int TRANSPARECIA_SELECCION_FOCO=80;

public BotonContactos()
{
super(Field.FOCUSABLE);
posLabelNombre=new XYRect();
posLabelEstado=new XYRect();
posImagenEstado=new XYRect();
posImagenFormaContacto=new XYRect();
}

protected void drawFocus(Graphics graphics, boolean on)
{
MensajesLog.debug(BotonContactos.class, "Dibujo el foco: "+this.getLabelNombre());
this.focus=true;
super.drawFocus(graphics, false);
}

public int getPreferredWidth()
{
int tam=0;
if(this.imagenEstado!=null&&this.imagenFormaContacto!=null)
{
//Ponemos el margen de la izquierda
tam+=margenAnchura;

//Ponemos el tamaño de la imagen de estado
tam+=imagenEstado.getWidth();

//Ponemos el tamaño de la imagen de forma de contacto
tam+=imagenFormaContacto.getWidth();

//Ponemos el margen entre las imagenes y los labels
tam+=margenAnchura;

//Ponemos el tamaño de los labels
MensajesLog.debug(BotonContactos.class, "El tamaño del contenedor es: "+this.getContentRect().width);
//TODO coger el tamaño del boton
anchuraLabels=anchuraBoton-tam-margenAnchura;
if(anchuraLabels>0)
{
tam+=anchuraLabels;
}
else
{
anchuraLabels=0;
}

//Ponemos el margen de la derecha
tam+=margenAnchura;
}
MensajesLog.debug(BotonContactos.class, "La anchura del boton es: "+tam);
return tam;
}

public int getPreferredHeight()
{
int tam=0;
if(this.imagenEstado!=null&&this.imagenFormaContacto!=null)
{
tam=imagenEstado.getHeight();
int tamFont=this.getFont().getHeight();
//multiplicamos por 2
tamFont=tamFont<<1;
if(tam<imagenFormaContacto.getHeight())
{
tam=imagenFormaContacto.getHeight();
}
else if(tam<tamFont)
{
MensajesLog.debug(BotonContactos.class, "La altura de las fuentes es mayor que las imagenes: ");
tam=tamFont;
}
}
tam+=(this.margenAltura>>1);
MensajesLog.debug(BotonContactos.class, "La altura del boton es: "+tam);
return tam;
}

/**
* Metodo que calcula la posicion de los componentes dentro
* del boton
*/
protected void layout(int width, int height)
{
// Calculate width.
anchuraBoton = width;
anchuraBoton = Math.min( anchuraBoton, getPreferredWidth() );
MensajesLog.debug(BotonContactos.class, "La anchuraBoton del boton es: "+width);
// Calculate height.
alturaBoton = Math.min( height, getPreferredHeight() );
MensajesLog.debug(BotonContactos.class, "La alturaBoton del boton es: "+height);

fuente=getFont();
if(this.imagenEstado!=null&&this.imagenFormaContacto!=null)
{
int posYEstado=calculoPosicionAlturaImagenes(imagenEstado);
this.posImagenEstado.setLocation(this.margenAnchura,posYEstado);
this.posImagenEstado.setSize(this.imagenEstado.getWidth(), this.imagenEstado.getHeight());

int posYFormaContacto=calculoPosicionAlturaImagenes(imagenFormaContacto);
int posXFormaContacto=this.posImagenEstado.x+imagenEstado.getWidth();
this.posImagenFormaContacto.setLocation(posXFormaContacto, posYFormaContacto);
this.posImagenFormaContacto.setSize(this.imagenFormaContacto.getWidth(), this.imagenFormaContacto.getHeight());

int posXCabecera=this.posImagenFormaContacto.x+imagenFormaContacto.getWidth()+this.margenAnchura;
int posYCabecera=this.margenAltura;
this.posLabelNombre.setLocation(posXCabecera, posYCabecera);

int posXCuerpo=this.posLabelNombre.x;
int espacioEntreLabels=fuente.getHeight()>>1;
espacioEntreLabels+=fuente.getHeight();
int posYCuerpo=this.posLabelNombre.y+espacioEntreLabels;
this.posLabelEstado.setLocation(posXCuerpo,posYCuerpo);
}
setExtent( anchuraBoton, alturaBoton );
}

/**
* Calcula la posicon y centrada de la imagen dentro del boton
* @param image
* @return
*/
private int calculoPosicionAlturaImagenes(EncodedImage image)
{
int posY=0;
if(image!=null)
{
int alturaImage=image.getHeight();
int margen=this.getPreferredHeight()-alturaImage;
if(margen>0)
{
posY=margen>>1;
}
}
return posY;
}

/**
* Metodo que pinta el Boton
*/
protected void paint(Graphics graphics)
{

if(this.imagenEstado!=null&&this.imagenFormaContacto!=null)
{
graphics.setFont(fuente);
if(this.focus)
{
//Dibujo el recuadro de seleccion
graphics.setColor(COLOR_FONDO_FOCO);
graphics.setGlobalAlpha(TRANSPARECIA_SELECCION_FOCO);
graphics.fillRoundRect(0, 0, this.anchuraBoton, this.alturaBoton, 20, 20);

//Pongo la configuracion para escribir el texto
graphics.setGlobalAlpha(255);
graphics.setColor(COLOR_LETRAS_FOCO);
}
else
{
graphics.setColor(COLOR_LETRAS_NO_FOCO);
}
MensajesLog.debug(BotonContactos.class, "Comienzo a pintar");
graphics.drawBitmap(this.posImagenEstado, this.bitMapEstado, 0, 0);
graphics.drawBitmap(this.posImagenFormaContacto, this.bitMapFormaContacto, 0, 0);
graphics.drawText(this.labelNombre, this.posLabelNombre.x, this.posLabelNombre.y);
graphics.drawText(this.labelEstado, this.posLabelEstado.x, this.posLabelEstado.y);
this.focus=false;

}

}

/**
* Metodo para leer el nombre del contacto que muestra el boton
* @return
*/
public String getLabelNombre()
{
return labelNombre;
}

/**
* Metodo para asignar el nombre del contacto al boton
* @param nombre
*/
public void setLabelNombre(String nombre)
{
this.labelNombre = nombre;
updateLayout();
}

/**
* Metodo para leer el estado del contacto que muestra el boton
* @return
*/
public String getLabelEstado()
{
return labelEstado;
}

/**
* Metodo para asignar el estado del contacto al boton
* @param estado
*/
public void setLabelEstado(String estado)
{
this.labelEstado = estado;
updateLayout();
}

/**
* Metodo que devuelve la imagen del estado
* @return
*/
public EncodedImage getImagenEstado()
{
return imagenEstado;
}

/**
* Metodo que asigna la imagen del estado
* @param imagenEstado
*/
public void setImagenEstado(EncodedImage imagenEstado)
{
if(imagenEstado!=null)
{
this.imagenEstado = imagenEstado;
bitMapEstado=this.imagenEstado.getBitmap();
updateLayout();
}
}

/**
* Metodo que devuelve la imagen de la forma de contacto
* @return
*/
public EncodedImage getImagenFormaContacto()
{
return imagenFormaContacto;
}

/**
* Metodo que asigna la imagen de la forma de contacto
* @param imagenFormaContacto
*/
public void setImagenFormaContacto(EncodedImage imagenFormaContacto)
{
if(imagenFormaContacto!=null)
{
this.imagenFormaContacto = imagenFormaContacto;
this.bitMapFormaContacto=this.imagenFormaContacto.getBitmap();
updateLayout();
}
}

protected void onFocus(int i) {
// TODO Auto-generated method stub
this.focus=true;

super.onFocus(i);
}

protected void onUnfocus() {
// TODO Auto-generated method stub
this.focus=false;
super.onUnfocus();
}

 


 

Please use plain text.
Developer
jllarraz
Posts: 69
Registered: ‎08-13-2008
My Device: Not Specified

Re: Problem background image Field

i have found the solution.

VerticalFieldManager panel= new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH |VerticalFieldManager.USE_ALL_HEIGHT)
{
/*
protected void paint(Graphics graphics)
{
try
{
graphics.drawBitmap(0, 0, graphics.getScreenWidth(), graphics.getScreenHeight(),
ComunicadorCore.getGestorImagenes().getImagen("fondo.png").getBitmap(), 0, 0);
}
catch (ImagenException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
super.paint(graphics);
}
*/
protected void paintBackground(Graphics graphics)
{
try
{
graphics.drawBitmap(0, 0, graphics.getScreenWidth(), graphics.getScreenHeight(),
ComunicadorCore.getGestorImagenes().getImagen("fondo.png").getBitmap(), 0, 0);
}
catch (ImagenException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
super.paintBackground(graphics);
}
};

 

Please use plain text.
New Developer
Chouman82
Posts: 28
Registered: ‎07-21-2008
My Device: Not Specified

Re: Problem background image Field

i am running into similar problem but yet not quite understand your resolution.  In your new VerticalFieldManager what is the paintBackground for? since the FieldManager doesn't extends Screen and the paintBackground method is in the Screen class, so not quite understanding how you were able to solve it.

 

Can you explain it more?

Please use plain text.
Developer
jllarraz
Posts: 69
Registered: ‎08-13-2008
My Device: Not Specified

Re: Problem background image Field

well, the paintBackground() is override from net.rim.device.api.ui.Field.paintBackground(). so all objects that extends FIeld has this method. when a field is draw by default paint the background of the field that is under it. You only have to put the background to the Main field manager of the screen and all of the fields that are in the manager appear whit this background. you must be careful with the VerticalScrollBar options, if you put this options bad when you shift down for the manager you will see that the background is shift across the manager and is possible that you see white background.

I hope that this explanation will be usefull to you. if you want to make me any question mor, i will try to respond it as better than i can.

Please use plain text.
New Developer
Chouman82
Posts: 28
Registered: ‎07-21-2008
My Device: Not Specified

Re: Problem background image Field

Hey jillarraz, I am not seeing the paintbackground method in Field class...
Please use plain text.
Developer
jllarraz
Posts: 69
Registered: ‎08-13-2008
My Device: Not Specified

Re: Problem background image Field

i think that you are seeing it in the api, you must override the method(i use eclipse) and then in the left of the line you will see a green triangle, then put the mouse up this triangle and you will see that says override method from .............

 

One thing more remember to call to the super method.

 

 

protected void paintBackground(Graphics graphics)
        {
                                       
                if(fondo!=null)
                {
                        graphics.drawBitmap(0, 0, graphics.getScreenWidth(), graphics.getScreenHeight(),
                                        fondo.getBitmap(), 0, 0);
                }
               
                super.paintBackground(graphics);
        }

Please use plain text.