#Delaying the repainting between 2 called methods
16 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @chilly mason! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
What’s the try catch way
he probably meant a Thread.sleep
Oooh yeah that would make sense
If you were sleeping the EDT then your draw calls never actually get run. To add delay into your project you have to use a swing timer or something like that
Is there more context there? ActionPerformed is a method that is called, for example, when a button is pressed
Or some other action occurs
So what is this action listener attached to in your case?
Oh
Like for the timer?
Lmao my bad I forgot how they worked for a sec
could you just explain what you are generally trying to do ? Why do you need the 2 sec delay ?
this is not a nice and clean solution but it will work.
public class gezearte extends JPanel {
Graphics e;
public gezearte() {
JFrame frame = new JFrame();
frame.getContentPane().add(this);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
e = getGraphics();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(this::printCircles); //Runs the printCircles method on the EDT
javax.swing.Timer t = new Timer(2000, e -> printCube()); //Creates a new timer with a delay of 2 seconds which will execute the print Cube method
t.setRepeats(false); //Timer will only execute once
t.start(); //Start the timer
}
public void printCircles() {
super.paintComponent(e);
Graphics2D e = (Graphics2D) this.e; //Cast to Graphics2D to allow next Step to work
e.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); //Enable AntiAliasing to smoothen the corners
e.setColor(Color.black);
e.drawOval(75, 75, 50, 50);
e.drawOval(75, 125, 50, 50);
e.drawOval(125, 75, 50, 50);
e.drawOval(125, 125, 50, 50);
}
public void printCube() {
super.paintComponent(e);
e.setColor(Color.black);
e.drawRect(100, 100, 50, 50);
e.drawLine(100, 100, 125, 75);
e.drawLine(150, 100, 175, 75);
e.drawRect(125, 75, 50, 50);
e.drawLine(100, 150, 125, 125);
e.drawLine(150, 150, 175, 125);
}
public static void main(String[] args){
new gezearte();
}
}
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.