简体   繁体   English

在Java小程序中绘制矩形

[英]Drawing a rectangle in Java applet

I am attempting to create a simple applet that can draw a rectangle, I have the following code: 我试图创建一个可以绘制矩形的简单小程序,我有以下代码:

import java.util.Scanner;
import java.awt.Graphics;
import javax.swing.JApplet;
public class DrawShapes extends JApplet{

    public void paint(Graphics canvas) {
        Scanner reader  = new Scanner(System.in); 
        System.out.println("How many sides do you want your shape to have: ");
        int sides = reader.nextInt();
        reader.close();
        super.paint(canvas);



        if(sides ==4){
        canvas.drawRect(100, 50, 200, 200);
        }

        System.out.println("Done!");

    }

}

I the run this code and when prompted to enter how many sides i want i enter "4" 我运行此代码,并在系统提示输入我要多少面时输入“ 4”

A new applet window is then created in the upper left corner without any errors. 然后,将在左上角创建一个新的applet窗口,而不会出现任何错误。 But, the second I attempt to expand the applet the following errors are printed to the console: 但是,第二次尝试扩展小程序时,将以下错误输出到控制台:

Exception in thread "AWT-EventQueue-1" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at DrawShapes.paint(DrawShapes.java:9)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Also, "Applet Started" is repeated in the applet window as such: "Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started Applet Started" What is going on with my code? 同样,在applet窗口中将这样重复“ Applet已启动”:“ Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动Applet已启动”我的代码?

This is because whenever you resize the window, the paint() method will be called again. 这是因为每当您调整窗口大小时,都会再次调用paint()方法。

The first time paint() method is called, the thread will wait for the user input from the console. 第一次调用paint()方法时,线程将等待来自控制台的用户输入。

After the first time, the thread will not wait for the console input any more because you have closed the input stream: 第一次之后,该线程将不再等待控制台输入,因为您已经关闭了输入流:

reader.close();

So either you create only one instance of new Scanner(System.in) and do not close it: 因此,您只能创建一个new Scanner(System.in)实例,而不关闭它:

public class DrawShapes extends JApplet{
    Scanner reader  = new Scanner(System.in); //  a single instance for the Applet
    public void paint(Graphics canvas) {
        super.paint(canvas); // this should be the first statement of the method
        System.out.println("How many sides do you want your shape to have: ");
        int sides = reader.nextInt();

        if(sides ==4){
            canvas.drawRect(100, 50, 200, 200);
        }

        System.out.println("Done!");
    }
}

Or you replace the console input with a JTextField since you are using GUI. 或者,因为使用GUI,所以将控制台输入替换为JTextField。

Painting methods are for painting only! 绘画方法仅用于绘画! Don't do I/O in a painting method. 不要使用绘画方法进行I / O。

Applets are not designed to do file I/O. Applet并非用于文件I / O。 Applets execute inside your web browser. 小程序在您的Web浏览器中执行。 There is no command line associated with the browwer. 没有与浏览器关联的命令行。

If you need user input then you should be using a JOptionPane.showInputDialog(...) to prompt the user for input in the init(...) method of the applet. 如果需要用户输入,则应使用JOptionPane.showInputDialog(...)提示用户在applet的init(...)方法中输入。 You can also use a JOptionPane.showMessageDialog(...) to display a message. 您还可以使用JOptionPane.showMessageDialog(...)显示消息。

Read the section from the Swing tutorial on How to Make Dialogs for more information. 阅读Swing教程中有关如何制作对话框的部分, 获取更多信息。

Also, custom painting is done by overriding the paintCompnent() method of a JPanel and then you add the panel to the applet. 另外,通过覆盖JPanel的paintCompnent()方法来完成自定义绘制,然后将面板添加到小程序中。 The tutorial also has a section on Custom Painting . 本教程还有一个关于“ Custom Painting的部分。 I suggest you read the tutorial as it covers the basics better than your class appears to be doing. 我建议您阅读该教程,因为它涵盖的基础知识比您的课堂看上去要好。

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

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