You could do something like this
For the two buttons, just change the boolean make
. You'll see why later
boolean make = false;
makeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent){
make = true;
}
});
missButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent){
make = false;
}
});
Then you have your paintComponent
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (make){
drawOval(whataver are your point requirements)
} else {
// draw an x at whatever points
}
}
You can see from above that I made use of the made
variable
Now to get the location you can do something like this
Point p;
courtPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
p = e.getLocationOnScreen();
repaint();
}
});
You paint component will paint based off those point coordinates.
SO basically, what all the thing I listed above does is:
1) When the
makeButton
is pressed, it changes the setting tomade
so when the panel is painted, it gets painted with a circle and anX
when themissButtton
is pressed.2) I add a
mouseListener to the
courtPanelbecuase everytime the panel is clicked somewhere, that's the point on the floor where is it either painted an
xor a
circle`
If you want multiple X
and circle
painted you do something like this
private boolean make = false;
private HashMap<Boolean, Point> points = new HaspMap<>();
makeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent){
make = true;
}
});
missButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent){
make = false;
}
});
courtPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (make) {
points.put(true, e.getLocationOnScreen());
repaint();
} else {
points.put(false, e.getLocationOnScreen());
repaint();
}
});
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Map.Entry<Boolean, Point> entry : points.entrySet()) {
Boolean key = entry.getKey();
Point point = entry.getValue();
if (key) {
g.grawOval(point.getX(), point.getY(), 10, 20);
} esle {
g.drawLine(.., .., .., ..); //draws one half of `X`
g.drawLine(.., .., .., ..); //draws other half
}
}
}