java drawing a circle when mouse clicked

33,401

Solution 1

Change your mouseClick(...) to:

int x, y;

public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    repaint();
}

Override paint(...):

@Override
public void paint(Graphics g) {
    drawCircle(x, y);
}

Solution 2

When you call repaint(), the component gets painted again from scratch. You're circle is wiped away. You will want to override paintComponent(Graphics) which is called every time the component is painted.

Share:
33,401
hkguile
Author by

hkguile

Updated on July 09, 2022

Comments

  • hkguile
    hkguile almost 2 years

    i am writing a program that when the mouse is clicked, a circle will be drawn. The below code i've wrote so far.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.event.*;
    import java.awt.geom.*;
    
    public class test extends JFrame implements ActionListener, MouseListener {
        Shape circle = new Ellipse2D.Float(10, 10, 10, 10);
    
        public test () {
            setSize(250,150);
            addMouseListener(this);
        }
    
        public static void main(String[] args) {
            //TODO code application logic here
            java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                       test frame = new test();
                       frame.setVisible(true);
                  }
            });
        }
    
        public void actionPerformed(ActionEvent ae) {
    
        }
    
        public void drawCircle(int x, int y) {
            Graphics g = this.getGraphics();
            g.drawOval(x, y, x, y);
            g.setColor(Color.BLACK);
            g.fillOval(x, y, 2, 2);
        }
    
        public void mouseClicked(MouseEvent e) {
            drawCircle(e.getX(), e.getY());
            repaint();
        }
    
        public void mouseExited(MouseEvent e) {
    
        }
    
        public void mousePressed(MouseEvent e) {
    
        }
    
        public void mouseReleased(MouseEvent e) {
    
        }
    
        public void mouseEntered(MouseEvent e) {
    
        }
    }
    

    The code is a 400X400 jframe, when clicked open display a circle at a half seconds, The problem is that, when i release the mouse, the circle disappear. why?

    • kleopatra
      kleopatra over 12 years
      how comes this getGraphics mess is spreading around - a teacher gone wild?
  • Matthias Bruns
    Matthias Bruns over 12 years
    You could also save all drawn circles in an array or list and redraw them everytime
  • Adam Paynter
    Adam Paynter over 12 years
    @Traxdata: Yeah, I was thinking about mentioning that, but this question seemed like a homework question, so I left it to him. :)
  • Matthias Bruns
    Matthias Bruns over 12 years
    trapped again... :D Anyway overriding paintComponent(Graphics) would be too hard if it's homework
  • Andrew Thompson
    Andrew Thompson over 12 years
    Note that the OP is painting in a top level container. JFrame and other TLCs have no paintComponent(Graphics) method. OTOH it is best not to override paint(Graphics) in a TLC, but instead use a JComponent or JPanel - which has the paintComponent(Graphics) method.