Search code examples
javaswingpaintcomponentrepaintjcomponent

Make an object render half way through a reapint


I am currently working on an animation to compare two stock exchange algorithms. I am running the algorithms within the paint component extending JComponent. (not the best, but I don't care) I need to have the screen refresh half way through the paint component. I do not want it to have to get all the way through before it up dates the screen. The reason being is I have one algorithm with a nested while loop and the other without. How would I go about doing this?

public void paintComponent(Graphics g) {            
    //calls in the super class and calls the calibrate the graphics method
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    calibrateFrame( getHeight(), getWidth() );

    //Clears the rectangle to avoid overlaying, makes it black
    g2D.clearRect(0, 0, getWidth(), getHeight());
    g2D.setColor(Color.BLACK);
    g2D.fillRect(0, 0, getWidth(), getHeight());

    //Draws the rectangles without the algorithm started
    redraw(g2D, -1);


    /**
     *algorithms
     */
    fastSpans[0] = 1;
    slowSpans[0] = 1;

    //Makes a new stack pushes 0 on the stack
    Stack myStack = new Stack();
    myStack.push(0);

    //If it has not been sorted or paused, continue the algorithm
    if (!(pause) && !(sorted)){

        //The slower algorithm needs to start out at zero (j)
        int j = indexValue-1;

        g2D.setColor(Color.BLUE);

        //Calculates the values for the X and Y coordinates for the 
        //new rectangle, along with the height
        int slowY = calSlowY(j);
        int slowX = calSlowX(j);
        int curHeightSlow = (int) ((stocks[j]/maxStockValue)*maxHeight);

        //Here is the actual algorithm
        int k = 1;
        boolean span_end = false;
        //Nested While Loop
        while (((j-k)>0) && !span_end){
            if (stocks[j-k] <= stocks[j]){
                k = k + 1;
                // Draw the current component
                // **********************
                // DO REFRESH MID PAINT COMPONENT
            }
            else{ span_end = true; }
        }
        slowSpans[j] = k;
        g2D.setColor(Color.WHITE);
        for(int i = 0; i < numberOfStock ; i++){


        }

        if (!(indexValue >= numberOfStock)){
            while (!( myStack.empty()) && (stocks[(int)myStack.peek()]) <= stocks[indexValue]){
                myStack.pop();
            }
            if (myStack.empty()){
                fastSpans[indexValue] = indexValue + 1;

            }
            else {
                fastSpans[indexValue]= indexValue - (int) myStack.peek();
                //System.out.println("Im in the else");
            }
            myStack.push(indexValue);
        }
    }
    drawStrings(g2D);
}

Solution

  • Don't understand what half way means.

    If it means half of the component, then the simple way is to using two JComponent.

    If u means in same component one line updated but another line not updated.

    What I understand the repaint is calling when it packs(), updateUI(), or caused by invalidate(). So in my view, the repaint() should only care about paint these lines/2D, and in another thread to execute these loops/generate these data. Once the data collection is finished just call updateUI()/paintImmediately.

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            repaint();
        }
    });