CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2008
    Posts
    12

    Applying a GridLayout to an already colored space...

    Hi all, I am attempting to create a pool of water graphic (divided into 9 equal parts (ie. 3x3 grid)) for a fishing game and would like to know how I would go about applying a grid to the space so I can refer to any of the 9 spaces.

    Code:
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Pool extends JFrame
    {
        public Pool()
        {
            setTitle("Carpet Fishing Game");
            getContentPane().add(new FishingPanel());
        }
        
        public static void main(String[] args)
        {
            Pool frame = new Pool();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
            
        }
    }
    
    class FishingPanel extends JPanel
    {
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            
             Color water = new Color (0, 100, 200);
             g.setColor (water);
             g.fillRect(100, 100, 300, 300);
        }
    }

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Applying a GridLayout to an already colored space...

    As such which ever LayoutManager you choose, has nothing at all to do with how your components using the layout are painted. Therefore, I dont quite understand the issue with "Applying a GridLayout to an already colored space".

    What is this for? If for layout, you have GridLayout doing all that nicely for you. If for purposes of painting the graphics, basically each component has a cordinate space, x pixels wide and y pixels high. Treating this as a grid, should not be a problem.

    You can eg. create a simple interface, that returns a Rectangle instance of the grid at indexes x,y. Make a JComponent / JPanel extension that implements it, then you can easily call each of the Rectangles and do what you need to do with it.

    If neither of these addressed your problem, can you be more specific as to what you want of it?
    Last edited by Londbrok; September 23rd, 2009 at 04:02 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured