473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. public class CircleGrid extends JPanel implements MouseListener, MouseMotionListener
  2.     {
  3.         int ifRed;
  4.         int notIfRed;
  5.            boolean ifEmpty;
  6.         String currentColour = "White";
  7.         final int empty = 0;
  8.         final int competitorOne = 1;
  9.         final int competitorTwo = 2;
  10.         static final int row = 6;
  11.         static final int col = 7;
  12.         Circle [][] cArray = new Circle[col][row]; //Declare array
  13.         Circle circle = new Circle(0,0);
  14.  
  15.            public CircleGrid()
  16.            {
  17.             ifRed = 1;
  18.             notIfRed = 0;
  19.             int redCircles = 0;
  20.             int yellowCircles = 0;
  21.             ifEmpty = true;
  22.              addMouseListener(this);
  23.              setLayout(new GridLayout(6,7));
  24.  
  25.              for (int i = 0; i < col; i++)
  26.                 {
  27.                     for (int h = 0; h < row; h++)
  28.                         {
  29.                             cArray[i][h] = new Circle(i,h);
  30.                             this.add(cArray[i][h]);
  31.                         }
  32.                 }
  33.            }
  34.  
And the Circle class

Expand|Select|Wrap|Line Numbers
  1. class Circle extends JPanel
  2.     {
  3.         int rowNum, colNum;
  4.         boolean occupied;
  5.         String colour;
  6.  
  7.     public Circle(int r, int c)
  8.     {
  9.         rowNum = r;
  10.         colNum = c;
  11.         occupied = false;
  12.     }
  13.  
  14.     public int getRow()
  15.     {
  16.         return rowNum;
  17.     }
  18.  
  19.     public int getCol()
  20.     {
  21.         return colNum;
  22.     }
  23.  
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.
Apr 6 '08 #1
10 3207
JosAH
11,448 Expert 8TB
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
Apr 6 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1. public class CircleGrid extends JPanel implements MouseListener, MouseMotionListener
  2.     {
  3.         int ifRed;
  4.         int notIfRed;
  5.            boolean ifEmpty;
  6.         String currentColour = "White";
  7.         final int empty = 0;
  8.         final int competitorOne = 1;
  9.         final int competitorTwo = 2;
  10.         static final int row = 6;
  11.         static final int col = 7;
  12.         Circle [][] cArray = new Circle[col][row]; //Declare array
  13.         Circle circle = new Circle(0,0);
  14.  
  15.            public CircleGrid()
  16.            {
  17.             ifRed = 1;
  18.             notIfRed = 0;
  19.             int redCircles = 0;
  20.             int yellowCircles = 0;
  21.             ifEmpty = true;
  22.              addMouseListener(this);
  23.              setLayout(new GridLayout(6,7));
  24.  
  25.              for (int i = 0; i < col; i++)
  26.                 {
  27.                     for (int h = 0; h < row; h++)
  28.                         {
  29.                             cArray[i][h] = new Circle(i,h);
  30.                             this.add(cArray[i][h]);
  31.                         }
  32.                 }
  33.         }
  34.  
  35.            public void alternateColour()
  36.            {
  37.              ifEmpty = !ifEmpty;
  38.              repaint(); //Repaint upon changes
  39.            }
  40.  
  41.            public void paintComponent(Graphics g)
  42.            {
  43.  
  44.             for (int k = 0; k < col; k++)
  45.                     for (int t = 0; t < row; t++)
  46.         {
  47.            if (ifEmpty)
  48.         {
  49.              g.setColor(Color.WHITE); //Set all of the ovals to white
  50.         }
  51.         else if (ifRed == clicks)
  52.         {
  53.              g.setColor(Color.RED); //Paint red
  54.              currentColour = "Red";
  55.              //System.out.println(getColour());
  56.              //System.out.println(playerOneMove());
  57.         }
  58.         else if (notIfRed == clicks)
  59.         {
  60.             g.setColor(Color.YELLOW); //Paint yellow
  61.             currentColour = "Yellow";
  62.             //System.out.println(getColour());
  63.             //System.out.println(playerTwoMove());
  64.         }
  65.              g.fillOval(47*k, 47*t, 45, 45); //Draw and fill oval
  66.         }
  67.         }
  68.  
Apr 6 '08 #3
JosAH
11,448 Expert 8TB
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
Apr 6 '08 #4
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.
Apr 6 '08 #5
JosAH
11,448 Expert 8TB
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
Apr 6 '08 #6
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.
Apr 6 '08 #7
JosAH
11,448 Expert 8TB
So remove the paintComponent code in the CircleGrid class...because all the code is in there at the moment.
yep.

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:

Expand|Select|Wrap|Line Numbers
  1. public void paintComponent(Graphics g) {
  2.    super.paintComponent(g); // if you want the default background
  3.    g.setColor( ... ); // set color of the oval
  4.    g.fillOval(0, 0, this.getWidth(), this.getHeight()); // draw oval
  5. }
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
Apr 7 '08 #8
This draws like 4 rectangles across the JPanel all squashed together. :S
Apr 7 '08 #9
JosAH
11,448 Expert 8TB
This draws like 4 rectangles across the JPanel all squashed together. :S
Show a bit of the relevant code.

kind regards,

Jos
Apr 7 '08 #10
Hey dude it was an error on my behalf. I do apologise. All working now.
Thank you for your help. :)

Take care.

- Jamie
Apr 7 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: pmud | last post by:
Hi, I have 4 columns in my sql database table. I added a data adapter , data set & a data grid to view this information. But the data grid is displaying those 4 columns TWICE , i.e in the data...
2
by: VM | last post by:
When I display data to a Windows datagrid I usually fill the underlying table (in another class) and then, once it contains all the data, I attach it to the grid. But there are some processes that...
2
by: scott | last post by:
Hi all, Iv got a slight problem with a datagrid and the ability to only display certain things in it. I have a Data grid which is connected to a data table. The data table is connected...
4
by: Jon | last post by:
Hi, suspect there might not be a good answer to this one. Using vb.net I have a page like this some text datagrid repeater Now the repeater is populated from a simple query that takes no time...
5
by: Lisa Calla | last post by:
Hi, I need to display records with lots of fields. When placed in a grid, a row of data will scroll off the screen. Not exactly what I'm looking for. I've also used a datalist, which allows...
7
by: What-a-Tool | last post by:
Have an Access table loaded into a DataTable that was created at run time, and am displaying these contents in a data grid. Me.dgGames.DataSource = Me.DsGameInfo.Tables("Games") I would like to...
4
by: SStory | last post by:
You know there is the controlpaint.Draw3DBorder and it works great for rectangles. How can one do this on ovals. 'thi is my rectangle code ControlPaint.DrawBorder3D(g, rctShape.X, rctShape.Y,...
1
by: thf | last post by:
Hi, I wish to know how to display a collection of object which is a property in another object in a data grid in VB.Net. For example, I'm displaying a collection of vendor objects (in ArrayList)...
0
by: Charles Krug | last post by:
List, I'd like to do the following with Tkinter's Frame() object: 1. Display a collection of pack()-able objects. Easy. Done. I hold the objects in a dictionary, mostly so that the owning...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.