-
[SOLVED] Flashing
Hello, I did a little test and I have a problem.
Here's my code:
Code:
import java.awt.*;
import javax.swing.*;
public class Test1
extends JFrame
implements Runnable
{
private Test1() {
super("Test");
setSize(1000,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setBackground(Color.white);
setResizable(false);
setVisible(true);
add((panel = new Test2(this)));
panel.setBounds(0,0,1000,500);
thread = new Thread(this);
thread.start();
panel.requestFocus();
}
public void run() {
while (true) {
if (panel != null)
panel.repaint();
try {
thread.sleep(cycletime);
}
catch (InterruptedException ie) {
}
}
}
public static void main(String[] args) {
new Test1();
}
final Thread thread;
final Test2 panel;
final int cycletime = 20;
}
Code:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class Test2
extends JPanel
{
Test2(Test1 t) {
setLayout(null);
t3 = new Test3();
t3.setBounds(0,0,800,500);
add(t3);
menuBar = new MenuBar();
menu = new Menu[4];
menuItems = new MenuItem[11];
menu[0] = new Menu("A");
menu[0].add(menuItems[0] = new MenuItem("1"));
menu[0].add(menuItems[1] = new MenuItem("2"));
menu[0].add(menuItems[2] = new MenuItem("3"));
menu[1] = new Menu("B");
menu[1].add(menuItems[3] = new MenuItem("4"));
menu[2] = new Menu("C");
menu[2].add(menuItems[4] = new MenuItem("5"));
menu[2].add(menuItems[5] = new MenuItem("6"));
menu[2].add(menuItems[6] = new MenuItem("7"));
menu[2].add(menuItems[7] = new MenuItem("8"));
menu[2].add(menuItems[8] = new MenuItem("9"));
menu[2].add(menuItems[9] = new MenuItem("10"));
menu[3] = new Menu("D");
menu[3].add(menuItems[10] = new MenuItem("11"));
t.setMenuBar(menuBar);
for (Menu m : menu)
menuBar.add(m);
}
public void paint(final Graphics g) {
if (b == null) {
off = new BufferedImage(200,500,BufferedImage.TYPE_INT_ARGB);
b = off.getGraphics();
}
t3.repaint();
b.setColor(Color.red);
b.fillRect(0,0,200,500);
g.drawImage(off,800,0,this);
}
public void update(final Graphics g) {
paint(g);
}
BufferedImage off;
Graphics b;
private final MenuBar menuBar;
private final Menu[] menu;
private final MenuItem[] menuItems;
Test3 t3;
}
Code:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class Test3
extends JPanel
{
public void paint(final Graphics g) {
if (b == null) {
off = new BufferedImage(800,500,BufferedImage.TYPE_INT_ARGB);
b = off.getGraphics();
}
b.setColor(Color.green);
b.fillRect(0,0,800,500);
g.drawImage(off,0,0,this);
}
public void update(final Graphics g) {
paint(g);
}
BufferedImage off;
Graphics b;
}
My problem: The green area (drawn by class Test3) keeps flashing white as I move my mouse over the menus (fast). Does anybody know what I'm doing wrong?
Help please. :)
-
You're calling paint directly instead of calling repaint()
-
panel.repaint();
t3.repaint();
Only in the update methods - as it should (I heard). It doesn't help anyway, I tried. :(
-
You don't appear to need those update methods at all. You also don't need to call t3.repaint() from within Test2.paint(). I also don't see a validate() anywhere.
-
Hmm, I checked, and indeed, the update methods are never called - while they, I'm sure, are needed for proper double buffering. Must be doing something wrong.
What do you mean by validate?
-
Nvm,
I solved it ;)
I had to call panel.repaint() directly from the mainclass's paint method.
Thx for your help anyway ;)
-
update() isn't going to get called unless you call it yourself.
You need a boolean flag to check whether the BufferedImage is up-to-date. Then the paint() method checks this and either paints the image on the Graphics, or paints the new stuff on both.
I was referring to Container.validate(), which is required after adding/removing components.