Reputation:
I've been trying to print using the drawString() function of Graphics class in a method other than paint(). I've tried this program which was the solution to an earlier doubt, but this code is not working. Please find me the flaws. Thanks. Here's it below:
import java.awt.*;
import java.applet.*;
public class PaintIssue extends Applet {
Graphics gg; //global Graphics object
@Override
public void init() {}
@Override
public void paint(Graphics g) {
g.drawString("Output of paint method",20,20);
myMethod(); //calling myMethod
}
public static void myMethod() {
gg.drawString("Output of myMethod",20,40);
}
}
Upvotes: 1
Views: 1022
Reputation: 21435
AWT doesn't have a concept of a "global graphics object". You have to pass down the Graphics object that your paint method receives.
@Override
public void paint(Graphics g) {
g.drawString("Output of paint method",20,20);
myMethod(g); //calling myMethod
}
public static void myMethod(Graphics g) {
g.drawString("Output of myMethod",20,40);
}
Upvotes: 1