0

So I am using Net Beans drag and drop feature to create my gui.

I dragged a Jpanel on the frame and generated a MouseClick Event.

private void gardenJPanelMouseClicked(java.awt.event.MouseEvent evt) {                                          

I am not sure what else to add. I have done this before making the gui manually but never with the gui generator.

 public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setColor(color);
        g.fillRect(0, 0, 1, 1);
    }

  //This does not work
}   
  • The techniques for doing this are no different for drag-and-drop components as for free-written components. In ***neither*** does a painting method go *inside* the mouse listener. Instead, you change the state of the program from within the mouse listener (change the state of key variables) and call repaint. The paintComponent method goes in the JPanel override proper and is prepended with an `@Override` annotation so you know that it is a proper override, and within it, you draw using the state fields that were changed by the mouse listener. – Hovercraft Full Of Eels Jul 14 '21 at 04:05
  • @HovercraftFullOfEels I am confused. How do I modify the JPanel???? Its drag and drop. – hecaviv546 Jul 14 '21 at 05:26
  • By overriding methods as per [this answer](https://stackoverflow.com/a/18416798/) – Hovercraft Full Of Eels Jul 14 '21 at 12:23
  • Read the section from the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html) for a complete working example. Start with the demo code and make modifications as required. – camickr Jul 14 '21 at 14:03

0 Answers0