How to draw a circle around current position?

david :

I am trying to build a program that allows you to draw a Graph. I am currently trying to add a function to add a node to the graph. My idea is that the user can click a button and then click somewhere on the window to add a node. I face the two following problems now:

  1. drawCenteredCircle method needs me to pass a Graphics object, but which one? Since it is an abstract class I cannot simply declare a new instance, so what can I pass?
  2. Currently the position of the center of the circle will be the one where the button was clicked. How can I solve this? I want that AFTER the button was clicked, the NEXT click will add a node. Basically I need a function that waits for another user input and then add this node.

    public class Main extends JFrame {

    int width = 500;
    int height = 500;
    JLabel label;
    JPanel panel;
    JButton addNodeButton;
    
    public Main() {
        setSize(width, height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        panel = new JPanel();
        label = new JLabel();
        addNodeButton = new JButton("Add Node");
        addNodeButton.addActionListener((new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Point p = MouseInfo.getPointerInfo().getLocation();
                drawCenteredCircle(g, p.x, p.y, 3);
            }
    
        }));
        panel.add(addNodeButton);
        this.add(panel);
        setVisible(true);
    
    }
    
    public static void main(String a[]) {
        new Main();
    }
    
    public void drawCenteredCircle(Graphics2D g, int x, int y, int r) {
          x = x-(r/2);
          y = y-(r/2);
          g.fillOval(x,y,r,r);
        }
    

    }

WJS :

Try this. The explanations will be forthcoming when you check out the tutorials on painting or consult the Java API. I used a mouseListener instead of a button. It draws a circle around where you click the button.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JPanel {
    int width = 500;
    int height = 500;
    JFrame frame = new JFrame();
    int x;
    int y; 
    int diameter = 60;
    List<Point> points = new ArrayList<>();
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main());
    }

    public Main() {
        setPreferredSize(new Dimension(width, height));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.white);
        addMouseListener(new MyMouseListener());
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.blue);
        g2d.setStroke(new BasicStroke(3));
        for(Point p : points) {
            g2d.drawOval(p.x,p.y, diameter,diameter);
        }

    }

    private class MyMouseListener extends MouseAdapter {
        public void mouseClicked(MouseEvent me) {
            x = me.getX()-diameter/2;
            y = me.getY()-diameter/2;
            points.add(new Point(x,y));
            repaint();
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276910&siteId=1