Skip to main content

How to set Background ?

2 replies [Last post]
terymas
Offline
Joined: 2007-09-03
Points: 0

Hi all, I'm very happy ! I have finished my first Xlet : )
Their is very simple... But it work fine.

Now, I don't know how set the background color : (

Who can help me ??

Many thanks,
Tery

My code ; )

import java.awt.Color;
import java.awt.Font;
import java.awt.Component;
import java.awt.Graphics;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;
import org.havi.ui.HGraphicsDevice;
import org.havi.ui.HBackgroundDevice;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HScreen;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.tv.xlet.*;
import org.davic.net.dvb.DvbLocator;
import org.havi.ui.*;
import java.awt.Color;

public class SampleXlet extends Component implements Xlet, KeyListener
{
private HScene hscene = null;
int PushEvent = 0;

private void DebugMessage(String strMsg)
{
System.out.println("CONSOLE MESSAGE: " + strMsg);
}
private HScene getHScene() {
if (hscene == null) {
HSceneFactory f = HSceneFactory.getInstance();
HScreen s = HScreen.getDefaultHScreen();
HGraphicsDevice gd = s.getDefaultHGraphicsDevice();
hscene = f.getFullScreenScene(gd);
setBounds(hscene.getBounds());
hscene.add(this);
}
DebugMessage("check constructor Xlet : Xlet constructor checked");
return hscene;
}

public void initXlet(XletContext ctx) throws XletStateChangeException {

addKeyListener(this);
getHScene().setVisible(true);
requestFocus();
DebugMessage("check init Xlet : Xlet initilized");
}

public void startXlet() throws XletStateChangeException
{
DebugMessage("check start Xlet : Xlet started");}

public void pauseXlet(){}

public void destroyXlet(boolean unconditional)
throws javax.tv.xlet.XletStateChangeException
{
DebugMessage("check destroy Xlet : Xlet destroy");
}
HBackgroundDevice backdevice;

public void paint(Graphics g)
{
DebugMessage("START paint");

g.setFont(new Font("Bitstream Vera Sans",Font.PLAIN,32));
g.setColor(Color.white);

int nLineNum = 12;
if (PushEvent ==1){
g.drawString("Remote Active : BTN 01", 50, 50 + nLineNum++ * g.getFont().getSize() + 10);}
else{
g.drawString("My Xlet", 50, 50 + nLineNum++ * g.getFont().getSize() + 10);}

DebugMessage("END paint");
}
public void keyPressed(KeyEvent evt) {
PushEvent = 0;
DebugMessage("START keyPressed");
int code = evt.getKeyCode();
switch (code) {

case KeyEvent.VK_0 :
break;

case KeyEvent.VK_1 :
PushEvent = 1;
break;
case KeyEvent.VK_2 :
break;
case KeyEvent.VK_3 :
break;
case KeyEvent.VK_4 :
break;
case KeyEvent.VK_5 :
break;
case KeyEvent.VK_6 :
break;
case KeyEvent.VK_7 :
break;
case KeyEvent.VK_8 :
break;
case KeyEvent.VK_9 :
break;
}
repaint();
DebugMessage("END keyPressed");
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bjoern_minkmar
Offline
Joined: 2007-06-11
Points: 0

Hi Terry,

you've got two choices:

1) set the background color of the whole HScene:

private HScene getHScene() {
...
setBounds(hscene.getBounds()); // old stuff
hscene.setBackground(Color.white); // new line -> set the color you want
hscene.add(this); // old stuff
}

2) set the background color of your component (SampleXlet)

private HScene getHScene() {
...
setBounds(hscene.getBounds()); // old stuff
this.setBackground(Color.white); // new line -> set the color you want
hscene.add(this); // old stuff
}

Cheers,
Björn

terymas
Offline
Joined: 2007-09-03
Points: 0

Hi Björn,

Thank you, for your answer !
I will test it...

Also, I have found a another way with paint.
But I think, it's not the best way for set the background color.

Regards,
Terry