Ich arbeite derzeit an einer Animation, um zwei Börsenalgorithmen zu vergleichen. Ich führe die Algorithmen innerhalb der Farbkomponente aus, die JComponent erweitert. (nicht die beste, aber das ist mir egal) Ich muss den Bildschirm halb durch die Farbkomponente aktualisieren lassen. Ich will nicht, dass es den ganzen Weg durchmacht, bevor es auf den Bildschirm geht. Der Grund dafür ist, dass ich einen Algorithmus mit einer geschachtelten While-Schleife und den anderen ohne. Wie würde ich das machen?Machen Sie ein Objekt rendern auf halbem Wege durch einen Reapint
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);
}
Sie darüber lesen sollten, wie die Lackieranlage arbeitet [hier] (http://www.oracle.com/technetwork/java/painting-140037.html), dann mit weiteren Fragen zurückkommen. Für alle Absichten und Zwecke sollten Sie paintComponent als eine atomare Operation betrachten. –