如何在 Java 中初始化 Graphics 对象?
这是代码:
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);
}
}
它表示 g1 未初始化。但是如何初始化抽象类呢?
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. But how do I initialize an abstract class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里有两个问题 1:
您收到 G1 未初始化的错误。这是因为变量 g1 从未设置为任何值,这会导致编译错误。要编译代码,您至少需要这样做:
但是,这显然不会对您有太大帮助。当您尝试运行代码时,您将收到 NullPointerException。为了真正让你的图形绘制出来,你需要这样做:
但是,这仍然不起作用,因为 Anim1 不会出现在屏幕上。要让它出现在屏幕上,您需要类似的东西:
现在请注意,我们实际上并没有自己调用 Paint() 函数。这是由 awt 处理的,它实际上选择图形上下文,并为我们调用绘制函数。不过,如果您愿意,您可以传入任何您想要的图形对象并要求它在其上进行绘制。 (所以如果你想将组件绘制到图像上,你可以这样做)
(注意,我将类名从 anim1 更改为 So1)
Well there are two issues here 1:
And you are getting the error that G1 is not initialized. That's because the variable g1 is never set to anything, and that causes a compile error. To get the code to compile, you would need to, at the very least do this:
However, that obviously won't help you out too much. You'll get a NullPointerException when you try to run your code. In order to actually cause your graphics to draw you need to this:
However, that still won't work because Anim1 will not appear on the screen. To get it to appear on the screen you need something like:
Now notice, we don't actually call the paint() function ourselves. That's handled by the awt, which actually picks the graphics context, and calls our paint function for us. 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)
Applet 不需要像常规 Java 应用程序那样的 main 方法。我建议从Sun 的Applets 教程开始。特别是,您可能想跳到 生命周期部分一个 Applet 以查看 Graphics 对象在 applet 中是如何处理的。
An applet doesn't need a main method like a regular Java application does. I'd recommend starting with Sun's Applets Tutorial. 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.
您需要做的就是简单地删除 main 方法,如下所示:
All you need to do is simply remove the main method like so:
您需要调用重绘或更新方法,而不是调用paint(Graphics g)。但为此,您的类必须属于 java.awt.Container 中的层次结构。
对于您的类,您已经重写了 Paint 方法,并且主要是您尝试调用 Paint 方法。您需要调用重绘或更新方法而不是绘制(如果您的类位于 java.awt.Container 的层次结构中),并且 java 的事件调度系统会调用您覆盖的绘制方法本身。
instead of a call to paint(Graphics g) you need to invoke the repaint or update method. But for this your class must belong to the hierarchy in the java.awt.Container.
For your class you have overriden the Paint method and in the main you are trying to invoke the paint method. 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.
您应该在paint方法中操作组件的图形并调用repaint()或update(),但不能直接调用paint方法。
从此处开始了解更多信息。
You should manipulate the graphics of a component within the paint method and invoke repaint() or update(), but not the paint method directly.
Start here for some more information.
您不初始化 Graphics 对象。
您可以通过 Component#getGraphics() 方法从组件获取图形对象。
在您的特定情况下,我认为
repaint()
就是您所需要的!You dont initialize a Graphics object.
You get the graphics object from a component via
Component#getGraphics()
method.In your particular case I think
repaint()
is all you need!!你不需要,你可以使用 getGraphics,但是,如果你真的想初始化它,那么输入 new Graphics(){};并享受填充所有抽象方法的乐趣。大多数时候,只需将代码放入 Paint(g) 中就可以了,您需要记住将小程序设置为可见,通常,它应该是构造函数中的最后一行,甚至在构造函数之外,我曾经犯过一个错误变得可见,然后初始化了一堆变量,它显示了一段时间的黑屏。
You don't, you use getGraphics, but, if you really want to initialize it, then type 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.