Hello,
I have a JWindow containing a layer with a background image and a layer with
a panel:
getContentPane().setLayout(new FlowLayout());
getContentPane().add(credits);
((JPanel)getContentPane()).setOpaque(false);
JLabel background = new JLabel(splash);
getLayeredPane().add(background, new Integer(Integer.MIN_VALUE),
1);
background.setBounds(0,0,WIDTHWINDOW,HEIGHTWINDOW);
This panel is setOpaque(false) (to be able to see the backgroundimage)
In this panel i put a JViewport containing a JTextArea. Every 120
milliseconds I move the viewport to another part of the JTextArea.
Now is my question: does anybody know how to avoid the flickering that comes
from repainting the JViewport every 120 milliseconds?
I tried double buffering (see code below the code for the panel), but that
gave a problem (see below)
(the objective of my code is to get an aboutscreen with a backgroundimage on
which you can see the credits going from bottom to top.)
It must be compatible with JDK 1.1.8
Does anybode know of any example classes or a solution for my problem??
SourceCode
**********
private class CreditsPanel extends JPanel{
JViewport mViewport;
CreditsPanel(){
setOpaque(false);
setLayout(null);
Font fnt = new Font("Arial", Font.BOLD, 9);
//fnt.setColor(Color.white);
FontMetrics fm =
Toolkit.getDefaultToolkit().getFontMetrics(fnt);
final int iHeight = 1; //fm.getHeight();
JTextArea credits = new JTextArea();
credits.setForeground(Color.white);
credits.setFont(fnt);
credits.append("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
credits.append("Developpers:\n");
credits.append("a\n");
credits.append("b\n");
credits.append("c\n");
credits.append("d\n");
credits.append("e\n");
credits.append("\n\n");
credits.append("Q&A:\n");
credits.append("f\n");
credits.append("g\n");
credits.append("h\n");
credits.append("i\n");
credits.append("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
credits.setEditable(false);
credits.setOpaque(false);
mViewport = new JViewport();
mViewport.setOpaque(false);
mViewport.setView(credits);
javax.swing.Timer t = new javax.swing.Timer(120,new
ActionListener(){
public void actionPerformed(ActionEvent e){
moveText(0, iHeight);
}
});
add(mViewport);
mViewport.setBounds(0,0, WIDTHCREDITS, HEIGHTCREDITS);
moveText(0,0);
t.start();
}
public Dimension getPreferredSize () { return new Dimension
(WIDTHCREDITS, HEIGHTCREDITS);}
private void moveText(int x , int y){
Point pt = mViewport.getViewPosition();
pt.x += x;
pt.y += y;
int maxY = getMaxYExtent();
pt.x = Math.max(0, pt.x);
pt.x = Math.min(getMaxXExtent(), pt.x);
pt.y = Math.max(0, pt.y);
pt.y = Math.min(maxY, pt.y);
if (pt.y == maxY) reset();
else mViewport.setViewPosition(pt);
}
private void reset(){
mViewport.setViewPosition (new Point(0,0));
}
protected int getMaxXExtent() {
Rectangle r = mViewport.getView().getBounds();
return (r.width - mViewport.getWidth());
}
protected int getMaxYExtent() {
Rectangle r = mViewport.getView().getBounds();
return (r.height - mViewport.getHeight());
}
}
Double buffering:
*****************
(this code was added to the code of the jpanel above.
The result was that i couldn't see the background image (on the other layer)
but a gray screen was the background.
Is there something i can do about this?
Image offscreen;
/**
* 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) {
if(offscreen == null) {
offscreen = createImage(getSize().width, getSize().height);
}
Graphics og = offscreen.getGraphics();
og.setClip(0,0,getSize().width, getSize().height);
super.paint(og);
g.drawImage(offscreen, 0, 0, null);
og.dispose();
}
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing