简体   繁体   English

如何在applet外部paint()方法中进行打印

[英]How to print in applet outside paint() method

I've been trying to print using the drawString() function of Graphics class in a method other than paint() . 我一直在尝试使用paint()以外的方法使用Graphics类的drawString()函数进行打印。 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);
    }
}

AWT doesn't have a concept of a "global graphics object". AWT没有“全局图形对象”的概念。 You have to pass down the Graphics object that your paint method receives. 您必须传递paint方法收到的Graphics对象。

    @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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM