3

I have made a Line class in Java, and I also have a drawing class with a method in it that draws that Line to the screen; when I call that method, it also centers the Line, and once the method is finished, it edits the value of the original Line.

A non-ideal solution I have thought of is de-centering the Line to change its value back, but I have a lot more stuff than just a Line and a drawing class in this project, and I would like to know a way to avoid this problem for good. In C++, I know that you can send parameters by address or by pointer, and to the best of my knowledge, you can't do that in Java, but if there's anything similar to that, that could possibly be the best solution.

This is the code for the method to draw the Line on the screen.

public void drawLine(Line ln) {
    //I have used a new variable to try to avoid the problem, but it doesn't work.
    Line buf = new Line();
    buf.begin = ln.begin;
    buf.end = ln.end;

    buf.begin = centerPoint(buf.begin);
    buf.end = centerPoint(buf.end);
    gfx.drawLine((int) buf.begin.x, (int) buf.begin.y, (int) buf.end.x, (int) buf.end.y);
}

Here is how I am using the Line

    Line ln = new Line(0, 0, 100, 0);
    draw.drawLine(ln);
    System.out.println(ln.begin.x + ", " + ln.end.x);
    //It should print 0, 0, but instead it prints out a different number, because it has been centered.

Thanks for your help.

divibisan
  • 11,659
  • 11
  • 40
  • 58
foo bar
  • 75
  • 6
  • 4
    You can avoid "pass by reference" by using Java, because it doesn't support that. And why do you center that line? – Tom Jul 01 '15 at 06:07
  • 1
    See this question about "Is Java “pass-by-reference” or “pass-by-value”?" http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – bish Jul 01 '15 at 06:09

1 Answers1

3

It looks like your problem is that the state of the ln.begin and ln.end instances is changed by the centerPoint method.

You can avoid that if buf.begin and buf.end would be copies of those instances instead of referring to them.

public void drawLine(Line ln) {
    Line buf = new Line();
    buf.begin = new Point(ln.begin); // I'm guessing the class name and available constructor
    buf.end = new Point(ln.end); // I'm guessing the class name and available constructor

    buf.begin = centerPoint(buf.begin);
    buf.end = centerPoint(buf.end);
    gfx.drawLine((int) buf.begin.x, (int) buf.begin.y, (int) buf.end.x, (int) buf.end.y);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • I would make it Clonable – SwiftMango Jul 01 '15 at 06:11
  • @texasbruce That's another option, though some people argue that the Cloneable interface is broken. – Eran Jul 01 '15 at 06:12
  • It's broken? How so? It is in JRE very long ago and never deprecated. – SwiftMango Jul 01 '15 at 06:13
  • I don't see any solid points from him. "extra-linguistic" what does that even mean? If you modify one, the other changes as well. And all of a sudden, you get random behavior. Yes that's because it is a shallow copy. The interface is not broken, but whoever uses it needs to know how to properly handle it. – SwiftMango Jul 01 '15 at 06:22
  • How do I make it Cloneable? – foo bar Jul 01 '15 at 06:26