在计时器上绘制图像

时间:2012-10-22 12:48:24

标签: java image netbeans timer

我正在用java创建一个xlet应用程序,在一个场景中我现在有一个鼠标,当我输入正确的键时。但现在我希望它继续使用计时器,我现在拥有的是一个计时器,并在刻度线上打印我的图像的位置,它确实正确的x和y更改。但是没有画出图像,有人能帮帮我吗?这是我有的3个文件:

这是hellotvxlet类(主要的):

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;


        }
        }

    }


}

这是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(){

    }
    */

}

这是计时器类:

/*
 * 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();
}
}

1 个答案:

答案 0 :(得分:1)

我没有看到任何导致刷新显示的内容。我认为您需要从计时器运行方法调用“repaint()”,以便刷新显示。