简体   繁体   English

在Java中,如何防止图形小程序多次运行?

[英]In Java, how do I prevent Graphics Applets from running multiple times?

I'm trying to write a simple program that draws fractals, and wrote the following code. 我正在尝试编写一个绘制分形的简单程序,并编写了以下代码。 I ran it to test whether it worked and found that it runs twice every time. 我运行它来测试它是否有效,发现它每次都运行两次。 I looked for answers and found that it likely has something to do with the graphics window redrawing and re-running the entire code as a result. 我寻找答案,发现它可能与图形窗口重绘并因此重新运行整个代码有关。 In other questions I've seen, no one seemed concerned with making the code not run multiple times, but rather why it was doing so, and no remedies were provided as a result. 在我看到的其他问题中,似乎没有人关心使代码不要多次运行,而是为什么这样做了,因此没有提供任何补救措施。 However, Because of the fact that I'm using Scanners, it's extremely inconvenient to have the program run repeatedly and my primary concern is making it run only once. 但是,由于我使用的是扫描仪,因此重复运行该程序非常不便,而我主要担心的是使其只能运行一次。

import java.awt.*;
import java.applet.*;
import java.util.Scanner;
public class FractalCreator extends Applet
{
     public void paint(Graphics g)
     {
          Scanner s=new Scanner(System.in);
          System.out.println("How many branches do you want each node to have?");
          int branches=s.nextInt();
          System.out.println("How many times do you want the fractal to repeat?");
          int repetitions=s.nextInt();
     }
}

What I want to know is how I can stop the graphics window from redrawing and re-running everything, or, if that isn't possible, what alternatives I have to the Graphics class to draw things in. 我想知道的是如何停止图形窗口重新绘制和重新运行所有内容,或者,如果不可能的话,我必须使用Graphics类的哪些替代项来绘制图形。

I put the code into non-Applet code with a main method and it ran once, as it was intended to. 我使用主要方法将代码放入非Applet代码中,并且按预期运行了一次。

If it is relevant, I'm using DrJava as an IDE. 如果相关,我正在使用DrJava作为IDE。

The answer to your question is, you can't. 您问题的答案是,不能。

So you screwing a bunch of things up. 所以你搞砸了很多事情。 An applet won't (normally) have a console for use interaction - you especially shouldn't be doing this from within the paint method as this could adversely effect the user interaction with your UI. 小程序(通常)不会具有使用交互的控制台-特别是您不应该从paint方法内部进行此操作,因为这可能会对用户与UI的交互产生不利影响。

GUI's are event driven environments, that is, something happens and you respond to it. GUI是事件驱动的环境,也就是说,发生了某些事情,您对此做出了响应。 You are still thinking in a linear/procedural/console based way 您仍在考虑基于线性/过程/控制台的方式

My first piece of advice is, don't use applets, at all, they aren't worth your time to learn right now. 我的第一条建议是,不要使用applet,因为它们根本不值得您花时间学习。 If you want a graphical output, I would start by having Creating a GUI With JFC/Swing for a better understanding of how to interact with users through an event driven environment. 如果需要图形输出,我将首先使用JFC / Swing创建GUI ,以更好地了解如何通过事件驱动的环境与用户进行交互。

I'd then suggest you have a look at Painting in AWT and Swing and Performing Custom Painting for a better understanding of how painting works in Swing. 然后,我建议您看看AWT中的绘画和Swing中的 绘画执行自定义绘画 ,以更好地了解Swing中的绘画工作原理。

Solution 1: Use a member variable to indicate whether the Applet has been painted already. 解决方案1:使用成员变量指示Applet是否已经绘制。 Might be prone to errors. 可能容易出错。

Solution 2: In the init() method, draw your picture to a BufferedImage member variable, and in the paint() method, simply draw that picture. 解决方案2:在init()方法中,将图片绘制到BufferedImage成员变量,而在paint()方法中,仅绘制该图片。

As the answer before me stated, you are acting inside the wrong event method for handling the painting, because as you noticed, it will get drawn over and over again. 就像我之前回答的那样,您正在使用错误的事件方法来处理绘画,因为正如您所注意到的,它将被一遍又一遍地绘制。

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

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