Java super.paintComponents(g) 的问题,它会打印屏幕
我正在尝试用 java 制作一个带有类和层次结构的绘画。但是我的绘画区域没有获得背景颜色(定义为白色),当我单击它时,它会在绘图的 jpanel 区域中生成一个打印屏幕。使用 super.paintComponent(g) 界面显示正常,但我每次只得到一分。使用 super.paintComponents(g) 它会在 jpanel 区域中打印框架。
对发生的事情有什么想法吗?
public class MandaDesenhar extends JPanel
{
static int x;
static int y;
private static final long serialVersionUID = 1L;
int i = 0;
public void paintComponent(Graphics g)
{
super.paintComponents(g);
if (Paint4Fun.lista.size() == 0)
return;
while (i<Paint4Fun.lista.size())
{
FormaPrimitiva forma = Paint4Fun.lista.get(i);
forma.desenha(g);
i++;
}
}
I'm trying to make a paint in java, with classes and hierarchy. But my paint area isn't getting the background color (defined as white) and when i click it makes a print screen in the jpanel area of drawing. With super.paintComponent(g) the interface appears all right but i only get one point of each time. With super.paintComponents(g) it prints the frame in the jpanel area.
any thoughts about what's happen?
public class MandaDesenhar extends JPanel
{
static int x;
static int y;
private static final long serialVersionUID = 1L;
int i = 0;
public void paintComponent(Graphics g)
{
super.paintComponents(g);
if (Paint4Fun.lista.size() == 0)
return;
while (i<Paint4Fun.lista.size())
{
FormaPrimitiva forma = Paint4Fun.lista.get(i);
forma.desenha(g);
i++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在
paintComponent
方法中本地定义i
,而不是在其外部,并将其初始化为0
。否则,您总是只绘制列表中的新元素,而不是旧元素。
编辑:
您可以将循环更好地编写为 for 循环:
或者更清楚:
通常,始终在尽可能小的范围(这里是方法,甚至是循环)中声明变量(例如这里的
i
) 。You should define
i
locally in yourpaintComponent
method, not outside of it, and initialize it there to0
.Otherwise you are always only painting the new elements of your list, not the older ones.
Edit:
You can write your loop better as a for-loop:
or even more clearly:
Generally, always declare variables (like the
i
here) in the smallest possible scope (the method or even the loop, here).