0

Hello im trying to draw lines inside a circle, i already created a circle but im stuck at the part when i need to draw the lines using mouse clicks. I need something like this:

https://i.stack.imgur.com/SXVuR.png

public class DrawCircle extends JFrame implements MouseListener {

private double radius = 0.0;
private double diameter ;

public DrawCircle() {
    addMouseListener(this);
    setSize(600,700);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D Circle = (Graphics2D)g;
    Circle.setStroke(new BasicStroke(10.f));
    Circle.setColor(Color.black);
    radius = 100;
    diameter = radius * 2 ;
    Circle.drawOval(200,300, (int) diameter, (int) diameter);
}
public static void main(String[] args) {
    new DrawCircle();
}


@Override
public void mouseClicked(MouseEvent e ) {
Graphics g = getGraphics();
if (e.getButton()==MouseEvent.BUTTON1) {
    g.setColor(Color.black);
    g.drawOval(e.getX(),e.getY(),5,5);
}

    }
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    Don't override paint() on a JFrame. Don't use getGraphics(). See: [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for custom painting basics. Also see [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) for two ways to do dynamic custom painting. – camickr Jan 02 '21 at 19:08
  • 1
    Does this help? https://stackoverflow.com/questions/40253767/java-swing-draw-lines-with-mouse-click-and-drag – Abra Jan 02 '21 at 19:19

0 Answers0