0

I have a JFrame that have inside a JPanel. When I press the mouse in the JPanel I can draw a Node... The program is used to draw a Graph. I'm using Netbeans editor, and I don't know how to resize and print the graphics only inside the JPanel. When I resize the JFrame I would resize too the JPanel (that is accomplish) for the moment.

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                   

}

  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks); see also this [example](http://stackoverflow.com/a/5312702/230513). – trashgod Jan 10 '17 at 10:04
  • If I change the name of the method in the code to "paintComponent()" I can't Override it... :( – lesoldado Cric Jan 10 '17 at 10:19
  • The example I cited shows how. – trashgod Jan 10 '17 at 10:30
  • ""Swing programs should override paintComponent() instead of overriding paint()."": yeah the usual crap – gpasch Jan 10 '17 at 22:46

0 Answers0