Results 1 to 7 of 7

Thread: Flashing

  1. #1
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default [SOLVED] Flashing

    Hello, I did a little test and I have a problem.

    Here's my code:
    Java 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;
    }
    Java 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;
    }
    Java 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. :)
    Last edited by Supamagier; 04-29-2009 at 12:29 PM. Reason: Solved.
    I die a little on the inside...
    Every time I get shot.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You're calling paint directly instead of calling repaint()
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    panel.repaint();
    t3.repaint();

    Only in the update methods - as it should (I heard). It doesn't help anyway, I tried. :(
    I die a little on the inside...
    Every time I get shot.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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?
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Nvm,

    I solved it ;)

    I had to call panel.repaint() directly from the mainclass's paint method.

    Thx for your help anyway ;)
    I die a little on the inside...
    Every time I get shot.

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Remove Flashing in Applet
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 05-30-2009, 07:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •