i'm making an xlet app with java, in a scene i now have an image that mouse when i enter the right keys. but now i want it to move on a timer, what i have now is a timer and on the ticks it prints the location of my image, and it does that correct zo x and y change. but the image isn't drawn, can someone help me? here are the 3 files i have:
this is the hellotvxlet class (the main thing):
package hellotvxlet;
import javax.tv.xlet.*;
import org.havi.ui.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import org.bluray.ui.event.HRcEvent;
import org.dvb.event.EventManager;
import org.dvb.event.UserEventListener;
import org.dvb.event.UserEventRepository;
import org.dvb.ui.*;
import java.util.Timer;
//import org.dvb.ui.*;
//import java.awt.event.ActionEvent;
//import org.havi.ui.event.HActionListener;
public class HelloTVXlet implements Xlet, UserEventListener{
private XletContext actueleXletContext;
public HScene scene;
int waardex = 20;
int waardey = 20;
// x; y;
MijnComponent mc = new MijnComponent("bitmap1.jpg",waardex,waardey);
// debuggen of niet ?
private boolean debug=true;
public HelloTVXlet(){
}
public void initXlet(XletContext context) throws XletStateChangeException {
if(debug) System.out.println("Xlet initialiseren");
this.actueleXletContext = context;
HSceneTemplate sceneTemplate = new HSceneTemplate();
sceneTemplate.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new HScreenDimension(1.0f, 1.0f), HSceneTemplate.REQUIRED);
sceneTemplate.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new HScreenPoint(0.0f,0.0f), HSceneTemplate.REQUIRED);
scene = HSceneFactory.getInstance().getBestScene(sceneTemplate);
scene.add(mc);
}
public void startXlet () throws XletStateChangeException {
MijnTimerTask objMijnTimerTask=new MijnTimerTask ( );
Timer timer = new Timer();
timer.scheduleAtFixedRate(objMijnTimerTask ,0, 100);
//star t na 0ms, elke 100ms
if(debug) System.out.println("Xlet starten");
EventManager manager = EventManager.getInstance();
UserEventRepository repository = new UserEventRepository("voorbeeld");
repository.addKey( org.havi.ui.event.HRcEvent.VK_UP);
repository.addKey( org.havi.ui.event.HRcEvent.VK_DOWN);
repository.addKey( org.havi.ui.event.HRcEvent.VK_RIGHT);
repository.addKey( org.havi.ui.event.HRcEvent.VK_LEFT);
manager.addUserEventListener(this, repository);
scene.validate();
scene.setVisible(true);
}
public void pauseXlet() {
}
public void destroyXlet(boolean unconditional)throws XletStateChangeException {
}
public void userEventReceived(org.dvb.event.UserEvent e){
if(e.getType() == KeyEvent.KEY_PRESSED){
System.out.println("pushed button");
switch (e.getCode()){
case HRcEvent.VK_UP:
System.out.println("vk up");
waardey --;
mc.setLocation(waardex, waardey);
break;
case HRcEvent.VK_DOWN:
System.out.println("vk down");
waardey ++;
mc.setLocation(waardex, waardey);
break;
case HRcEvent.VK_RIGHT:
System.out.println("vk right");
mc.setLocation(waardex, waardey);
waardex ++;
break;
case HRcEvent.VK_LEFT:
System.out.println("vk left");
mc.setLocation(waardex, waardey);
waardex --;
break;
}
}
}
}
This is MijnComponent:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hellotvxlet;
import org.havi.ui.*;
import java.awt.*;
import org.dvb.ui.*;
/**
*
* @author student
*/
public class MijnComponent extends HComponent{
private Image bmap;
private MediaTracker mtrack;
public MijnComponent(String bitmapnaam, int x, int y){
bmap = this.getToolkit().getImage(bitmapnaam);
mtrack = new MediaTracker(this);
mtrack.addImage(bmap,0);
try{
mtrack.waitForAll();
}
catch(Exception e){
System.out.println(e.toString());
}
this.setBounds(x,y,bmap.getWidth(null),bmap.getWidth(null));
}
public void paint (Graphics g){
java.awt.Image imgjpg = this.getToolkit().getImage("bitmap1.jpg");
g.drawImage(bmap, 0,0,null);
}
/* public void MoveLeft(){
}
public void MoveRight(){
}
public void MoveUp(){
}
public void MoveDown(){
}
*/
}
and this is the timer class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hellotvxlet;
import java.util.TimerTask;
/**
*
* @author student
*/
public class MijnTimerTask extends TimerTask {
HelloTVXlet xlet = new HelloTVXlet();
int waardex = 20;
int waardey = 20;
// x; y;
public void run ( )
{
System.out.println(xlet.mc.getLocation());
xlet.waardex ++;
xlet.mc.setLocation(xlet.waardex, xlet.waardey);
//int appelsap = (int) xlet.mc.getAlignmentX();
}
}
I don't see anything that causes the refresh of the display. I think you need to call "repaint()" from the timer run method so that the display is refreshed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With