Java GUI:repaint()或类交互问题

时间:2018-05-07 17:42:04

标签: java swing class user-interface repaint

所以我正在创建一个乌龟图形项目,而且在给定命令输入时我无法让乌龟移动。下面是两个交互类,我的TextPanel类处理用户输入,GraphicsPanel包含可绘制的'画布'等等。

由于我现在没有收到任何错误,我想知道我是否正确调用了GraphicsPanel类,或者我是否使用了repaint()并且它是错误的方法?我不知所措,可以用一双新鲜的眼睛。谢谢你的时间。

if (boolean[y][x])

这是我的GraphicsPanel类,包含命令函数和JPanel设置,包括bufferedimage等。

public class TextPanel extends JPanel implements ActionListener {

private JTextArea textArea;
private JScrollPane scrollPane;
private JTextField commField;
private JButton enterBut;
private TextListener textListener;
private GraphicsPanel graphicsPanel;

//***********//
//CONSTRUCTOR//
//***********//

public TextPanel() {

    textArea = new JTextArea();
    scrollPane = new JScrollPane ( textArea );
    enterBut = new JButton("Execute");
    commField = new JTextField(10);
    graphicsPanel = new GraphicsPanel();


    textArea.setLineWrap(true);         //stops text from going outside
    textArea.setWrapStyleWord(true);    //panel boundaries

    //**********************//
    //COMMAND IMPLEMENTATION//
    //**********************//

    commField.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {

            if(commField.getText().contains("penup")) {
                GraphicsPanel.penUp();
            }
            else if(commField.getText().contains("pendown")) {
                GraphicsPanel.penDown();
            }
            else if(commField.getText().contains("turnright")) {
                GraphicsPanel.turnRight();
            }
            else if(commField.getText().contains("turnleft")) {
                GraphicsPanel.turnLeft();
            }
            else if(commField.getText().startsWith("forward")) {        //getText method retrives user input from JTextField
                String cForward = commField.getText();                  //<startsWith> searches string for desired command
                String[] cForwardArray = cForward.split("\\s+");        //string array created and split with " " as split
                try {
                    int distance = Integer.parseInt(cForwardArray[1]);  //parseInt takes the integer from string split
                    GraphicsPanel.forward(distance);                    //GraphicsPanel command called + distance integer
                    textArea.append(cForward + "\n");           //test for variable values
                    GraphicsPanel.validate();
                    GraphicsPanel.repaint();                            //canvas is repainted 


                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }
            else if(commField.getText().startsWith("backward")) {
                String cBackward = commField.getText();
                String[] cBackwardArray = cBackward.split("\\s+");
                try {
                    int distance = Integer.parseInt(cBackwardArray[1]);
                    GraphicsPanel.backward(distance);
                    textArea.append(cBackward +"\n");
                    GraphicsPanel.repaint();
                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }

            else if(commField.getText().contains("black")) {
                GraphicsPanel.black(Color.black);
            }
            else if(commField.getText().contains("green")) {
                GraphicsPanel.green(Color.green);
            }
            else if(commField.getText().contains("red")) {
                GraphicsPanel.red(Color.red);
            }
            else if(commField.getText().contains("reset")) {
                GraphicsPanel.clear();
            }
            else {
                JOptionPane.showMessageDialog(textArea, "Invalid command, try again");
            }

            commField.setText("");
            GraphicsPanel.repaint();


        }
    });


    //****************//
    //PANEL DIMENSIONS//
    //****************//

    setBackground(Color.black);

    Dimension dim = getPreferredSize();     //sets dimensions of panel
    dim.width = 250;
    setPreferredSize(dim);

    //setBorder(BorderFactory.createTitledBorder("Enter Commands here: "));
    //setTitleColor(Color.white); //not sure why this doesnt change the text colour


    //**************//
    //GRIDBAG LAYOUT//
    //**************//

    setLayout(new GridBagLayout());
    GridBagConstraints constraint = new GridBagConstraints();

    add(commField, constraint);

    constraint.fill = GridBagConstraints.HORIZONTAL;
    constraint.ipady = 20;
    constraint.ipadx = 60;
    constraint.insets = new Insets(10, 5, 0, 0);
    constraint.weightx = 1;
    constraint.weighty = 0;
    constraint.gridx = 0;                           //grid position
    constraint.gridy = 0;

    add(commField, constraint);
    //***********************//

    constraint.fill = GridBagConstraints.HORIZONTAL;
    constraint.ipady = 15;
    constraint.ipadx = 25;
    constraint.anchor = GridBagConstraints.PAGE_END;    //anchors to bottom of GUI
    constraint.insets = new Insets(10, 0, 0, 0);        //inserts gap
    constraint.weightx = 0;
    constraint.weighty = 0;
    constraint.gridx = 1;                       
    constraint.gridy = 0;

    add(enterBut, constraint);

    //*************************//

    constraint.fill = GridBagConstraints.BOTH;      //option how to fill 'cell' 
    constraint.ipady = 150;                         //dim of component 
    constraint.ipadx = 150;
    constraint.insets = new Insets(0, 5, 0, 0);
    constraint.gridwidth = 3;                       //how many grid cell it spans
    constraint.weightx = 1;                         //precedent for space
    constraint.weighty = 1;
    constraint.gridx = 0;                           //grid position
    constraint.gridy = 1;

    add(new JScrollPane(textArea), constraint);                     //adds component
                                                                    //and JSCrollPane
    //************************//

    //**NOTES****************************//
    //next time use smaller variable name//
    //***********************************//

}


private void setTitleColor(Color white) {


}

public void appendText(String text) {

}


@Override
public void actionPerformed(ActionEvent e) {


}

public void setTextListener(textListener listener) {
    this.TextListener = listener;
}
}

}

0 个答案:

没有答案