aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Can I add an arrow onto a shape in a GridLayout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can I add an arrow onto a shape in a GridLayout?" Watch "Can I add an arrow onto a shape in a GridLayout?" New topic
Author

Can I add an arrow onto a shape in a GridLayout?

Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
I have an applet which uses a Gridlayout to display 6 differently colored squares, triangles, circles etc. I now want to have an arrow point to each shape in turn, and the child playing the game should click a switch when the red triangle (or blue circle etc.) is being pointed to.

Is it possible to have an arrow point each shape in turn? or is there another way to have each shape selected so that the child will know when the required shape and color is currently chosen and will be able to click a switch, click a mouse or press the space bar?

Any ideas will be welcome


<a href="http://www.geocities.com/robslearningfun" target="_blank" rel="nofollow">http://www.geocities.com/robslearningfun</a><br /><a href="http://www.geocities.com/appletsforteachers" target="_blank" rel="nofollow">http://www.geocities.com/appletsforteachers</a>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You could set a border, say LineBorder or MatteBorder, on the component in the GridLayout to "select" it. Then setBorder(null) or set an EmptyBorder to deselect it when you move on to the next component.
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
Thanks, Craig, this sounds good.

When you say, "set a border on the component in the GridLayout", do you mean on one of the cells of the grid that contain the triangles, squares etc? I can do this when I addthe shapes, I think, but is it possible to change the border of existingindividual cells inside the grid so that I can select cells 0 to 5 in turn?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
If you drew your colored squares on JComponents (or JPanels) and added these to the GridLayout then you can set their borders at any time. All you need is a reference to the JComponents which you can store in a member variable, save at construction or obtain later in your event code with Container methods such as getComponent(int n) or getComponents. Sometimes it’s handy to keep the references in an array — then you can get to them with an index.
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
Thanks Craig,

I can't get down to this until Wednesday but, then I'll get the books out with some idea of what I am looking for.

I'll be back, either with tales of success or begging for even more help.
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14


I have included my code above. I hope that this is allowed.

I think that I have drawn the shapes in a JPanel and have added them to a GridLayout (am I correct?) but how do I create a reference which I can use to switch the Borders off and on.

Any help or criticism would be greatly appreciated. On reading this I must admit that I sound like a school student looking to have my assignment done for me but I am, in fact, a teacher just looking to advance a little.

Thanks Craig
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I think that I have drawn the shapes in a JPanel and have added them to a GridLayout (am I correct?) yes
but how do I create a reference which I can use to switch the Borders off and on. you're there
1 —

In your init method you saved a reference to each of the ShapeDraw objects you added to your applet. You can refer to the second one with the reference sh1, a local variable. You can use this to set a border with

The way your applet is written you could have added the ShapeDraw panels without saving a reference to each one

2 — to prevent the soFar variable from exceeding the range of the arrays it is used to index into you can limit its range with something like

or

3 — it is wise to avoid putting logic/decision–making code inside paintComponent. One side–effect of the do while loop at the top of the method is that everything is changed with each repaint. You can see the difficulty by partially covering the applet and then uncovering it: the covered/uncovered part will be repainted leaving the undisturbed part intact. To avoid this you can put this loop in event code and call it with an event, eg, via mouse or button.

4 — this line

can also be written
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
Thanks Graig,

My little applet is up and running now and your idea of using the borders to show the selected shape is better than my original idea of pointing to each shape with an arrow.

Thanks also for the improvements that you have suggested. I had noticed that an exceeding range message was occuring sometimes but hadn't got round to finding out why. Now I know and I can get on with the rest of the applet.

Perhaps some day I'll be able to do something useful with java.

Thanks again Craig.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can I add an arrow onto a shape in a GridLayout?