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);
}
}