[英]Calling methods to draw on JFrame
Can somebody explain to me why this isn't working? 有人可以向我解释为什么这行不通吗? The error seems to be inside the Gen class but I think it may something to do with BoxMan. 该错误似乎在Gen类内部,但我认为可能与BoxMan有关。 The error says cannot find symbol- variable g. 错误提示找不到符号变量g。 I also tried putting in ints and doubles but it gives me: Required (Java.awt.Graphics) Found(int) / (double). 我也尝试放入int和doubles,但是它给了我:必需(Java.awt.Graphics)Found(int)/(double)。 So how can fix this? 那么如何解决呢? I've looked everywhere and can't find the answer. 我到处都是,找不到答案。 Help a beginner! 帮助初学者!
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.lang.Object.*;
public class JFrame_Test
{
public static void main (String [] args)
{
Gen Gen= new Gen (1500,1000,"A Name"); // this gives parameters for a Jframe later.
}
}
{
Gen (int size1, int size2, String title)
{
JFrame aFrame = new JFrame (title);
aFrame.setSize(size1,size2);
aFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
aFrame.setVisible(true);
//aFrame.getContentPane().add(new Canvas());
//Was trying to get it to work with a canvas
BoxMan.paint (g); // the error pops up here.
}
}
public class BoxMan
{
public Graphics2D g2;
public void paint(Graphics a )
{
g2 = (Graphics2D) g; // i even tried declaring "g" here.
g2.drawRect (10, 10, 200, 200);
}
}
Rather then repeating what Jantomedes has already said (which is all excellent), I'm going to expand on it... 而不是重复Jantomedes已经说过的话(这一切都很棒),我将在其上进行扩展...
Painting in AWT and Swing is done through the paint sub system. AWT和Swing中的绘画是通过绘画子系统完成的。 This system makes decision about what and when to paint and calls the appropriate methods to update the components on the screen. 该系统决定何时涂漆以及何时涂漆,并调用适当的方法来更新屏幕上的组件。
See Painting in AWT and Swing for more details 有关更多详细信息,请参见在AWT和Swing中绘画
Graphics
is an abstract concept in Java and is used to standardise the concept of painting to a variety of possible outputs, including the screen, image and printers. Graphics
是Java中的抽象概念,用于将绘画概念标准化为各种可能的输出,包括屏幕,图像和打印机。 Apart from images, you can't create your own Graphics
context, you need it to be supplied by the system 除了图像,您无法创建自己的Graphics
上下文,需要由系统提供
Check out Perfoming Custom Painting in Swing for details 查阅Swing中的Perfoming Custom Painting了解详细信息
The Graphics
object isn't declared anywhere. Graphics
对象未在任何地方声明。 If you want to draw on your JPanel
you should rather create a class extending JPanel
and there add draw()
method which will get an "automated" Graphics
object. 如果要在JPanel
上进行绘制,则应该创建一个扩展JPanel
的类,并在其中添加draw()
方法,该方法将获得“自动” Graphics
对象。
Eventually you can create your own Graphics
object but you didn't do that anywhere in that code. 最终,您可以创建自己的Graphics
对象,但是您并没有在该代码中的任何地方这样做。 Your BoxMen
class is very messy. 您的BoxMen
类非常混乱。 You have to decide if you use Graphics
object argument under paint()
method or declare it yourself. 您必须决定是在paint()
方法下使用Graphics
对象参数还是自己声明。 I'm assuming you try the second one, if so, you should change the g
to a
(there isn't a g
variable anywhere in BoxMen
class). 我假设您尝试第二个,如果这样做,则应将g
更改为a
( BoxMen
类中的任何地方都没有g
变量)。 You can also get rid of the field g2
and use a local variable instead. 您也可以摆脱g2
字段,而改用局部变量。
The error pops up because Java doesn't know what you mean by g
(it isn't declared anywhere). 由于Java不了解g
含义(未在任何地方声明),因此会弹出错误消息。 It depends on you if you want to use the JPanel
's Graphics
or your own. 是否要使用JPanel
的Graphics
或您自己的Graphics
,取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.