Displaying ovals in grid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aurora88
    New Member
    • Apr 2008
    • 6

    #1

    Displaying ovals in grid

    Hi guys. I have had this problem for so long now it's making me feel so depressed. :x

    I am trying to display 42 ovals (connect 4) on JPanel but it seems to display a huge white box at the top of the panel and the tips os 7 ovals and the rest of the JPanel is blue (the background colour is set to blue).
    My CircleGrid class:

    [code=java]
    public class CircleGrid extends JPanel implements MouseListener, MouseMotionList ener
    {
    int ifRed;
    int notIfRed;
    boolean ifEmpty;
    String currentColour = "White";
    final int empty = 0;
    final int competitorOne = 1;
    final int competitorTwo = 2;
    static final int row = 6;
    static final int col = 7;
    Circle [][] cArray = new Circle[col][row]; //Declare array
    Circle circle = new Circle(0,0);

    public CircleGrid()
    {
    ifRed = 1;
    notIfRed = 0;
    int redCircles = 0;
    int yellowCircles = 0;
    ifEmpty = true;
    addMouseListene r(this);
    setLayout(new GridLayout(6,7) );

    for (int i = 0; i < col; i++)
    {
    for (int h = 0; h < row; h++)
    {
    cArray[i][h] = new Circle(i,h);
    this.add(cArray[i][h]);
    }
    }
    }
    [/code]

    And the Circle class

    [code=java]
    class Circle extends JPanel
    {
    int rowNum, colNum;
    boolean occupied;
    String colour;

    public Circle(int r, int c)
    {
    rowNum = r;
    colNum = c;
    occupied = false;
    }

    public int getRow()
    {
    return rowNum;
    }

    public int getCol()
    {
    return colNum;
    }
    [/code]

    My JPanel (gameCenter) adds the CircleGrid() in the main class.
    When I click on the tips of the ovals it counts it as 22 clicks instead of (and which should be) one.

    Any help would be immensely appreciated.

    Thank you so much in advance.
    Last edited by JosAH; Apr 6 '08, 02:22 PM. Reason: fixed the code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Aurora88
    Hi guys. I have had this problem for so long now it's making me feel so depressed. :x

    I am trying to display 42 ovals (connect 4) on JPanel but it seems to display a huge white box at the top of the panel and the tips os 7 ovals and the rest of the JPanel is blue (the background colour is set to blue).
    No need to feel depressed; it's only code ;-) From what I see from that code is
    no drawing code at all so I have no idea what the problem might be.

    kind regards,

    Jos

    Comment

    • Aurora88
      New Member
      • Apr 2008
      • 6

      #3
      Originally posted by JosAH
      No need to feel depressed; it's only code ;-) From what I see from that code is
      no drawing code at all so I have no idea what the problem might be.

      kind regards,

      Jos
      Oh apologies, I forgot the drawing code. Here it is:

      Code:
      public class CircleGrid extends JPanel implements MouseListener, MouseMotionListener
      	{
      	    int ifRed;
      	    int notIfRed;
      	   	boolean ifEmpty;
      		String currentColour = "White";
      		final int empty = 0;
      		final int competitorOne = 1;
      		final int competitorTwo = 2;
      		static final int row = 6;
      		static final int col = 7;
      		Circle [][] cArray = new Circle[col][row]; //Declare array
      		Circle circle = new Circle(0,0);
      
      	   	public CircleGrid()
      	   	{
      			ifRed = 1;
      			notIfRed = 0;
      			int redCircles = 0;
      			int yellowCircles = 0;
      			ifEmpty = true;
      	     	addMouseListener(this);
      	     	setLayout(new GridLayout(6,7));
      
      	     	for (int i = 0; i < col; i++)
      				{
      					for (int h = 0; h < row; h++)
      						{
      							cArray[i][h] = new Circle(i,h);
      							this.add(cArray[i][h]);
      						}
      				}
      		}
      
      	   	public void alternateColour()
      	   	{
      	     	ifEmpty = !ifEmpty;
      	     	repaint(); //Repaint upon changes
      	   	}
      
      	   	public void paintComponent(Graphics g)
      	   	{
      
      			for (int k = 0; k < col; k++)
      					for (int t = 0; t < row; t++)
      		{
      	   	if (ifEmpty)
      	    {
      	     	g.setColor(Color.WHITE); //Set all of the ovals to white
      	    }
      	    else if (ifRed == clicks)
      	    {
      		 	g.setColor(Color.RED); //Paint red
      		 	currentColour = "Red";
      		 	//System.out.println(getColour());
      		 	//System.out.println(playerOneMove());
      		}
      		else if (notIfRed == clicks)
      		{
      			g.setColor(Color.YELLOW); //Paint yellow
      			currentColour = "Yellow";
      			//System.out.println(getColour());
      			//System.out.println(playerTwoMove());
      		}
      	     	g.fillOval(47*k, 47*t, 45, 45); //Draw and fill oval
      		}
      		}

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Your program logic is flawed: you have a CircleGrid JPanel that stores 42 small
        JPanels that store one oval each. When your CircleGrid JPanel has to draw
        something it attempts to draw the entire grid/board by itself. None of the circles
        in your GridLayout even attempt to draw anything so the JPanels themselves
        have to handle the drawing (they just draw their grey backgrounds).

        Move all your actual drawing logic to where it belongs: each Circle should draw
        itself. The GridLayout will take care that each one draws itself in the correct place
        (that's what LayoutManagers are for). I haven't thoroughly checked your code but
        my guess is that your CircleGrid actually draws the ovals but the GridLayout
        also wants to draw (nothing happens there, except for grey background drawing).

        kind regards,

        Jos

        Comment

        • Aurora88
          New Member
          • Apr 2008
          • 6

          #5
          Oh right I see what you mean. So where should all the drawing go preferably? (I have only been learning for a few months and this is a project for university - a bit difficult I think!).

          Thank you so far.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Aurora88
            Oh right I see what you mean. So where should all the drawing go preferably? (I have only been learning for a few months and this is a project for university - a bit difficult I think!).

            Thank you so far.
            Remove the drawing code you have now and create a bit of drawing code in the
            Circle class; the code should just draw one single oval and it should fill its
            JPanel which it extends. The GridLayout will do the rest.

            kind regards,

            Jos

            Comment

            • Aurora88
              New Member
              • Apr 2008
              • 6

              #7
              So remove the paintComponent code in the CircleGrid class...because all the code is in there at the moment.

              then in the Circle class...

              something like...

              public void paintComponent( Graphics g) {

              g.drawOval(47*k ,47*k, 45, 45) //this still draws the grey rectangle at the top of the JPanel

              }

              Do I use any of the exact same code in the current paintComponent method?
              All the circle begin as white, then change to red or yellow depending on the number of mouse clicks.

              I've changed it so much recently that I'm lost on where the errors occur now. I need to test it 15th April. I'm not looking forward to that. :S
              Thank you so far.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Aurora88
                So remove the paintComponent code in the CircleGrid class...because all the code is in there at the moment.
                yep.

                Originally posted by Aurora88
                then in the Circle class...

                something like...

                public void paintComponent( Graphics g) {

                g.drawOval(47*k ,47*k, 45, 45) //this still draws the grey rectangle at the top of the JPanel

                }
                Nope; a Circle is implemented as a JPanel (look at your own code). A Circle is
                just *one* oval in a JPanel so it fills the entire oval as much as possible:

                [code=java]
                public void paintComponent( Graphics g) {
                super.paintComp onent(g); // if you want the default background
                g.setColor( ... ); // set color of the oval
                g.fillOval(0, 0, this.getWidth() , this.getHeight( )); // draw oval
                }[/code]

                This only draws one oval but you have stored 42 of them in your GridLayout so
                all 42 of them will be drawn at their correct location. As I wrote: that's what
                LayoutManagers are for.

                kind regards,

                Jos

                Comment

                • Aurora88
                  New Member
                  • Apr 2008
                  • 6

                  #9
                  This draws like 4 rectangles across the JPanel all squashed together. :S

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Aurora88
                    This draws like 4 rectangles across the JPanel all squashed together. :S
                    Show a bit of the relevant code.

                    kind regards,

                    Jos

                    Comment

                    • Aurora88
                      New Member
                      • Apr 2008
                      • 6

                      #11
                      Hey dude it was an error on my behalf. I do apologise. All working now.
                      Thank you for your help. :)

                      Take care.

                      - Jamie

                      Comment

                      Working...