Uønsket repaint-løkke
Hej!I forbindelse med udviklingen af en awt applikation (i JDK 1.1.8) benytter jeg en double buffered klasse, der "resizer" billedet fra 720x576 til 1024x576.
Mit problem er nu, at når jeg starter applikationen, bliver billedet gentegnet igen og igen i en uendelig løkke... :( (dvs. sætningen "Painting TPanel" bliver udskrevet hele tiden)
Her følger "resize"-klassen og klasse, den arver fra:
public class TResizePanel extends TPanel{
/** Creates a new instance of TResizePanel */
public TResizePanel() {
super();
}
/**
* null out the offscreen buffer as part of invalidation
*/
public void invalidate() {
super.invalidate();
offscreen = null;
}
/**
* override update to *not* erase the background before painting
*/
public void update(Graphics g) {
paint(g);
}
/**
* paint children into an offscreen buffer, then blast entire image
* at once.
*/
public void paint(Graphics g) {
super.repaint(); // needed to repaint components
if(offscreen == null) {
offscreen = createImage(1024,576); // Creates an off-screen drawable image
}
Graphics og = offscreen.getGraphics(); // Creates a graphics context for drawing to an off-screen image
og.setClip(0,0,1024,576); // Sets the current clip to the rectangle specified by the given coordinates
super.paint(og);
g.drawImage(offscreen, 0, 0, 720, 576, null); // must be after call to super to reduce flicker
g.dispose();
og.dispose();
}
}
public class TPanel extends HContainer{
Component currentContentPanel = null;
Image offscreen = null;
public TPanel(){
super();
setLayout(null);
}
public TPanel(int x, int y, int width, int height){
setBounds(x,y,width,height);
setLayout(null);
}
// only add one child component at a time
public void setContentPanel(Component contentPanel){
// if there is already a panel attached we remove it
if(currentContentPanel != null){
// currentContentPanel.setVisible(false);
remove(currentContentPanel);
}
// add the new contentPane
currentContentPanel = contentPanel;
Debug.println(this,"adding contentPanel");
add(contentPanel);
//contentPanel.setVisible(false);
// make sure the new panel is visible
//contentPanel.setVisible(true);
// make sure the panel is updated
//repaint();
}
public Component getContentPanel(){
return currentContentPanel;
}
/**
* paint children into an offscreen buffer, then blast entire image
* at once.
*/
public void paint(Graphics g) {
System.out.println("Painting TPanel");
super.paint(g);
}
// make sure all children are called whenever this panel is made visible
public void setVisible(boolean isVisible) {
// Call setVisible for the parent class
super.setVisible(isVisible);
Debug.println(this,"Recursively calling the children of a class");
int numberOfChildren = getComponentCount();
java.awt.Component [] children = getComponents();
for (int i = 0; i< numberOfChildren ; i++){
children[i].setVisible(isVisible);
}
}
}
