使对象渲染到重绘的一半
Make an object render half way through a reapint
我目前正在制作一个动画来比较两种股票交易算法。我是 运行 扩展 JComponent 的绘制组件中的算法。 (不是最好的,但我不在乎)我需要在绘制组件中途刷新屏幕。我不希望它必须在更新屏幕之前一直通过。原因是我有一个带有嵌套 while 循环的算法,而另一个没有。我该怎么做?
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);
}
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?
那就不要运行算法通过paintComponent。而是使用 SwingWorker<Void, Image>
,更新 BufferedImage 并通过 worker 的 publish/process 方法对将该图像传递给 GUI,同时调用 repaint()
。有关如何使用 SwingWorker 的更多信息,请查看:Lesson: Concurrency in Swing
没看懂half way是什么意思
如果是指一半的组件,那么简单的方法就是使用两个JComponent。
如果你表示在同一个组件中更新了一行但未更新另一行。
据我了解,重绘是packs()、updateUI()时调用的,或者是invalidate()引起的。
所以在我看来,repaint()应该只关心paint这些lines/2D,而在另一个线程中去执行这些loops/generate这些数据。数据收集完成后,只需调用 updateUI()/paintImmediately。
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
repaint();
}
});
我目前正在制作一个动画来比较两种股票交易算法。我是 运行 扩展 JComponent 的绘制组件中的算法。 (不是最好的,但我不在乎)我需要在绘制组件中途刷新屏幕。我不希望它必须在更新屏幕之前一直通过。原因是我有一个带有嵌套 while 循环的算法,而另一个没有。我该怎么做?
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);
}
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?
那就不要运行算法通过paintComponent。而是使用 SwingWorker<Void, Image>
,更新 BufferedImage 并通过 worker 的 publish/process 方法对将该图像传递给 GUI,同时调用 repaint()
。有关如何使用 SwingWorker 的更多信息,请查看:Lesson: Concurrency in Swing
没看懂half way是什么意思
如果是指一半的组件,那么简单的方法就是使用两个JComponent。
如果你表示在同一个组件中更新了一行但未更新另一行。
据我了解,重绘是packs()、updateUI()时调用的,或者是invalidate()引起的。 所以在我看来,repaint()应该只关心paint这些lines/2D,而在另一个线程中去执行这些loops/generate这些数据。数据收集完成后,只需调用 updateUI()/paintImmediately。
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
repaint();
}
});