Java 动态重绘面板
我想做一个程序,展示一个水池如何充满时间的动画。问题是我不能让它每次都刷新。。。代码如下:它可以工作,但不会在循环中刷新,但如果我手动调整窗口大小,我可以在面板中看到它的新绘制:Java 动态重绘面板,java,swing,paint,graphics2d,Java,Swing,Paint,Graphics2d,我想做一个程序,展示一个水池如何充满时间的动画。问题是我不能让它每次都刷新。。。代码如下:它可以工作,但不会在循环中刷新,但如果我手动调整窗口大小,我可以在面板中看到它的新绘制: static class _cistern extends JPanel{ private volatile double _maxCapacity; private double _flux; private volatile double currentStorage = 0.0;
static class _cistern extends JPanel{
private volatile double _maxCapacity;
private double _flux;
private volatile double currentStorage = 0.0;
private boolean IsStarted = false;
private volatile int Up = 1;
private Thread Fill = new Thread(new Runnable(){
@Override
public void run(){
while(currentStorage < _maxCapacity){
currentStorage += _flux;
try{Thread.sleep(500);}catch(Exception ex){}
repaint();
}
}
});
public _cistern(double MaximunCapacity, double Flux, double CurrentStorage){
setPreferredSize(new Dimension(800, 600));
setBackground(Color.BLACK);
IsStarted = true;
Fill.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.drawOval(getWidth()/20, getHeight()/15, getWidth() - getWidth()/10, getHeight() - getHeight()+20);
g2.drawLine(getWidth()/20, getHeight()/15+10, getWidth()/20, (getHeight() - getHeight()/15)-10);
g2.drawOval(getWidth()/20, (getHeight() - getHeight()/15 - 20), getWidth() - getWidth()/10, getHeight() - getHeight()+20);
g2.drawLine(getWidth() - getWidth()/20, getHeight()/15 +10, getWidth() - getWidth()/20, (getHeight() - getHeight()/15)-10);
if(IsStarted){ //I know I have some drawings wrong here
//I can fix them but I just want this to refresh dinamically. The problema is the REFRESH PAINT.
//If you test code and see bad drawing, that's not the problema, I want to see it refreshing :P, that's the question. Don't fix this
g2.setColor(Color.BLUE);
g2.drawOval(getWidth()/20, (getHeight() - getHeight()/15 - 20) - Up++, getWidth() - getWidth()/10, getHeight() - getHeight()+20 - Up++);
g2.drawLine(getWidth()/20, (getHeight() - getHeight()/15)-10, getWidth()/20, (getHeight() - getHeight()/15)-10);
g2.drawLine(getWidth() - getWidth()/20, getHeight()/15 +10, getWidth() - getWidth()/20, (getHeight() - getHeight()/15)-10);
g2.drawOval(getWidth()/20, (getHeight() - getHeight()/15 - 20), getWidth() - getWidth()/10, getHeight() - getHeight()+20);
}
}
}
谢谢:p看了我的代码一段时间后,我想起我正在使用一个线程调用和EDT。。。所以我必须补充一点:
private Thread Fill = new Thread(new Runnable(){
@Override
public void run(){
while(currentStorage < _maxCapacity){
currentStorage += _flux;
try{Thread.sleep(500);}catch(Exception ex){}
SwingUtilities.invokeLater(new Runnable(){
public void run(){
revalidate();
repaint();
}
});
}
}
});
使用Swing计时器而不是线程,这样更安全……您可能希望进行通读,这将使人们更容易阅读您的代码,您也更容易阅读其他代码。不要从paintComponent方法中修改组件的状态,绘制是为了绘制。调用paintComponent可能有多种原因,导致不可预知的结果当我一直在处理您的代码时,您应该知道Java graphics对负值的响应不好…@AndrewThompson哦,是的!重绘是Swing中为数不多的线程安全方法之一。重新验证与您的情况无关,如果您不想更新容器的布局hierarchy@MadProgrammer大概我需要使用线程,而不是计时器,因为这是一个简单的家庭作业。我真的不喜欢java,但无论如何我都要学习它,尽可能地学习一些有用的东西。我知道使用重绘是不好的,但正如所说的,在持续快速更新中重绘更好我的原始循环每秒更新10次,而不是本例中的500毫秒,我应该使用它而不是更新。无论如何,谢谢你!!