繁体   English   中英

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

[英]Java graphics not working in timer

我正在用java编写一个小游戏。 为此,我想打印一个对话框文本框,其中包含一些文本,以便与游戏中的人交谈。

    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);}

不要担心图形,这是有效的,因为该程序正在绘制白色文本框。 但字符串'firstLine'中的字母不在那里。 在运行中,这应该是文本的动画!

我认为这与der Timer有关! 但输出控制台中的打印效果非常好! 我也尝试在for循环中使用Thread.sleep,但仍然无法正常工作......

有任何想法吗?...

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

但是如何在动作中获得图形,Timer可以使用? 顺便说一句,System.out正在努力工作! 不错......;)

暂无
暂无

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

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