-2

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);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    You should read about how the painting system works [here](http://www.oracle.com/technetwork/java/painting-140037.html), then come back with any additional questions. For all intents and purposes you should consider paintComponent to be an atomic operation. – Mad Physicist Sep 23 '17 at 04:01

2 Answers2

0

I am running the algorithms within the paint component extending JComponent. (not the best, but I don't care)

But you should care, since this impacts on your problem and possible solution.

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?

Then don't run the algorithm through paintComponent. Instead use a SwingWorker<Void, Image>, update a BufferedImage and pass that image to the GUI through the worker's publish/process method pair, calling repaint() at the same time. For more on how to use a SwingWorker, please have a look at: Lesson: Concurrency in Swing

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-2

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();
    }
}); 
forqzy
  • 389
  • 2
  • 11
  • If i call the repaint method in the paint component, wont that make it a recursive function? – Morris Sowards Sep 23 '17 at 04:09
  • How will calling `repaint` solve anything? @MorrisSowards: you're right that you shouldn't call `repaint()` from within a painting method, but painting manager is not going to let this become recursive. – Hovercraft Full Of Eels Sep 23 '17 at 04:12
  • @MorrisSowards: but more to the point, why are you commenting to forqzy's answer, one that is off base, and not to my answer, an answer that is actually practical -- to use a SwingWorker? – Hovercraft Full Of Eels Sep 23 '17 at 04:13
  • @MorrisSowards: Again, the solution is to use a background thread such as a SwingWorker, not to do what forqzy suggests. You've accepted an incorrect answer and have reduced the utility of this site in the process. Shame. – Hovercraft Full Of Eels Sep 23 '17 at 04:37
  • @Hovercraft Full Of Eels What I propose is to running the logical in another thread, and repaint in Swing thread. – forqzy Sep 23 '17 at 04:56
  • @forqzy: the purpose is to 1) put long-running code off the Swing event thread so as not to block it and prevent it from doing necessary functionality such as user interaction and painting, and 2) to separate the model from the view. I assume that you're familiar with the MVC design pattern, so my suggestion should not come as any surprise to you, right? – Hovercraft Full Of Eels Sep 23 '17 at 04:58
  • Yes, your understanding is correct. I think he is not familiar with the repaint(). I have the same stock program need to repaint for several different lines, so I think he is stuck there. He don't understand how to repaint(). Repaint only happen when updateUI/packs/invalidate calls first, then call repaint will effect. And repaint should be an atomic method. – forqzy Sep 23 '17 at 05:03
  • Please [edit] and improve your answer so that I can at least remove my down-vote. Make it helpful for all future visitors. – Hovercraft Full Of Eels Sep 23 '17 at 05:12