0

I'm using the Slick library as the framework for my small game. Linebreaks ain't supported by default on the Graphics2d object. However, i found this little fix:

private void drawString(Graphics g, String text, int x, int y) {
    for (String line : text.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
}

As i am very new to Java, i am not sure what the easiest way to implement this would be, and would really love some help! :-)

vels4j
  • 11,208
  • 5
  • 38
  • 63
Patrick Reck
  • 303
  • 1
  • 10
  • 24
  • 1
    what is the problem with above approach? can you be clearer what sort of help? – vishal_aim Dec 05 '12 at 08:55
  • Well, where can i put this code? If i commandclick the drawString method, it takes me to the class, but it says the method itself is compiled and read-only – Patrick Reck Dec 05 '12 at 08:56

3 Answers3

0

My advice is: download the sources of the Silk library you're using, you can download https://bitbucket.org/kevglass/slick.

Editing method you specified. Build it and include it in your project. With IDE like NetBeans or Eclipse is simple.

You should submit a bug with relative patch so that it is integrated in the general library.

user1594895
  • 587
  • 1
  • 10
  • 31
0

What you want in your case is :

a) To overload the given method (in case you have access to the class and maybe add one more parameter )

example :

private void drawString(Graphics g, String text, int x, int y,boolean test)

and pass a true on test or something like that but this is a crude way of doing things

b) Otherwise you could just extend the class that implements your method and re implement the method as you wish in the extended class

c) or as a third alternative which maybe is the most viable in this case is to just edit the original method as @user1594895 states, but in this case you must be sure you won't make any use of the original method otherwise consider doing a) or b)

Let me know what kind of approach you've decided to use.

Lucian Enache
  • 2,510
  • 5
  • 34
  • 59
0

An easy way (which is already mentioned in your own post) which you should apply when you only call this method from one class: Just insert the code snippet above in your class, then you call it like any other method..it's kind of an helper method then. (just ask If you don't get what I mean)


Another easy way to do is - if the class is not final - is just to extend the class and override the method:

public class MyGraphics extends Graphics {
      @Override
      public void drawString(String text, int x, int y) {
         for (String line : text.split("\n"))
            super.drawString(line, x, y += getFontMetrics().getHeight());
      }
}

Then you just work with your own class - which is exactly the same as the other class, expect your drawString Method is different, you would instantiate it like this: Graphics g = new MyGrpahics();

OschtärEi
  • 2,255
  • 3
  • 20
  • 41