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: -
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]);
-
}
-
}
-
}
-
And the Circle class -
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;
-
}
-
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.
10 3207
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
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: -
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
-
}
-
}
-
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
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.
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
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.
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: -
public void paintComponent(Graphics g) {
-
super.paintComponent(g); // if you want the default background
-
g.setColor( ... ); // set color of the oval
-
g.fillOval(0, 0, this.getWidth(), this.getHeight()); // draw oval
-
}
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
This draws like 4 rectangles across the JPanel all squashed together. :S
This draws like 4 rectangles across the JPanel all squashed together. :S
Show a bit of the relevant code.
kind regards,
Jos
Hey dude it was an error on my behalf. I do apologise. All working now.
Thank you for your help. :)
Take care.
- Jamie
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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)...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |