#How do I delay a drawing/graph?

1 messages · Page 1 of 1 (latest)

woeful pilot
#

Ok so I've been attempting to do this for like a whole week and have not even gotten close so im here to take a crack at asking around. I'm pretty new to java too so I appreciate if anyone can help me on this.

I'm building a "falling sand" project where I can make sand fall as well as water and add other elements. I want to make a clear screen button where some lasers go through each row and clears the screen. I want to add a delay between each laser by like 1/5 of a second maybe. Problem is I have no clue how to do this. I tried using Thread.sleep() but it froze the whole graph and didn't do anything. I just found the SwingTimer and the Timer class and I'll look into it but for now I'm lost and need some guidance.

sage forgeBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

assume you are referring to delaying the rendering of a drawing or graph in a Java application. To achieve this, you can use the Thread.sleep() method to pause the execution of your program for a specified amount of time.

Here's an example of how you can delay a drawing/graph in Java:

import javax.swing.*;
import java.awt.*;

public class DelayedDrawing extends JPanel {

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

        // Draw your graph or any other drawing here

        // Delay the rendering for 1 second (1000 milliseconds)
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Continue with the rest of your code
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Delayed Drawing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        DelayedDrawing panel = new DelayedDrawing();
        
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

In this example, we override the paintComponent() method from JPanel and add our drawing code inside it. After that, we call Thread.sleep() to pause the execution for 1 second (1000 milliseconds). Finally, we continue with the rest of our code.

Note that calling Thread.sleep() inside the Event Dispatch Thread (EDT), which is responsible for handling UI events, may freeze your application's UI. To avoid this, you can use a separate thread or utilize Swing Timer for more precise control over animations and delays.

I hope this helps! Let me know if you have any further questions.

haughty bough
#

awesome advice

slate pendant
#

which means while sleep is executing, the user won't be able to interact with the GUI. they wouldn't even be able to close/X out of the app

#

if that's not a problem for the OP, then sure. but in most situations, you'd want the user to be able to close your app

slate pendant
unreal epoch
#

nah it took your question like u asked it. Gpt needs more specific terms and details for a better answer

woeful pilot
#

Works