简体   繁体   English

Swing/AWT 文件选择器验证图形绘制

[英]Swing / AWT filechooser validation graphics drawing

I'm making a drawing program which reads commands from a text file, when file is selected the program should validate the commands for correct entry parameters. I'm making a drawing program which reads commands from a text file, when file is selected the program should validate the commands for correct entry parameters. the text file opened in the program includes;程序中打开的文本文件包括;

Move 100 100 // (move pen to X Y)
MIVE 100 50 // (Invalid comamnd spelt incorrectly)
move x y // (invalid command not an integer)
Line 20 100 // (draw a line at X Y)

The problem that I am having is that upon opening the text file into the program, it is importing the text file but it is not validating it into the JTextArea and drawing the line at selected X / Y co-ordinates.我遇到的问题是,在将文本文件打开到程序中时,它正在导入文本文件,但它没有将其验证到JTextArea并在选定的 X/Y 坐标处绘制线条。 Could anybody point me in the right direction?有人能指出我正确的方向吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Instructionpanel extends JPanel {

JTextArea instructions;

// Move line text clear
public Instructionpanel(GraphicsPanel graphicspanel) {

    instructions = new JTextArea(
            "This is where the instructions will displayed", 10, 50); // Rows
                                                                        // *
                                                                        // columns
    instructions.setLineWrap(true);
    instructions.setWrapStyleWord(true);
    instructions.setEditable(true);
    JScrollPane areaScrollPane = new JScrollPane(instructions);
    areaScrollPane
            .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    add(areaScrollPane);
    // add(instructions);
}

public void processFile(File file) {
    Scanner scan = null;
    try {
        scan = new Scanner(file);

    } catch (FileNotFoundException el) {
        el.printStackTrace();
    }

    String allInstructions = "";

    String allInstructions1 = "";
    String instruction = "";
    while (scan.hasNext()) {
        instruction = scan.nextLine() + "\n";
        // check or validate the instruction

        allInstructions1 += validateInstruction(instruction);

    }

    instructions.setText(allInstructions1);

}

public String validateInstruction(String orig) {
    String returnString = orig;

    // Do all the checking
    // Convert string to an array
    String command = "";
    String[] instructionArray = orig.split("  ");

    int x, y = 0;

    switch(instructionArray[0])
        {
    case "MOVE":
        // Check there three parameters
        doMove( instructionArray );            {
            // And they are integers
            instructions = new JTextArea(" Incorrect parameter type i.e 100");
            instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200");
            try {

                GraphicsPanel.setPos (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2]));

            } 

            catch (NumberFormatException e) 
            {
                instructions = new JTextArea(" only numbers are allowed ");
            }
        }
        break;

            case "LINE":
                doLine ( instructionArray );
        // Check there three parameters
        if (instructionArray.length != 3) {
            // And they are integers
            instructions = new JTextArea(" Incorrect parameter type i.e 100");
            instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200");
            try {
                GraphicsPanel.drawLine (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2]));
            } catch (NumberFormatException e) {
                instructions = new JTextArea(" only numbers are allowed ");
            }break;

            }}
    return orig;}

private void doLine(String[] instructionArray) {
    // TODO Auto-generated method stub

}
private void doMove(String[] instructionArray) {
    // TODO Auto-generated method stub  
}
}

From a quick look I can say that your String[] instructionArray probably created with only one element since the orig.split(" ") has double space as a delimiter, therefore instructionArray[0] contains the whole line since your tokens are spaced with only one space.快速浏览一下,我可以说您的String[] instructionArray可能只用一个元素创建,因为orig.split(" ")有双倍空格作为分隔符,因此instructionArray[0] orig.split(" ") instructionArray[0]包含整行,因为您的标记与只有一个空间。 Therefore none of your cases in validateInstruction(String orig) are matched.因此,您在validateInstruction(String orig)中的所有案例都不匹配。

For now you can probably get away with replacing the double space to single space in the orig.split(" ") call.现在,您可以在orig.split(" ")调用中将双空格替换为单空格。

That being said I cant guaranty this will solve all the potential issues that might occur after the change.话虽如此,我不能保证这将解决更改后可能发生的所有潜在问题。

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

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