Page 1 of 2 12 LastLast
Results 1 to 20 of 34
Like Tree1Likes

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

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

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

    Greetings EveryOne

    Thanks to Norms help I was able to make the first step to make my board game in java.

    This is my code

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    
    public class ClickToDrawPeiceInsideCell extends Applet 
                                                     implements MouseListener
    {
        private int MouseX;
        private int MouseY;
        
        private boolean ImageBeenLoaded = false;
        
        Image ImgBuffer;
        
        Image Smily, BoardImg;
        
        Graphics2D Graph;
        
        DrawGrid Grid = new DrawGrid();
        
        BufferedImage BuffImg = new BufferedImage(Grid.GetBoardWidth(), 
                                                  Grid.GetBoardHeight(),
                                                  BufferedImage.TYPE_INT_RGB);
        
        Graphics2D GridGraph = BuffImg.createGraphics();
        
        @Override
        public void init()
        {
            setSize(Grid.GetWinWidth(), Grid.GetWinHeight());
            
            Smily = getImage(getDocumentBase(), "Smily.jpg");
            BoardImg  = getImage(getDocumentBase(), "ChessBoard.jpg");
    
            addMouseListener(this);
        }
        
        @Override
        public void paint(Graphics G)
        {
            int GridSize = Grid.GetGridSize();
            
            if(!ImageBeenLoaded)
            {
                if(GridGraph.drawImage(BoardImg, 0, 0, this))
                {
                    GridGraph.drawImage(Smily, -50, -50, this);
                    ImageBeenLoaded = true;
                }
            }
    
            G.drawImage(BuffImg, GridSize, GridSize, this);
        }
    
        @Override
        public void mouseClicked(MouseEvent ME)
        {
            MouseX = ME.getX();
            MouseY = ME.getY();
            
            Grid.ClickToDraw(GridGraph, Smily, BoardImg, MouseY, MouseX);
            repaint();
        }
    
        @Override
        public void mousePressed(MouseEvent ME) {}
    
        @Override
        public void mouseReleased(MouseEvent ME) {}
    
        @Override
        public void mouseEntered(MouseEvent ME) {}
    
        @Override
        public void mouseExited(MouseEvent ME) {}
    }
    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawGrid extends Applet
    {
        private final int GridSize = 58;
        private final int GridRows = 8;
        private final int GridColumns = 8;
        
        private int BoardWidth  = GridColumns * GridSize;
        private int BoardHeight = GridRows    * GridSize;
        
        private int WinWidth  = BoardWidth+(GridSize*2);
        private int WinHeight = BoardHeight+(GridSize*2);
      
        private int Coord_Data[] = new int[9];
        private int PeiceCoordX[][] = new int[9][9];
        private int PeiceCoordY[][] = new int[9][9];
        
        Image ImgBuffer;
        
        Image Smily;
        
        Graphics2D Graph;
        
        public DrawGrid()
        {
            SetUpGameCoord();
        }
        
        private void SetUpGameCoord()
        {
            int PeiceOffsetCoord = 4;
            Coord_Data[1] = PeiceOffsetCoord;
            
            for(int i=2; i<=8; i++)
            {
                Coord_Data[i] = Coord_Data[i-1]+GridSize;
            }
            
            for(int i=1; i<=8; i++)
            {
                for(int j=1; j<=8; j++)
                {
                    PeiceCoordX[j][i] = Coord_Data[i];
                    PeiceCoordY[j][i] = Coord_Data[j];
                }
            }
        }
        
        public int GetWinWidth()
        {
            return WinWidth;
        }
        
        public int GetWinHeight()
        {
            return WinHeight;
        }
        
        public int GetGridSize()
        {
            return GridSize;
        }
        
        public int GetBoardWidth()
        {
            return BoardWidth;
        }
        
        public int GetBoardHeight()
        {
            return BoardHeight;
        }
        
        public int GetPeiceX(int y, int x)
        {
            return PeiceCoordX[y][x];
        }
        
        public int GetPeiceY(int y, int x)
        {
            return PeiceCoordY[y][x];
        }
        
        public void ClickToDraw(Graphics2D Graph, Image PeiceImg, 
                                Image BoardImg, int y, int x)
        {
            y = (int)(y/GridSize);
            x = (int)(x/GridSize);
            
            if(y<=8 && x<=8 &&
               y>=1 && x>=1   )
            {
                Graph.drawImage(BoardImg, 0, 0, this);
                Graph.drawImage(PeiceImg, GetPeiceX(y, x), GetPeiceY(y, x), this);
            }
        }
    }
    The Game Images: GameIMG.zip

    The problem is the graphics flickers when ever you click to move the Smily face, even with BufferedImage, I might guess BufferedImage is not well implemented.

    Can You Point the problem Please. Thank You In Andvance
    Last edited by Shikatsu; 11-27-2011 at 04:05 PM.
    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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

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

    You have been asked to follow coding conventions. Why should members here struggle with your variable and method names that start with an uppercase letter? That's not what we're used to seeing.

    Why does your DrawGrid class extend Applet? It certainly isn't being used as an Applet. And why does it have an instance field Graph which is never used (nor needed)?

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

  3. #3
    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

    Why does your DrawGrid class extend Applet? It certainly isn't being used as an Applet. And why does it have an instance field Graph which is never used (nor needed)?
    They was there before and I forgot to omit them. I can't edit the first post so I can't take them off.

    You have been asked to follow coding conventions. Why should members here struggle with your variable and method names that start with an uppercase letter? That's not what we're used to seeing.
    Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.

    If you like to help I will appreciate it, but if you don't like to I thank you either ways.

    Thank You For Your Understanding.
    Last edited by Shikatsu; 11-27-2011 at 05:37 PM.
    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

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

    Not following the naming conventions makes your code hard to read.
    How can a new reader of your code tell if a symbol/word is a class name or a variable name?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

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

    Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.
    Good to know that you know more about coding standards than all the text books, tutorials and other professional programmer around the world?

    Since you know more than we do I guess there is no need to help since you will be able to solve the problem on your own. Good luck.
    Last edited by camickr; 11-27-2011 at 05:56 PM.

  6. #6
    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

    About extending the Applet class, it's needed to implement the drawImage() method.
    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. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

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

    About extending the Applet, it's needed to implement the drawImage() method.
    No it isn't. That is not how you do custom painting.

  8. #8
    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

    Good to know that you know more about coding standards than all the text books, tutorials and other professional programmer around the world?

    Since you know more than we do I guess there is no need to help since you will be able to solve the problem on your own. Good luck.
    Please don't be mean, I am here to learn wither you like it or not, read my signature before accusing me with such a thing.
    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. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

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

    I am not asking you to code it for me, what i need is guidance so i can learn
    You where given guidance which you promply ignored.

    I would suggest you start with the Swing tutorial on: http://docs.oracle.com/javase/tutori...ts/applet.html

    The table of contents also has a section on how to do Custom Painting.
    Last edited by camickr; 11-27-2011 at 05:59 PM.

  10. #10
    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

    You where given guidance which you promply ignored.
    Think of it as you like but I am not, if you don't want or like to help me please just ignore me and ignore my post, Thank You.

    I would suggest you start with the Swing tutorial on: http://docs.oracle.com/javase/tutori...ts/applet.html

    The table of contents also has a section on how to do Custom Painting.
    Thank You, I will do.
    Last edited by Shikatsu; 11-27-2011 at 06:05 PM.
    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. #11
    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

    Not following the naming conventions makes your code hard to read.
    How can a new reader of your code tell if a symbol/word is a class name or a variable name?
    Sir Norm, you are right on that, maybe the IDE can help.
    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

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

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

    Try converting the code to use Swing classes and methods. The flicker goes away when you do that.
    Shikatsu likes this.

  13. #13
    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

    Try converting the code to use Swing classes and methods. The flicker goes away when you do that.
    Thank you Sir Norm, I am gonna work on that and report back, You are a real mentor Bless You.
    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

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by Shikatsu View Post
    They was there before and I forgot to omit them. I can't edit the first post so I can't take them off.
    Yes should be able to edit the first post.


    Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.
    It has nothing to do with not "liking" and all to do with not understanding what you've written. If you want our help, we would appreciate it if you took the extra effort to make your code understandable by others.

  15. #15
    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

    Quote Originally Posted by Fubarable
    Yes should be able to edit the first post.
    I can't see the edit link next to reply & reply with quote!
    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

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by Shikatsu View Post
    I can't see the edit link next to reply & reply with quote!
    Hm, perhaps this isn't available until you've been here longer. Sorry, and I retract that part of my comment.

  17. #17
    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

    After following Mr. Norm's advice, I had managed to convert my code to use Swing classes and methods.

    Here is my code:

    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ClickToDrawPeiceInsideCell extends JApplet
    {
        private Image vBoardImg, vSmily;
        
        public static void main(String args[]) throws IOException
        {
            JApplet MyJApplet = new ClickToDrawPeiceInsideCell();
            
            JFrame MyJFrame = new JFrame();
            
            MyJFrame.setContentPane(MyJApplet);
            MyJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyJFrame.setTitle("ClickToDrawPeiceInsideCell");
            MyJFrame.pack();
            //MyJFrame.setResizable(false);
            
            MyJFrame.setVisible(true);
        }
        
        public ClickToDrawPeiceInsideCell() throws IOException
        {
            File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
            File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
            
            vBoardImg = ImageIO.read(vChessBoardImgFile);
            vSmily    = ImageIO.read(vBlackSmilyImgFile);
            
            DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
            
            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);
        }
    }
    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.

    Can You Help Me With This, Thank You In Advance
    Last edited by Shikatsu; 11-30-2011 at 12:18 AM.
    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

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

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

    There is a contradiction in the ClickToDrawPeiceInsideCell class.
    It extends JApplet which implies that it is an applet that runs from an html page in a browser.
    And it has a main() method which is used as a starting point for the java command.
    Is it an applet or an application?
    How is it executed?

  19. #19
    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

    It's a JApplet but it's run as an application maybe it's a contradiction as you said, I am not sure about that.

    I took this approach working on this Example:

    Java: Example - PaintDemo

    Thank You For You Quick Reply Mr. 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

  20. #20
    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 tried to get it work using init() method, but I couldn't find any helpful example.
    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

Page 1 of 2 12 LastLast

Similar Threads

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