kacper derwinski
kacper derwinski

Reputation: 1

How to use paintComponent in loop?

I'm trying to draw many shapes using paint component method, but I've gotten an undesirable outcome. So… for a few seconds my frame is white and after 'Draw'== 0 it shows all my shapes in one moment. I would like to draw one shape, then wait 500ms, then draw another shape without removing previous one.

public package GUI;

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


public class okno3 extends JPanel{
String sb="";
int pozx,pozy,pozx2,pozy2,r,s,krztalt;
Color c;
int Draw = 3;

public static void main(String args[]){
    JFrame Frame = new JFrame();
    okno3 okno = new okno3();
    Frame.setSize(768,768);
    Frame.setTitle("Projetk 3");
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setVisible(true);
    Frame.add(okno);
}

public void paintComponent(Graphics g) {
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(getForeground());

    while(this.Draw>0) {
        this.Draw--;
        this.c = new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
        g.setColor(c);
        this.pozx = (int)(getWidth()*Math.random());
        this.pozy = (int)(getHeight()*Math.random());
        this.pozx2 = (int)(getWidth()*Math.random());
        this.pozy2 = (int)(getHeight()*Math.random());
        this.r =(int)(Math.random()*75)+25;
        this.s =(int)(Math.random()*75)+25;

        this.krztalt = (int)(Math.random()*3)+1;

        switch(krztalt) {
            case 1:
                g.drawOval(pozx, pozy, r, s);
            break;
            case 2:
                g.drawRect(pozx, pozy, r, s);
            break;
            case 3:
                g.drawLine(pozx, pozy, pozx2, pozy2);
            break;
        }
        try{
            FileWriter fstream = new FileWriter("Wynik.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            String sb = krztalt + " " + pozx + " " + pozy + " " + r + " " + s + " " + c;
            this.sb = sb + "\n" + this.sb;
            fstream.write(this.sb);
            fstream.close();

            out.close();
            }
        catch (Exception e){
            System.out.println("Error: " + e.getMessage());
        }
        try{
            Thread.sleep(500);
        }

        catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
}

Upvotes: 0

Views: 1059

Answers (1)

WJS
WJS

Reputation: 40064

Do not sleep inside a paintComponent method or any other method that runs in the EDT. The EDT (Event Dispatch Thread) is a single thread used to process events and other sequential tasks such as painting. So none of your changes will show until after you leave paintComponent.

You need to do all of your calculations outside of paintComponent and then every 500 milliseconds, invoke repaint() and have the paintComponent repaint them. It is best to use a Swing timer to wait between repaint calls.

Check out Custom Painting in the Java Tutorials and search this site for more information on painting and the Event Dispatch Thread.

Upvotes: 1

Related Questions