Search code examples
javaslick2d

Editing already existing method


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! :-)


Solution

  • 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.