Java JFrame stuff

TridenT

Lifer
Sep 4, 2006
16,800
45
91
It's pretty simple, but I am having issues trying to figure out how I would draw this with JFrame/JPanel/JComponents:

http://img89.imageshack.us/img89/9986/sudokuboard.png
http://img852.imageshack.us/img852/4186/sudokuedgedetail.png (Close up)
http://img59.imageshack.us/img59/5689/sudokuedgedetail2.png (More details)

I've been gone from class for two weeks practically and the professor (refuses to help) isn't covering stuff that is in the book. So, I am a bit lost.

I've got this so far. http://img192.imageshack.us/img192/4846/codesofar1.jpg Which, really does nothing. I'm not sure what to do. Mind you, each one of those cells/boxes has to be a separate JComponent/thing. So I was thinking somehow you could have the border take up the whole frame, then just overlay the grid on top of that but pushed in on each edge by 2 pixels? I don't know if that is possible.

More details available if needed... But I am lost so far.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Idk how I would draw a rectangle around the whole grid. Draw the rectangle with no fill, but then I need to have a grid in there. :/ Idk how to manage that.

So you draw the grid in there :). Primitives like rectangle, line, ellipse are meant to be combined into more complex figures. Look at what you want to draw, and then decide which primitives to use to get that figure. In the case of a grid it might be just a few lines draw horizontally and vertically from one side of the rectangle to the other.
 

TridenT

Lifer
Sep 4, 2006
16,800
45
91
So you draw the grid in there :). Primitives like rectangle, line, ellipse are meant to be combined into more complex figures. Look at what you want to draw, and then decide which primitives to use to get that figure. In the case of a grid it might be just a few lines draw horizontally and vertically from one side of the rectangle to the other.

It's more complex than that from the assignment. :/ Each cell needs to be a separate JComponent from what I understand

EDIT: Can't use URL shorteners... but I need to.. Hm. http://pastebin.ca/2052400 This contains the info about how it's done.

I need to get the stuff able to be highlighted by clicks, needs to be able to put numbers in it that end up actually being shapes that I custom draw later that I define (I'll define up to 6 numbers and draw them as shapes for a 2x3 board I guess), and more..
 
Last edited:

Leros

Lifer
Jul 11, 2004
21,867
7
81
It's more complex than that from the assignment. :/ Each cell needs to be a separate JComponent from what I understand

EDIT: Can't use URL shorteners... but I need to.. Hm. http://pastebin.ca/2052400 This contains the info about how it's done.

I need to get the stuff able to be highlighted by clicks, needs to be able to put numbers in it that end up actually being shapes that I custom draw later that I define (I'll define up to 6 numbers and draw them as shapes for a 2x3 board I guess), and more..

I haven't done anything graphical in Java in a while so I don't know exactly how this will work.

The general approach I would take is to subclass whatever the standard graphical object is (JComponent perhaps?) to make your cell component. I would guess that class has some method like draw() or paint() which draws that particular component. You'll override that method and draw your cell using the primitive shapes (rectangle, line, etc). Then you'll add some custom methods, such as showing a number in that cell, which will change the internal state of the component and redraw it.
 

TridenT

Lifer
Sep 4, 2006
16,800
45
91
I haven't done anything graphical in Java in a while so I don't know exactly how this will work.

The general approach I would take is to subclass whatever the standard graphical object is (JComponent perhaps?) to make your cell component. I would guess that class has some method like draw() or paint() which draws that particular component. You'll override that method and draw your cell using the primitive shapes (rectangle, line, etc). Then you'll add some custom methods, such as showing a number in that cell, which will change the internal state of the component and redraw it.

Hmm. Yeah, I guess.
 

TridenT

Lifer
Sep 4, 2006
16,800
45
91
I haven't done anything graphical in Java in a while so I don't know exactly how this will work.

The general approach I would take is to subclass whatever the standard graphical object is (JComponent perhaps?) to make your cell component. I would guess that class has some method like draw() or paint() which draws that particular component. You'll override that method and draw your cell using the primitive shapes (rectangle, line, etc). Then you'll add some custom methods, such as showing a number in that cell, which will change the internal state of the component and redraw it.

I know this is basically what I'll have to do, but I am having troubles figuring it out.

I know for instance that if I do a JPanel with a grid layout that each one of those grid cells needs to be drawn by some other class. Now, I don't know how to do really do that for each cell. Something like this is what I came up with Magic(int row, int col, boolean given(It's something as part of the assignment)). Now, that's all dandy, but maybe I should pass the number as well with another parameter added on. That's probably what I should do. But I don't know how I am going to get that class to draw shit for every panel, then get highlighted when I click on each cell. That's trouble.

Code:
for(int i = 0; i < base.size*base.size;i++){
            Magic new1 = new Magic(i / base.size, i &#37; base.size, base.isGiven(i / base.size, i % base.size));
            grid.add(new1);
        }
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
It's more complex than that from the assignment. :/ Each cell needs to be a separate JComponent from what I understand

EDIT: Can't use URL shorteners... but I need to.. Hm. http://pastebin.ca/2052400 This contains the info about how it's done.

I need to get the stuff able to be highlighted by clicks, needs to be able to put numbers in it that end up actually being shapes that I custom draw later that I define (I'll define up to 6 numbers and draw them as shapes for a 2x3 board I guess), and more..

Not knowing the specifics of Java graphics I'll have to lurk this one, but yeah that's a valid approach too. I've done it both ways in Silverlight. It depends on the requirements. Using native components to represent in-game features gets you all the advantages of framework objects, i.e. event handling, drawing, styles, properties, etc. You can also draw the elements yourself and use the event handlers on the top container to obtain input. In some cases I prefer that approach.
 

TridenT

Lifer
Sep 4, 2006
16,800
45
91
Not knowing the specifics of Java graphics I'll have to lurk this one, but yeah that's a valid approach too. I've done it both ways in Silverlight. It depends on the requirements. Using native components to represent in-game features gets you all the advantages of framework objects, i.e. event handling, drawing, styles, properties, etc. You can also draw the elements yourself and use the event handlers on the top container to obtain input. In some cases I prefer that approach.

Well for the event handling of the highlighting of the specific grid cells I am thinking of using some kind of "getCursorClick" type method. It'd just redraw the cell that is within the bounds of where the cursor clicked last with a different background. Of course, every click would have to change about two cells. One would have to turn yellow, one would have to turn back to white or gray. Very unsure of how to go along that too.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Well for the event handling of the highlighting of the specific grid cells I am thinking of using some kind of "getCursorClick" type method. It'd just redraw the cell that is within the bounds of where the cursor clicked last with a different background. Of course, every click would have to change about two cells. One would have to turn yellow, one would have to turn back to white or gray. Very unsure of how to go along that too.

Visual components usually have some notion of being "in focus". I'd be willing to bet that JComponent has event handlers for when the component gets and loses focus. So, when a cell gains focus it could turn yellow and when it loses focus it could turn white.
 

TridenT

Lifer
Sep 4, 2006
16,800
45
91
notperfect.jpg


Finally got something going... ugh. It's pretty much finished as far as I can make out. It sucks when you don't know just about anything about JPanel/JComponent/JStuff