简体   繁体   English

如何在Java中初始化Graphics对象?

[英]How do I initialize a Graphics object in Java?

this is the code: 这是代码:

import java.awt.*;
import java.applet.*;

public class anim1 extends Applet{

    public void paint (Graphics g)
    {
        g.drawString("",400,300);
    }

    public static void main(String ad[])
    {
        anim1 a=new anim1();
        Graphics g1;
        a.paint(g1);
    }
}

It says that g1 is not initialized. 它说g1没有初始化。 But how do I initialize an abstract class? 但是如何初始化抽象类呢?

Well there are two issues here 1: 那么这里有两个问题1:

    Graphics g1;
    a.paint(g1);

And you are getting the error that G1 is not initialized. 并且您收到G1未初始化的错误。 That's because the variable g1 is never set to anything, and that causes a compile error. 那是因为变量g1永远不会设置为任何东西,这会导致编译错误。 To get the code to compile, you would need to, at the very least do this: 要获得编译代码,您至少需要这样做:

    Graphics g1 = null;
    a.paint(g1);

However, that obviously won't help you out too much. 但是,这显然不会帮助你太多。 You'll get a NullPointerException when you try to run your code. 当您尝试运行代码时,您将收到NullPointerException。 In order to actually cause your graphics to draw you need to this: 为了实际绘制您的图形,您需要这样:

    anim1 a=new anim1();
    Graphics g1 = anim1.getGraphics();
    a.paint(g1);

However, that still won't work because Anim1 will not appear on the screen. 但是,由于Anim1不会出现在屏幕上,因此仍然无效。 To get it to appear on the screen you need something like: 要让它出现在屏幕上,您需要以下内容:

import java.awt.*;
import javax.swing.*;
import java.applet.*;

public class So1 extends Applet{

    public void paint (Graphics g)
    {
        g.drawString("hello",40,30);
    }

    public static void main(String ad[])
    {

        JFrame jp1 = new JFrame();
        So1 a=new So1 ();
        jp1.getContentPane().add(a, BorderLayout.CENTER);
        jp1.setSize(new Dimension(500,500));
        jp1.setVisible(true);

    }
}

Now notice, we don't actually call the paint() function ourselves. 现在注意,我们实际上并没有自己调用paint()函数。 That's handled by the awt, which actually picks the graphics context, and calls our paint function for us. 这是由awt处理的,它实际上选择了图形上下文,并为我们调用了paint函数。 If you want, though, you can pass in any graphics object you want and ask it to draw on to that. 但是,如果你想要,你可以传入你想要的任何图形对象,并要求它绘制它。 (so if you want to draw your component onto an image, you can do it) (所以如果你想把你的组件画到图像上,你可以这样做)

(note, I changed the classname from anim1 to So1) (注意,我将类名从anim1更改为So1)

All you need to do is simply remove the main method like so: 您需要做的就是删除main方法,如下所示:

import java.awt.*;
import java.applet.*;

public class anim1 extends Applet {

    public void paint (Graphics g) {
        g.drawString("Hello",100,100);
    }
}

An applet doesn't need a main method like a regular Java application does. applet不需要像常规Java应用程序那样的main方法。 I'd recommend starting with Sun's Applets Tutorial . 我建议从Sun的Applets教程开始 In particular you may want to skip ahead to the section Life Cycle of an Applet to see how the Graphics object is handled within the applet. 特别是,您可能希望跳到Applet的生命周期部分,以查看如何在applet中处理Graphics对象。

You should manipulate the graphics of a component within the paint method and invoke repaint() or update(), but not the paint method directly. 您应该在paint方法中操作组件的图形并调用repaint()或update(),而不是直接调用paint方法。

Start here for some more information. 这里开始获取更多信息。

instead of a call to paint(Graphics g) you need to invoke the repaint or update method. 而不是调用paint(Graphics g),你需要调用repaint或update方法。 But for this your class must belong to the hierarchy in the java.awt.Container. 但为此,您的类必须属于java.awt.Container中的层次结构。

For your class you have overriden the Paint method and in the main you are trying to invoke the paint method. 对于您的类,您已经覆盖了Paint方法,并且在主要中您尝试调用paint方法。 Instead of paint you need to invoke the repaint or update method (if your class is in the hierarchy of java.awt.Container) and the event dispatching system of java invokes your overridden paint method itself. 您需要调用repaint或update方法(如果您的类位于java.awt.Container的层次结构中)而不是paint,而java的事件调度系统将调用您重写的paint方法本身。

You dont initialize a Graphics object. 你没有初始化Graphics对象。

You get the graphics object from a component via Component#getGraphics() method. 您可以通过Component#getGraphics()方法从组件中获取图形对象。

In your particular case I think repaint() is all you need!! 在您的特定情况下,我认为repaint()就是您所需要的!

You don't, you use getGraphics, but, if you really want to initialize it, then type new Graphics(){}; 你没有,你使用getGraphics,但是,如果你真的想初始化它,那么输入new Graphics(){}; And have fun filling all the abstract methods. 并且尽情享受所有抽象方法。 Most of the times just putting code in paint(g) should do, you need to remember to set your applet visible, and usually, it should be the last line in your constructor or even outside it, I have made a mistake once where I made visible and then initialized a bunch of variables, it showed a black screen for a while. 大多数情况下只需要将代码放在paint(g)中就可以了,你需要记住将applet设置为可见,通常,它应该是构造函数中的最后一行,甚至在它之外,我曾经犯过一次错误使得可见,然后初始化了一堆变量,它显示了一段时间的黑屏。

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

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