问题paintComponent()被调用但不起作用

时间:2019-03-13 22:04:16

标签: java

我正在创建一个与Paint类似的程序,但是该程序仅允许通过命令行调用的命令进行绘制。以前,我使用((Graphics2D) getGraphics) .drawLine ()来绘制panel,它可以正常工作,但是出于各种需要,我不得不使用paintComponent

我的问题是PaintPanel的paintComponent方法被正确调用,但是不起作用。

public class Frame extends javax.swing.JFrame {
PaintPanel panel;
CommandLine cmd;

/**
 * Creates new form Frame
 */
public Frame() {
    initComponents();

    // Crea il pannello sul quale disegnare e lo inserisce
    panel = new PaintPanel(coordsLabel);
    panel.setSize(containerPanel.getSize());
    //panel.setVisible(true);
    add(panel);

    cmd = new CommandLine(panel);

    // Sistema la grafica
    linesList.setBackground(jPanel1.getBackground());
    inputCommand.requestFocus();
    colorLabel.setBackground(PaintPanel.COLOR_BLACK);
    colorLabel.setOpaque(true);
    widhtLabel.setText("Widht: " + cmd.getPanel().getWidth());
    heightLabel.setText("Height: " + cmd.getPanel().getHeight());
    labelLookCommand.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

/**
 * Invia il comando chiamando gli appositi metodi.
 */
private void sendTheCommand() {        
    if (inputCommand.getText().isEmpty()) {
        return;
    }

    outputCommandLine.setText("no output");

    // Se il comando è errato, stampa l'output
    CommandError error = cmd.newCommand(inputCommand.getText());
    if (error.getNumber() != 0) {
        outputCommandLine.setForeground(Color.RED);
        outputCommandLine.setText("Error n. " + error.getNumber() + ": " + error.getError());
        outputCommandLine.setForeground(Color.BLACK);
    }

    validate();
    repaint();

    printOnPanelLines();

    inputCommand.setText("");
    inputCommand.requestFocus();

    // Aggiorna le label della grandezza e colore
    sizeLabel.setText("Size: " + cmd.getPanel().getSizeLine());
    colorLabel.setBackground(cmd.getPanel().getColor());
}

private void printOnPanelLines() {
    linesList.removeAll();
    for (Line line : cmd.getPanel().getLines()) {
        linesList.add(line.toString());
    }
}

/* ... code ... */                        

private void inputCommandActionPerformed(java.awt.event.ActionEvent evt) {                                             
    sendTheCommand();
}      

这是PaintPanel类的paintComponent方法

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println("paintComponents()");
    g.setColor(Color.BLUE);
    g.fillRect(10, 10, 1100, 500);
}

我验证了通过System.out.println()方法正确调用了paintComponent。

0 个答案:

没有答案