在JPanel里面绘制图形,里面是JFrame(SWING / AWT)

时间:2017-01-10 09:48:05

标签: java swing graphics jframe jpanel

我有一个JFrame,里面有一个JPanel。 当我在JPanel中按下鼠标时,我可以绘制一个节点...该程序用于绘制图形。 我使用的是Netbeans编辑器,我不知道如何仅在JPanel中调整图片大小和打印图形。 当我调整JFrame的大小时,我会暂时调整JPanel(即完成)的大小。

public class PrincipalWindow extends javax.swing.JFrame {

private Controller controller;
/**
 * Creates new form PrincipalWindow
 */
public PrincipalWindow() {
    initComponents();       //Inicialitzacio de components "autogenerats" per mostrar l'interficie.
    myInitComponents();     //Inicialitzacio de components propis per mostrar l'interfície i per la logica de funcionament del programa.
}

/**
 * myInitComponents
 * Inicialitzacio de components o parametres propis per mostrar l'interficie.
 */
private void myInitComponents() {
    controller = new Controller();
    drawPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    //this.add(drawPanel, drawPanel.getComponents());
    //this.pack();
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    //Toolkit.getDefaultToolkit().setDynamicLayout(true); 
}

 private void drawPanelMousePressed(java.awt.event.MouseEvent evt) {                                       
    String nodeSelected;
    boolean nodeCreated;
    int nodeSelectedCounter = 0;
    int edgeCost = 0;

    if (evt.isMetaDown()) {

    }

    nodeSelected = controller.rightClickOnNode(drawPanel.getGraphics(), evt.getX(), evt.getY());
    if ("NO_NODE".equals(nodeSelected)) {
        nodeCreated = controller.createNode(drawPanel.getGraphics(), evt.getX(), evt.getY());
        if (!nodeCreated) {
            JOptionPane.showMessageDialog(null, "No es poden crear més vèrtexs!", "ERROR", JOptionPane.ERROR_MESSAGE);
        }

    }

    if ("CREATE_EDGE".equals(nodeSelected)) {
        edgeCost = setCostEdge();
        controller.createSimetricEdge(drawPanel.getGraphics(), edgeCost);
    }
}

@Override
public void paint(Graphics drawPanel) {
    super.paintComponents(drawPanel);
    List<GraphicNode> graphicNodeList = controller.getGraphicNodeList();
    List<GraphicEdge> graphicEdgeList = controller.getGraphicEdgeList();
    List<GraphicArrow> graphicArrowList = controller.getGraphicArrowList();


    Graphics2D drawPanel2D = (Graphics2D) drawPanel;
    drawPanel2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (GraphicArrow tmpGraphicArrow : graphicArrowList) {
        tmpGraphicArrow.drawArrow(drawPanel2D);
        //drawPanel.add(tmpGraphicArrow);
    }

    for (GraphicEdge tmpGraphicEdge : graphicEdgeList) {
        tmpGraphicEdge.drawEdge(drawPanel2D);
    }

    for (GraphicNode tmpGraphicNode : graphicNodeList) {
        tmpGraphicNode.drawNode(drawPanel2D);
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(PrincipalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(PrincipalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(PrincipalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PrincipalWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new PrincipalWindow().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel costPermAlgFORM;
private javax.swing.JLabel costRobFloAlgFORM;
private javax.swing.JLabel costRobFloAlgLABEL;
private javax.swing.JPanel drawPanel;
private javax.swing.JButton executeRobFloAlgorithmBTN;
private javax.swing.JButton executeTotalPermAlgorithmBTN;
private javax.swing.JMenuItem exitMENUITEM;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenuItem loadGraphMENUITEM;
private javax.swing.JMenuBar menuBAR;
private javax.swing.JMenu menuEditSLIDER;
private javax.swing.JMenu menuFileSLIDER;
private javax.swing.JMenuItem newGraphEDITITEM;
private javax.swing.JLabel permAlgCostLABEL;
private javax.swing.JMenuItem saveGraphMENUITEM;
private javax.swing.JPopupMenu.Separator separatorMENUITEM;
private javax.swing.JLabel timePermAlgFORM;
private javax.swing.JLabel timePermAlgLABEL;
private javax.swing.JLabel timeRobFloAlgFORM;
private javax.swing.JLabel timeRobFloAlgLABEL;
// End of variables declaration                   

}

0 个答案:

没有答案