简体   繁体   English

Java图形无法在计时器中工作

[英]Java graphics not working in timer

I am programming a tiny game in java. 我正在用java编写一个小游戏。 For that I want to print a dialog-text-box with some text in it to talk to people inside the game. 为此,我想打印一个对话框文本框,其中包含一些文本,以便与游戏中的人交谈。

    public static void printConBox(Graphics g, String firstLine, String secondLine, String thirdLine, String fourthLine){
    g.setColor(Color.WHITE);
    g.fillRect(x, y, 600, 180);
    g.setColor(Color.BLACK);
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){

        @Override
        public void run() {
            if(i <= firstLine.length() - 1){
                char c = firstLine.charAt(i);
                i++;
                String s = "" + c;
                System.out.print(s);
                g.drawString(s, xdif, y + 25);
                xdif += 15;
            }else{
                timer.cancel();
            }
        }

    }, 0, 100);}

Don't worry about the graphics, this is working because the programm is painting the white Text-Box. 不要担心图形,这是有效的,因为该程序正在绘制白色文本框。 But the letters in the String 'firstLine' arent there. 但字符串'firstLine'中的字母不在那里。 In run this should be an animation for the text! 在运行中,这应该是文本的动画!

I think this has to do with der Timer! 我认为这与der Timer有关! But the printing in the output-Console is working perfectly! 但输出控制台中的打印效果非常好! I also try it with Thread.sleep in a for-loop, but still not working... 我也尝试在for循环中使用Thread.sleep,但仍然无法正常工作......

Any ideas?... 有任何想法吗?...

PS.: I have use paintComponent and that, so the graphics is working believe me ;) PS。:我使用paintComponent,所以图形工作相信我;)

    public void actionPerformed(ActionEvent e){ 
    if(Conversation.i <= firstLine.length() - 1){
        char c = firstLine.charAt(Conversation.i);
        Conversation.i++;
        String s = "" + c;
        System.out.print(s);
        g.drawString(s, Conversation.xdif, Conversation.y + 25);
        Conversation.xdif += 15;
        Conversation.timer.restart();
    }else{
        Conversation.timer.stop();
    }
}

But how do I get the Graphics in der actionPerformed, the Timer works with? 但是如何在动作中获得图形,Timer可以使用? By the way the System.out is working angain! 顺便说一句,System.out正在努力工作! Nice... ;) 不错......;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM