Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
Like Tree1Likes

Thread: Can you tell why the graphics flickers even with BufferedImage implemented in my code

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    If its an applet, the main method will not be called unless you call it. The init() method will be called by the browser.
    If its an app there is nothing special about a method called init(). Your code would have to explicitly call it.

  2. #22
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Here I managed to make it work with init():

    Java Code:
    public class ClickToDrawPeiceInsideCell extends JApplet
    {
        private Image vBoardImg, vSmily;
        
        @Override
        public void init()
        {
            Container MyJApplet = getContentPane();
            
            setSize(600, 600);
            
            File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
            File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
            
            try
            {
                vBoardImg = ImageIO.read(vChessBoardImgFile);
                vSmily    = ImageIO.read(vBlackSmilyImgFile);
            }
            catch (IOException ex)
            {
                Logger.getLogger(ClickToDrawPeiceInsideCell.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
            
            MyJApplet.add(Grid);
        }
    }
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Is that another project done?

  4. #24
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Ops, the same problem stated remains:

    The problem is nothing showing up when the JApplet is run, when you minimize the window and get it back it stills the same nothing shows up, only when you maximize the window the images shows up but when they do they show up multiple times one on top of the other from upper left to down right.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    I managed to make it work
    Confusing.

    You will have to post your current code.

  6. #26
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Sorry for the confusion, meant I got the code to be executed with init() method.

    Here is it:

    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ClickToDrawPeiceInsideCell extends JApplet
    {
        private Image vBoardImg, vSmily;
        
        @Override
        public void init()
        {
            Container MyJApplet = getContentPane();
            
            setSize(600, 600);
            
            File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
            File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
            
            try
            {
                vBoardImg = ImageIO.read(vChessBoardImgFile);
                vSmily    = ImageIO.read(vBlackSmilyImgFile);
            }
            catch (IOException ex)
            {
                Logger.getLogger(ClickToDrawPeiceInsideCell.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
            
            MyJApplet.add(Grid);
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class DrawGrid extends JPanel
    {
        private final int vGridSize = 58;
        private final int vGridRows = 8;
        private final int vGridColumns = 8;
        
        private int vBoardWidth  = vGridColumns * vGridSize;
        private int vBoardHeight = vGridRows    * vGridSize;
        
        private int vWinWidth  = vBoardWidth+(vGridSize*2);
        private int vWinHeight = vBoardHeight+(vGridSize*2);
        
        private boolean vImageBeenLoaded = false;
        
        private BufferedImage vBuffImg = null;
        
        private Image vBoardImg, vSmily;
        
        public DrawGrid(Image vBoardImg, Image vSmily)
        {
            this.vBoardImg = vBoardImg;
            this.vSmily    = vSmily;
            
            setPreferredSize(new Dimension(vWinWidth, vWinHeight));
            setBackground(Color.white);
            
            repaint();
        }
        
        @Override
        public void paintComponent(Graphics vG)
        {
            Graphics2D vGraph2D = (Graphics2D)vG;
            
            super.paintComponent(vGraph2D);
            
            if(vBuffImg == null)
            {
                vBuffImg = new BufferedImage(vBoardWidth, vBoardHeight,
                                                  BufferedImage.TYPE_INT_RGB);
                
                int vG_Width  = getSize().width;
                int vG_Height = getSize().height;
                
                vBuffImg = (BufferedImage)this.createImage(vG_Width, vG_Height);
                
                vGraph2D = vBuffImg.createGraphics();
            }
            
            if(!vImageBeenLoaded)
            {
                if(vGraph2D.drawImage(vBoardImg, 0, 0, this))
                {
                    vGraph2D.drawImage(vSmily, 0, 0, this);
                                    
                    vImageBeenLoaded = true;
                }
            }
            
            vGraph2D.drawImage(vBuffImg, vGridSize, vGridSize, this);
        }
    }
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    You do not have any debugging printlns in your code. How do you know if any of the methods are being executed?

  8. #28
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    I checked if the images are not null before posting and the images show up eventually but kinda messed up.

    I followed your advice and checked every method with debugging printlns and it seems that every method is working.

    In case if someone doesn't believe that I tried the printlns:

    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ClickToDrawPeiceInsideCell extends JApplet
    {
        private Image vBoardImg, vSmily;
        
        @Override
        public void init()
        {
    //        System.out.println("init");
            Container MyJApplet = getContentPane();
            
            setSize(600, 600);
            
            File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
            File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
            
            try
            {
                vBoardImg = ImageIO.read(vChessBoardImgFile);
                vSmily    = ImageIO.read(vBlackSmilyImgFile);
            }
            catch (IOException ex)
            {
                Logger.getLogger(ClickToDrawPeiceInsideCell.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
            
            MyJApplet.add(Grid);
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class DrawGrid extends JPanel
    {
        private final int vGridSize = 58;
        private final int vGridRows = 8;
        private final int vGridColumns = 8;
        
        private int vBoardWidth  = vGridColumns * vGridSize;
        private int vBoardHeight = vGridRows    * vGridSize;
        
        private int vWinWidth  = vBoardWidth+(vGridSize*2);
        private int vWinHeight = vBoardHeight+(vGridSize*2);
        
        private boolean vImageBeenLoaded = false;
        
        private BufferedImage vBuffImg = null;
        
        private Image vBoardImg, vSmily;
        
        public DrawGrid(Image vBoardImg, Image vSmily)
        {
    //        System.out.println("DrawGrid");
            this.vBoardImg = vBoardImg;
            this.vSmily    = vSmily;
            
            setPreferredSize(new Dimension(vWinWidth, vWinHeight));
            setBackground(Color.white);
            
            repaint();
        }
        
        @Override
        public void paintComponent(Graphics vG)
        {
    //        System.out.println("paintComponent");
            Graphics2D vGraph2D = (Graphics2D)vG;
            
            super.paintComponent(vGraph2D);
            
            if(vBuffImg == null)
            {
    //            System.out.println("paintComponent");
                vBuffImg = new BufferedImage(vBoardWidth, vBoardHeight,
                                                  BufferedImage.TYPE_INT_RGB);
                
                int vG_Width  = getSize().width;
                int vG_Height = getSize().height;
                
                vBuffImg = (BufferedImage)this.createImage(vG_Width, vG_Height);
                
                vGraph2D = vBuffImg.createGraphics();
            }
            
            if(!vImageBeenLoaded)
            {
                if(vGraph2D.drawImage(vBoardImg, 0, 0, this))
                {
    //                System.out.println("paintComponent");
                    vGraph2D.drawImage(vSmily, 0, 0, this);
                                    
                    vImageBeenLoaded = true;
                }
            }
            
            vGraph2D.drawImage(vBuffImg, vGridSize, vGridSize, this);
        }
    }
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    You need to go through your code carefully and look at it closely, line by line and variable by variable.

    You need more printlns to show the values of variables as they change values and are used.

  10. #30
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Any suggestion that might guide me to get good results?
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Read post # 29.
    That is exactly what I did to find the problem. You should learn how to do it. I will have done it faster than you can, but I have many years of experience. You learn by doing.

  12. #32
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    I got it finally.

    After giving it some thoughts, I said since the buffered image shows up multiple times, maybe it's reinitialized again and again so I decided to initialize the buffered image out of the paintComponent() method and with some trial and errors I got it to work it took me a while though.

    Here is how I had done it:

    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ClickToDrawPeiceInsideCell extends JApplet
    {
        private Image vBoardImg, vSmily;
        
        @Override
        public void init()
        {
            Container MyJApplet = getContentPane();
            
            setSize(600, 600);
            
            File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
            File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
            
            try
            {
                vBoardImg = ImageIO.read(vChessBoardImgFile);
                vSmily    = ImageIO.read(vBlackSmilyImgFile);
            }
            catch (IOException ex)
            {
                Logger.getLogger(ClickToDrawPeiceInsideCell.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
            
            MyJApplet.add(Grid);
        }
    }
    Java Code:
    public class DrawGrid extends JPanel
    {
        private final int vGridSize = 58;
        private final int vGridRows = 8;
        private final int vGridColumns = 8;
        
        private int vBoardWidth  = vGridColumns * vGridSize;
        private int vBoardHeight = vGridRows    * vGridSize;
        
        private int vWinWidth  = vBoardWidth+(vGridSize*2);
        private int vWinHeight = vBoardHeight+(vGridSize*2);
        
        private boolean vImageBeenLoaded = false;
        
        private BufferedImage vBuffImg = new BufferedImage
                                             (vBoardWidth, vBoardHeight,
                                              BufferedImage.TYPE_INT_RGB);
        
        Graphics2D Graph = vBuffImg.createGraphics();
        
        private Image vBoardImg, vSmily;
        
        public DrawGrid(Image vBoardImg, Image vSmily)
        {
            this.vBoardImg = vBoardImg;
            this.vSmily    = vSmily;
            
            setPreferredSize(new Dimension(vWinWidth, vWinHeight));
            setBackground(Color.white);
            
            repaint();
        }
        
        @Override
        public void paintComponent(Graphics vG)
        {
            Graphics2D vGraph2D = (Graphics2D)vG;
            
            super.paintComponent(vGraph2D);
            
            if(!vImageBeenLoaded)
            {
                if(Graph.drawImage(vBoardImg, 0, 0, this))
                {
                    Graph.drawImage(vSmily, 0, 0, this);
                    vImageBeenLoaded = true;
                }
            }
            
            if(vBuffImg == null)
            {
                System.out.println("vBuffImg is null");
            }
    
            vGraph2D.drawImage(vBuffImg, vGridSize, vGridSize, this);
        }
    }
    I have a couple questions:

    - Is this a good approach or there is a better one?

    - Since I couldn't solve it with debugging printlns even that I tried, that's because I didn't know how the variables should normally be at each point, Would you Please teach me how to do it so I can use it next time.

    Thank You So Very Much For Your Help Sir. Norm.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Debugging is an iterative process. You start out with a lot of printlns, most of which are not useful. You see an area of the program where the output doesn't make sense or raises more questions and add some more printlns to look at what is going on in more detail. Hard to explain.

  14. #34
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Can you tell why the graphics flickers even with BufferedImage implemented in my

    Quote Originally Posted by Shikatsu View Post
    I got it finally.

    After giving it some thoughts, I said since the buffered image shows up multiple times, maybe it's reinitialized again and again so I decided to initialize the buffered image out of the paintComponent() method
    :
    :
    - Is this a good approach or there is a better one?
    It's a good approach. In general, override painting methods for painting tasks only. Do not perform any disk or network I/O in a painting method, and do not change the state of the component being painted or of any container in its hierarchy. Better still don't change the state of anything in a painting method.

    Remember, you have absolutely no control over how many time or how often a painting method is invoked. Even successive repaint() calls from your code may be coalesced into a single paint event.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Can an Interface have implemented method???
    By pppriyadarshi in forum New To Java
    Replies: 1
    Last Post: 09-10-2011, 07:44 PM
  2. How should an Animation class be implemented?
    By kiregad in forum New To Java
    Replies: 9
    Last Post: 06-18-2010, 08:39 AM
  3. My animation flickers.
    By Somelauw in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2010, 10:20 PM
  4. Replies: 1
    Last Post: 11-26-2009, 06:05 PM
  5. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:29 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
  •