sun.awt.noerasebackground under Linux/X11

Hi you all,

i have to following problem:

Im trying to add an awt.Panel with implemented double-buffering to an awt.Frame and give the user the possibility to freely set the Panels position and size inside the container.

It all works fine but the panel "flickers" while beeing resized, due to its background being cleared all the time.

I can turn this off by setting the JVM-property "sun.awt.noerasebackground" to "true", but for me this only works under MS Windows (XP).

On my Linux installation the property has no effect at all. I tried a few other properties and settings but that doesnt help.

Here is some sample-code that demonstrates the case.

Drag on the red rectangle to change its size.

Use of settings and properties can be customized by changing the static variables.

With the given variable-settings it looks perfect under win and flickers under linux. if you set SET_AWT_NOERASEBACKGROUND_PROPERTY_TO_TRUE to false it flickers under both systems.

( The awt.Frame which is in use here, also implementes double-buffering but this is not neccessary to see the described effect. It also appears using a simple awt.Frame)

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Panel;

import java.awt.Point;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.image.BufferedImage;

/**

* Class DBFrame implemenents a double-buffered AWT-Frame

* @author Example

*/

publicclass DBFrameextends Frame

{

/** Switch this on/off to see the effect*/

privatefinalstaticboolean SET_AWT_NOERASEBACKGROUND_PROPERTY_TO_TRUE=true;

privatefinalstaticboolean SET_AWT_ERASEBACKGROUNDONRESIZE_PROPERTY_TO_FALSE=false;

privatefinalstaticboolean SET_DYNAMIC_LAYOUT_TO_TRUE=false;

private BufferedImage bufferImage =null;

/** Main-method*/

publicstaticvoid main(String[] args)

{

if (SET_AWT_NOERASEBACKGROUND_PROPERTY_TO_TRUE) java.lang.System.setProperty("sun.awt.noerasebackground","true");

if (SET_AWT_ERASEBACKGROUNDONRESIZE_PROPERTY_TO_FALSE) java.lang.System.setProperty("sun.awt.erasebackgroundonresize","false");

if (SET_DYNAMIC_LAYOUT_TO_TRUE) Toolkit.getDefaultToolkit().setDynamicLayout(true);

DBFrame dbf =new DBFrame();

dbf.setVisible(true);

}

/** Contructor. set layout and add DBPanel*/

public DBFrame()

{

this.setLayout(null);

this.setBounds(10,10,300,300);

DBPanel dbp =new DBPanel();

dbp.setBounds(10,10,100,100);

this.add(dbp);

}

/** To prevent drawing the background*/

publicvoid update(Graphics g)

{

paint(g);

}

/** Overwritten to paint the component*/

publicvoid paint(Graphics g)

{

// Create a BufferedImage

if (bufferImage==null) bufferImage = (BufferedImage)createImage(1000,1000);

// Get graphics and draw offscreen Image

Graphics bufferGraphics = bufferImage.getGraphics();

this.paintBuffer(bufferGraphics);

bufferGraphics.dispose();

// Draw the offscreen Image on the surface

g.drawImage(bufferImage,0,0,this);

g.dispose();

}

/** Render Component. Draw onto the buffer-image*/

publicvoid paintBuffer(Graphics g)

{

g.setColor(Color.red);

g.fillRect(0,0,1000,1000);

}

/*

* --

*/

/**

* The internal class DBPanel implemenents a double-buffered AWT-Panel

* which can be resized by dragging on the panel

* @author Example

*/

class DBPanelextends Panel

{

private BufferedImage bufferImage =null;

privateboolean panelMoving =false;

private Point mouseDownPosition =null;

private Dimension panelSize =null;

/** Create MouseListeners to enable Movement of the panel*/

public DBPanel()

{

/* Make the control resizable*/

this.addMouseListener(new MouseListener(){

publicvoid mouseEntered(MouseEvent e){}

publicvoid mouseExited(MouseEvent e){}

publicvoid mouseClicked(MouseEvent e){}

publicvoid mouseReleased(MouseEvent e)

{

// Resizing over

panelMoving=false;

}

publicvoid mousePressed(MouseEvent e)

{

// Resizing starts. store the mouseposition

mouseDownPosition=e.getPoint();

panelSize=getSize();

panelMoving=true;

}

});

this.addMouseMotionListener(new MouseMotionListener(){

publicvoid mouseMoved(MouseEvent e){}

publicvoid mouseDragged(MouseEvent e)

{

// Resize the panel, due to the given coordinates

int dX = e.getX() - mouseDownPosition.x;

int dY = e.getY() - mouseDownPosition.y;

setBounds(getX(),getY(),panelSize.width+dX , panelSize.height+dY);

// Redraw the background

getParent().repaint();

}

});

}

/** To prevent drawing the background*/

publicvoid update(Graphics g)

{

paint(g);

}

/** Overwritten to paint the component*/

publicvoid paint(Graphics g)

{

// Create a BufferedImage

if (bufferImage==null) bufferImage = (BufferedImage)createImage(1000,1000);

// Get graphics and draw offscreen Image

Graphics bufferGraphics = bufferImage.getGraphics();

this.paintBuffer(bufferGraphics);

bufferGraphics.dispose();

// Draw the offscreen Image on the surface

g.drawImage(bufferImage,0,0,this);

g.dispose();

}

/** Render Component. Draw onto the buffer-image*/

publicvoid paintBuffer(Graphics g)

{

g.setColor(Color.gray);

g.fillRect(0,0,this.getWidth(),this.getHeight());

}

}

}

Can anyone reproduce this behaviour?

Does anyone know what to do, or does someone know a workaround?

Thanks in advance!

[11081 byte] By [faprasa] at [2007-11-26 18:18:39]
# 1
Hi again,Nobody who has any idea? Anyone who tried it out and could/could not reproduce it?Does this happen under OSX ,too?Really every sort of help is most welcome!!!Thanks!
faprasa at 2007-7-9 5:52:20 > top of Java-index,Desktop,Core GUI APIs...