Way to repaint this JPanel after given count

82 views Asked by At

Hello I would like to prevent graphics drawing and drawing again but I don't know how to do, I just want my panel delete all painted graphics and restart with same code. I tried some methods posted here but no one does the job.

     public class Main extends JPanel implements ActionListener {
Timer timer;
private double angle = 444;
private double scale = 1;
private double delta = 0.0001;
RoundRectangle2D.Float r = new RoundRectangle2D.Float();
int counter = 0;

public Main() {
    timer = new Timer(55, this);
    timer.start();
}

public void paint(Graphics g) {
    counter++;

    int h = getHeight();
    int w = getWidth();
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(randomNumber(0, 155), randomNumber(0, 255),randomNumber(0, 155), randomNumber(0, 255)));
    drawCircles(g2d, getWidth()/2, getHeight()/2, 250);
    if(counter > 200){
        g2d.clearRect (0, 0, getWidth(), getHeight());
        super.paintComponent(g2d);
        counter = 0;
    }
}

public int randomNumber(int min, int max) {

    int c = new Random().nextInt((max - min) + 1);
    return c;
}

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setUndecorated(true);

    Dimension dim = new Dimension(Toolkit.getDefaultToolkit()
            .getScreenSize().width, Toolkit.getDefaultToolkit()
            .getScreenSize().height);

    frame.setSize(dim);
    frame.setLocation(0, 0);
    frame.setBackground(new Color(0, 0, 0, 255));
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
void drawCircles(Graphics graphics, int xMid, int yMid, int radius) {
    // end recursion
    if(radius < 5)
        return;

    // Draw circle

    // start recursion
    //left
    drawCircles(graphics, xMid-radius, yMid, radius / 2);
    ((Graphics2D) graphics).rotate(angle);
    graphics.drawOval(xMid - radius, yMid - radius, radius * 2, radius * 2);

    //right
    drawCircles(graphics, xMid+radius, yMid, radius / 2);
    graphics.drawOval(xMid - radius, yMid - radius, radius * 2, radius * 2);

    ((Graphics2D) graphics).rotate(angle);

    ((Graphics2D) graphics).rotate(angle);
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
}

public void actionPerformed(ActionEvent e) {

    if (scale < 0.01) 
        delta = -delta;
    else if (scale > 0.99) 
        delta = -delta;
    scale += delta;
    angle += 0.001;
    repaint();
}
}
1

There are 1 answers

0
hfontanez On BEST ANSWER

I am not sure I understand you fully, but you can use a JToggleButton (for example) where is the toggle button is down it prevents drawing. I can see something like this inside your drawCircles() method:

void drawCircles(Graphics graphics, int xMid, int yMid, int radius)
{
    if(!toggleBtn.isSelected() // the toggle button is pressed
    {
        // draw something
    }
}

In your example, you are drawing two circles and two ovals. If I understood you correctly, you want to be able to pause in the middle of the method, for example, and only draw the first circle. Then, at some point, you want to continue drawing the two ovals and the remaining circle. Unfortunately, you cannot do that. You cannot stop (or pause) a method in the middle of it.

Methods have to execute to completion (whether to the end, or an exception is thrown). However, you can create some kind of task to draw ONE shape (for example, a circle). If you create multiple tasks, you can draw many circles. To accomplish this, you will need to learn about Concurrency and probably about Java Tasks. You can have these tasks execute in some kind of order and because of concurrency, you could pause and resume these drawing tasks the way I think you would want.