简体   繁体   English

在 Java 中画几秒钟的图像

[英]Draw image for few seconds in Java

How can I draw an image that would be available for just a few seconds?如何绘制仅几秒钟可用的图像?

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
if (dots % 3 == 0 && dots != 3){
                g.drawImage(clocks, clocksX, clocksY, this);
            }
}

Thanks谢谢

Painting of a component is done by setting properties of the component.组件的绘制是通过设置组件的属性来完成的。

So if you want to control whether an image is or isn't painted you would need an instance variable in your class that does the custom painting, lets say drawImage .因此,如果您想控制是否绘制图像,则需要在 class 中使用一个实例变量来进行自定义绘制,比如说drawImage Then you would need a method to change the state of the variable:然后您需要一种方法来更改变量的 state:

public void setDrawImage(boolean drawImage)
{
    this.drawImage = drawImage;
    repaint();
}

So know your paintComponent() method would look like:所以知道你的 paintComponent() 方法看起来像:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

    if (drawImage)
        g.drawImage(clocks, ...);
}

So now if you want animation you would uses a Swing Timer.所以现在如果你想要 animation 你会使用 Swing 定时器。 So the code would be something like:所以代码会是这样的:

Timer timer = new Timer(3000, (e) -> 
{
    yourComponent.setDrawImage(false);
    
    Timer timer2 = (Timer)e.getSource();
    timer2.stop();
};

yourComponent.setDrawImage(true);
timer.start();   

Read the section from the Swing tutorial onHow to Use Swing Timers for more information.阅读 Swing 教程中有关如何使用 Swing 定时器的部分以获取更多信息。

Thank you.谢谢你。

private int t = 0;
        if (dots % 3 == 0 && dots != 3){
            if(t < 2400){
                g.drawImage(clocks, clocksX, clocksY, null);
            }
            Timer timer1 = new Timer(3000, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    t += 110;

                    if (t >= 10000) {
                        ((Timer)e.getSource()).stop();
                        t = 100000;
                    }
                    repaint();
                }
            });
            System.out.println(t); //its for to test my timer1
            timer1.start();
             if (t >= 30000){
                 timer1.stop();
                 t = 0;
             }
        }

And its work.及其工作。 But when t > 30000, its actually dont stop.但是当 t > 30000 时,它实际上并没有停止。 And timer2, not working as it should.和 timer2,不能正常工作。 Maybe because, in this class, where I want to execute this method, there is already another timer?可能是因为,在这个class,我想执行这个方法的地方,已经有另一个定时器了? Maybe because of this?也许是因为这个?

I myself answered my question)) If I already had a timer, I use it.我自己回答了我的问题))如果我已经有一个计时器,我会使用它。 Here is what I wrote and it works:这是我写的,它有效:

if (dots % 3 == 0 && dots != 3) {
            t += 300; 
            if (t < 5100) {
                g.drawImage(clocks, clocksX, clocksY, null);
            }
        }

        if (dots % 3 != 0) {
            t = 0;
        }

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

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