0

I'm trying to create a tool similar to Visual Paradigm. This is what I did so far Screenshot of the tool

For the next step, I want a circle to be drawn on the white plain panel when the "Host" button is clicked. However, it's not working.

This method is for the Host button action:

  private void hostButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    System.out.println("Host button clicked.");
    final JPanel center = new JPanel();
    center.setLayout(null);
    JPanel drawingPanel = new CircleDraw();
    drawingPanel.setLocation(100,100);
    drawingPanel.setSize(100,100);
    center.add(drawingPanel);
    center.repaint();
}       

and this is the CircleDraw Class

import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;

public class CircleDraw extends JPanel {
Ellipse2D.Double circle;

public CircleDraw() {
    circle = new Ellipse2D.Double(100, 100, 100, 100);
    setOpaque(false);
    System.out.println("I'm inside CIRCLEDRAW constructor");
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D ga = (Graphics2D) g;
 //   ga.draw(circle);
    ga.setPaint(Color.green);
    ga.fill(circle);
    ga.setPaint(Color.red);
    System.out.println("I'm inside method PAINT");
    }
}

However, it never prints "I'm inside method PAINT". I tried following the steps provided here, but I still can't get it to work. Any help would be appreciated.

Alaa
  • 19
  • 4
  • You should override `paintComponent(Graphics g)` for a `JPanel`. [Difference between paint, paintComponent and paintComponents in Swing](https://stackoverflow.com/questions/9389187/difference-between-paint-paintcomponent-and-paintcomponents-in-swing) – d.j.brown Oct 29 '17 at 12:52
  • 1) You place your drawingPanel CircleDraw object within the center JPanel, but what do you add the center JPanel to? Nothing. If it is not added to the GUI, how will it ever be displayed? 2) avoid null layouts. 3) You're much better off giving CircleDraw the ability to draw or not draw the circle by changing a boolean field that it contains. This way you can add your CircleDraw object to the GUI on creation and then in your action listener, simply change the state of the boolean and call repaint. 4) yes, draw within paintComponent, not paint. – Hovercraft Full Of Eels Oct 29 '17 at 12:59
  • 5) [Many similar questions](https://www.google.com/search?q=siite%3Astackoverflow.com+java+swing+draw+circle+on+button+press) can be easily found – Hovercraft Full Of Eels Oct 29 '17 at 13:00

0 Answers0