Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Grid rendering problem in windows forms

Grid rendering problem in windows forms

Scheduled Pinned Locked Moved C#
csswinformsgraphicshelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    I have the following code to draw a grid based on the size, width and height of a map:

        public void DrawCollisionMap(Map map)
        {
            \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize);
    
            \_panel.Paint -= \_panel\_Paint;
            \_panel.Paint += \_panel\_Paint;
            \_panel.Invalidate(true);
        }
    
        void \_panel\_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);
            for (int x = 0; x < \_map.Width; x++)
            {
                for (int y = 0; y < \_map.Height; y++)
                {
                    e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height);
                }
            }
           
        }
    

    The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:

    T L 4 Replies Last reply
    0
    • V venomation

      I have the following code to draw a grid based on the size, width and height of a map:

          public void DrawCollisionMap(Map map)
          {
              \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize);
      
              \_panel.Paint -= \_panel\_Paint;
              \_panel.Paint += \_panel\_Paint;
              \_panel.Invalidate(true);
          }
      
          void \_panel\_Paint(object sender, PaintEventArgs e)
          {
              e.Graphics.Clear(Color.White);
              for (int x = 0; x < \_map.Width; x++)
              {
                  for (int y = 0; y < \_map.Height; y++)
                  {
                      e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height);
                  }
              }
             
          }
      

      The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      venomation wrote:

      I do not understand :(
      :laugh:

      Man, are you happy or sad now? :confused:

      venomation wrote:

      The problem is that when it is rendered the grid does not render correctly

      What do you mean? what's your expected output?

      thatraja


      **My Tip/Tricks
      My Dad had a Heart Attack on this day so don't...
      **

      1 Reply Last reply
      0
      • V venomation

        I have the following code to draw a grid based on the size, width and height of a map:

            public void DrawCollisionMap(Map map)
            {
                \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize);
        
                \_panel.Paint -= \_panel\_Paint;
                \_panel.Paint += \_panel\_Paint;
                \_panel.Invalidate(true);
            }
        
            void \_panel\_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.Clear(Color.White);
                for (int x = 0; x < \_map.Width; x++)
                {
                    for (int y = 0; y < \_map.Height; y++)
                    {
                        e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height);
                    }
                }
               
            }
        

        The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        venomation wrote:

        I do not understand

        I do. I read your code and saw the mistake right away. However I'm not going to tell you what it is, I just give you some advice: when something does not behave as expected, trim it down to a fraction of what you intend to have, and check that first. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        V 1 Reply Last reply
        0
        • L Luc Pattyn

          venomation wrote:

          I do not understand

          I do. I read your code and saw the mistake right away. However I'm not going to tell you what it is, I just give you some advice: when something does not behave as expected, trim it down to a fraction of what you intend to have, and check that first. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
          Please use <PRE> tags for code snippets, they improve readability.
          CP Vanity has been updated to V2.3

          V Offline
          V Offline
          venomation
          wrote on last edited by
          #4

          Aha how silly of me ! Apart from being silly the other reason why I didn't notice the maps dimensions instead of the tile-size is that I got it working on DirectX so "thought" I knew what I was doing ^^. Thanks for making me see the light :laugh:

          1 Reply Last reply
          0
          • V venomation

            I have the following code to draw a grid based on the size, width and height of a map:

                public void DrawCollisionMap(Map map)
                {
                    \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize);
            
                    \_panel.Paint -= \_panel\_Paint;
                    \_panel.Paint += \_panel\_Paint;
                    \_panel.Invalidate(true);
                }
            
                void \_panel\_Paint(object sender, PaintEventArgs e)
                {
                    e.Graphics.Clear(Color.White);
                    for (int x = 0; x < \_map.Width; x++)
                    {
                        for (int y = 0; y < \_map.Height; y++)
                        {
                            e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height);
                        }
                    }
                   
                }
            

            The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            We're not done yet. I have two comments on your code: 1. the remove-then-add-a-handler stuff looks unnecessary. Why are you doing this? 2. once you fixed the error, you are drawing a large number of tiny adjacent rectangles, which means a lot of computational effort, and also each tiny edge gets drawn twice. And all you really need for a grid of N by N cells, is N+1 horizontal lines and N+1 vertical lines. That does not take more code, it does not have nested for loops, and even may be less error prone! :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
            Please use <PRE> tags for code snippets, they improve readability.
            CP Vanity has been updated to V2.3

            V 1 Reply Last reply
            0
            • L Luc Pattyn

              We're not done yet. I have two comments on your code: 1. the remove-then-add-a-handler stuff looks unnecessary. Why are you doing this? 2. once you fixed the error, you are drawing a large number of tiny adjacent rectangles, which means a lot of computational effort, and also each tiny edge gets drawn twice. And all you really need for a grid of N by N cells, is N+1 horizontal lines and N+1 vertical lines. That does not take more code, it does not have nested for loops, and even may be less error prone! :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
              Please use <PRE> tags for code snippets, they improve readability.
              CP Vanity has been updated to V2.3

              V Offline
              V Offline
              venomation
              wrote on last edited by
              #6

              The event handling is terrible I know, only there for a quick hack as I wanted to fix the old issue... The cells need to be "cell by cell" instead of lines as the user will change them (tile map style). I don't know if your suggestion would help for that, I made a slight improvement in the loop by calculating the difference before:

                        for (int x = dbPanel1.HorizontalScroll.Value%size; x < width\*size; x+= size)
                      {
                          for (int y = dbPanel1.VerticalScroll.Value % size; y < height \* size; y += size)
                          {
                              e.Graphics.DrawRectangle(\_p, x , y , size, size);
                          }
                      }
              
              L 1 Reply Last reply
              0
              • V venomation

                The event handling is terrible I know, only there for a quick hack as I wanted to fix the old issue... The cells need to be "cell by cell" instead of lines as the user will change them (tile map style). I don't know if your suggestion would help for that, I made a slight improvement in the loop by calculating the difference before:

                          for (int x = dbPanel1.HorizontalScroll.Value%size; x < width\*size; x+= size)
                        {
                            for (int y = dbPanel1.VerticalScroll.Value % size; y < height \* size; y += size)
                            {
                                e.Graphics.DrawRectangle(\_p, x , y , size, size);
                            }
                        }
                
                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Yes, the readability has increased a lot. :thumbsup:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                Please use <PRE> tags for code snippets, they improve readability.
                CP Vanity has been updated to V2.3

                1 Reply Last reply
                0
                • V venomation

                  I have the following code to draw a grid based on the size, width and height of a map:

                      public void DrawCollisionMap(Map map)
                      {
                          \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize);
                  
                          \_panel.Paint -= \_panel\_Paint;
                          \_panel.Paint += \_panel\_Paint;
                          \_panel.Invalidate(true);
                      }
                  
                      void \_panel\_Paint(object sender, PaintEventArgs e)
                      {
                          e.Graphics.Clear(Color.White);
                          for (int x = 0; x < \_map.Width; x++)
                          {
                              for (int y = 0; y < \_map.Height; y++)
                              {
                                  e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height);
                              }
                          }
                         
                      }
                  

                  The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Some more comments, out of the blue: 1. if you create instances of a class that has a Dispose() method(such as Graphics, Font, Pen, Brush), you should call it when done with the objects. 2. you should avoid creating objects inside an event handler that is going to be called often; say you need a few brushes, just create them once and keep them around. 3. There are a whole lot of stock objects available at no extra charge, see classes such as Pens and Brushes. (and no, you don't dispose them, they aren't yours, you just borrow them). :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                  Please use <PRE> tags for code snippets, they improve readability.
                  CP Vanity has been updated to V2.3

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups