如何在 Java 中初始化 Graphics 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1965347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I initialize a Graphics object in Java?
提问by Muntasir
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?
它说 g1 没有初始化。但是我如何初始化一个抽象类呢?
采纳答案by Chad Okere
Well there are two issues here 1:
那么这里有两个问题1:
Graphics g1;
a.paint(g1);
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:
并且您收到 G1 未初始化的错误。那是因为变量 g1 从未设置为任何内容,这会导致编译错误。要编译代码,您至少需要这样做:
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. In order to actually cause your graphics to draw you need to this:
然而,这显然不会帮助你太多。当您尝试运行代码时,您将收到 NullPointerException。为了实际使您的图形绘制,您需要这样做:
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. To get it to appear on the screen you need something like:
但是,这仍然不起作用,因为 Anim1 不会出现在屏幕上。要让它出现在屏幕上,你需要像这样:
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. 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)
现在请注意,我们实际上并没有自己调用paint() 函数。这由 awt 处理,它实际上选择图形上下文,并为我们调用我们的绘制函数。但是,如果您愿意,您可以传入任何您想要的图形对象并要求它在其上绘制。(所以如果你想把你的组件绘制到图像上,你可以这样做)
(note, I changed the classname from anim1 to So1)
(注意,我将类名从 anim1 更改为 So1)
回答by Amir Afghani
回答by Bill the Lizard
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 Appletto see how the Graphics object is handled within the applet.
小程序不需要像常规 Java 应用程序那样的 main 方法。我建议从Sun 的 Applets 教程开始。特别是您可能想跳到Applet 的生命周期部分,以了解如何在Applet内处理 Graphics 对象。
回答by S M Kamran
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.
您需要调用 repaint 或 update 方法,而不是调用paint(Graphics g)。但为此,您的类必须属于 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 方法,并且主要尝试调用 Paint 方法。您需要调用 repaint 或 update 方法(如果您的类位于 java.awt.Container 的层次结构中)而不是绘制,并且 java 的事件调度系统会调用您重写的绘制方法本身。
回答by Suraj Chandran
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()
就是您所需要的!!
回答by Znd
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);
}
}
回答by DSOI__UNUNOCTIUM
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.
你没有,你使用 getGraphics,但是,如果你真的想初始化它,然后输入 new Graphics(){}; 并享受填充所有抽象方法的乐趣。大多数情况下,只需将代码放入paint(g)中即可,您需要记住将小程序设置为可见,通常,它应该是构造函数中的最后一行,甚至在构造函数之外,我曾经犯过一个错误使其可见,然后初始化一堆变量,它显示了一段时间的黑屏。