Customized link

Hi Walter,
I have two JGoTextNode objects linked by a JGoLink. I would like to
have an oblong box drawn around the arrow when I move the
mouse over the link. This box should appear when they are near
the link, not necessarily on top of the link. The box will be in the
background of the link. Can you advise?

Hi. I moved this topic from the GoDiagram forum.
Here’s some code to get you started. First, a JGoLink class that customizes the paint method to first draw a round rectangle around the link:
public class TestLink extends JGoLink {
public TestLink() { }
public TestLink(JGoPort to, JGoPort from) { super(to, from); }
public boolean isHighlighted() { return myHighlighted; }
public void setHighlighted(boolean b) {
boolean old = myHighlighted;
if (old != b) {
myHighlighted = b;
private boolean myHighlighted = false;
public boolean doUncapturedMouseMove(int flags, Point dc, Point vc, JGoView view) {
if (view instanceof Demo1View) {
return true;
return super.doUncapturedMouseMove(flags, dc, vc, view);
public void paint(Graphics2D g, JGoView view) {
if (isHighlighted()) {
Rectangle r = getBoundingRect();
JGoDrawable.drawRoundRect(g,, null, r.x, r.y, r.width, r.height, 10, 10);
super.paint(g, view);
This depends on using a JGoView subclass that keeps track of the current “highlighted” object:
public class Demo1View extends JGoView {

public void doUncapturedMouseMove(int modifiers, Point dc, Point vc) {
super.doUncapturedMouseMove(modifiers, dc, vc);
if (pickDocObject(dc, false) == null) setHighlighted(null);
public JGoObject getHighlighted() { return myHighlighted; }
public void setHighlighted(JGoObject x) {
JGoObject old = myHighlighted;
if (old != x) {
if (old instanceof TestLink) ((TestLink)old).setHighlighted(false);
myHighlighted = x;
if (x instanceof TestLink) ((TestLink)x).setHighlighted(true);
private JGoObject myHighlighted = null;
Of course you’ll want to adapt this code to meet your own needs.

I dont see the rectangle around my link object using the code snippet

I just copied the code from a modified version of Demo1 that I made.
Are you sure that you have instances of TestLink in your document?
For user-drawn links, you’ll want to override JGoView.newLink to create an instance of TestLink; Demo1View sometimes creates a JGoLabeledLink, and sometimes a JGoLink – I just modified it to initialize the “L” local variable to a newly allocated TestLink, throwing out the code to maybe create a JGoLabeledLink.

I noticed that in line:
JGoDrawable.drawRoundRect(g,, null, r.x, r.y, r.width, r.height, 10, 10);

r.height is 0. When I replace r.height with say “60”, I see a rectangle.
Do you know why?

Oh, if the link (aka stroke) is completely horizontal or vertical, either the height or width (or both!?) could be zero.
You need to decide how large to make the rectangle.
You will also need to override expandRectByPenWidth to make sure the region to be (re-)painted includes the region where you might be drawing.

Hi Walter,
Thanks for the response. Is it possible to add an icon to the rectangle

You can add whatever drawing code you want. You’ll want to call Graphics2D.drawImage.