0

Here is the code for actually drawing the circle which doesn't work.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class TestCode extends JFrame {

private JPanel mousepanel;
private JLabel statusbar;

public TestCode() {
    super("title");
    mousepanel = new JPanel();
    mousepanel.setBackground(Color.WHITE);
    add(mousepanel, BorderLayout.CENTER);

    statusbar = new JLabel("default");
    add(statusbar, BorderLayout.SOUTH);

    Handlerclass handler = new Handlerclass();
    mousepanel.addMouseListener(handler);
    mousepanel.addMouseMotionListener(handler);

}

private class Handlerclass extends JFrame implements ActionListener, MouseListener, MouseMotionListener {

    public void paint(int a, int b) {
        Graphics g = this.getGraphics();
        int r = 10;
        g.drawOval(a, b, r, r);
        g.setColor(Color.BLACK);
        g.fillOval(a, b, r, r);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        int x,y;
        statusbar.setText(String.format("clicked at %d, %d", e.getX(), e.getY()));
        x=e.getX();
        y=e.getY();
        //paint(x, y);
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent ae) {

    }

    }

}

Hello I am currently trying to write a code where i click my mouse and it paints a circle. I don't know why it's not working. Here's my main method. When I run my code all it does is it shows the window and when i click nothing shows up except the coordinates on the bottom.

import java.awt.Graphics;

import javax.swing.JFrame;

public class Ahh {

public static void main(String[] args) {
    // TODO Auto-generated method stub
            TestCode qwerty = new TestCode();
            // qwerty.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            qwerty.setSize(300, 300);
            qwerty.setVisible(true);


    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • You're not doing Swing graphics correctly for one. Please read the standard tutorials to see what I mean: [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – Hovercraft Full Of Eels May 12 '18 at 02:47
  • I highly recommend that you start by having a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in Swing](http://www.oracle.com/technetwork/java/painting-140037.html) to get a better understanding of how painting works and how you should use it – MadProgrammer May 12 '18 at 02:48
  • Then create a class that overrides JPanel, draw in the paintComponent method, add a MouseListener to the same JPanel, have the MouseListener change fields in the JPanel, call repaint, and then draw the circle in paintComponent depending on the fields (x and y values of the mouse) states. – Hovercraft Full Of Eels May 12 '18 at 02:48

0 Answers0