0

I have a rectangle, and I am trying to grow it like a graph of some sorts, but it does not show it growing in real time, it just has a white screen then I see a rectangle. Any help would be appreciated, thanks. The code I am having a problem with is under the ¨Animates the bar¨ comment.

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

public class Main extends JPanel {
    static String[] mainArr;
    static int start;
    static boolean done = false;
    static double datapoint1;
    static double datapoint2;
    static int jPlaceholder;

    public static void main(String[] args) throws Exception {

        // Creating the window
        JFrame panel = new JFrame();
        panel.setSize(450,250); 

        // Creating the window that shows the animation
        JFrame drawingFrame = new JFrame();
        drawingFrame.setSize(450,250);
        JPanel jp = new JPanel();
        jp.setLayout(null);
        jp.setBackground(Color.red);
        drawingFrame.add(jp);

        // Creating all the text fields
        JTextField dataTypesTextField = new JTextField("This box is currently not in use. Please do not type anything into this box");
        dataTypesTextField.setBounds(50,50, 400,30);
        panel.add(dataTypesTextField);
        JTextField yearStartTextField = new JTextField("Type in this box what year your data starts in:");
        yearStartTextField.setBounds(50,100, 400,30);
        panel.add(yearStartTextField);
        JTextField yearEndTextField = new JTextField("Type in this box what year your data ends in:");
        yearEndTextField.setBounds(50,150, 400,30);
        panel.add(yearEndTextField);

        // Creating the button to submit the data
        JButton enterButton = new JButton("Enter");
        enterButton.setBounds(50,200, 100, 30);
        panel.add(enterButton);

        // =================================== ActionListener for enter button ========================================

        enterButton.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {

                if (done==false) {
                    // Creating the variables to store the data the user just inputted
                    start = Integer.parseInt(yearStartTextField.getText());
                    int end = Integer.parseInt(yearEndTextField.getText());
                    mainArr = new String[end-start+1];

                    // Gets the data points
                    dataTypesTextField.setText("Datapoints you will use in order, space between each: ");
                    done = true;
                } else {
                    // Getting all the data needed
                    mainArr = dataTypesTextField.getText().split(" ");
                    double[] datapoints = new double[mainArr.length];
                    for (int i=0; i<datapoints.length; i++) {
                        datapoints[i] = Double.parseDouble(mainArr[i]);
                    }

under here is where I had my problems I am pretty sure, but I could have screwed up somewhere else.

                    // Animates the bar
                    for (int i=0; i<datapoints.length-1; i++) {

                        // Getting all the datapoints
                        datapoint1 = datapoints[i];
                        datapoint2 = datapoints[i+1];
                        int j = 0;
                        while(j<50) {
                            j++;
                            int width = (int) (datapoint1+((datapoint2-datapoint1)/50)*j);
                            JPanel rectangle = new JPanel();
                            rectangle.setBackground(Color.black);
                            rectangle.setBounds(50, 50, width, 30);
                            jp.add(rectangle);
                            drawingFrame.setVisible(true);
                            rectangle.repaint();
                            System.out.println("The width is: "+width);

at first I thought it was because there was no pause between each ¨frame¨ but it still just shows a white screen, then it shows the rectangle.

                            try {
                                Thread.sleep(20);
                            } catch (Exception exp) {

                            }
                            
                        }
                    }
                }
            }
        });

        // =====================================================================================================
        
        // Finishes up both the windows
        panel.setLayout(null);
        panel.setVisible(true);

    }
}
  • 1
    If the code you've shown is complete, at least the loop with the "animates the bar" comment, then you're doing it on the event dispatch thread (you should definitely [read up on it](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)). Put the loop into a SwingWorker (i.e. a separate thread) and it should work if the rest of your code is correct (didn't check that). For the SwingWorker have a look at [How do I use SwingWorker in Java?](https://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java) – Thomas Apr 26 '21 at 12:20
  • The structure of your code is incorrect. The main() method should only be used for creating the frame and add a component to the frame. You should not be using static variables. You should not be extending JPanel, unless you actually add logic the that class. Check out: https://stackoverflow.com/a/33907282/131872 and https://stackoverflow.com/a/54028681/131872 and https://stackoverflow.com/q/64196198/131872 for different example using animation. – camickr Apr 26 '21 at 15:10

0 Answers0