How to replace button shown on code with a filled circled.The circle (e.g. red) must move away from cursor just as same it was with button. Is it another possible way to implement this type of task?
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class NewSwingTemplate {
private Random d = new Random();
public NewSwingTemplate() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(createButton());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JButton createButton() {
final JButton button = new JButton("Button");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
int a = d.nextInt(200);
int b = d.nextInt(200);
button.setLocation(a, b);
}
});
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewSwingTemplate();
}
});
}
}