Я пытаюсь создать светофор для класса. Нам не разрешено использовать IDE, так что это делается в текстовой панели. Как мне получить доступ к свойствам каждого отдельного круга, чтобы я мог сделать один красный, зеленый, желтый?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Lab4Panel extends JPanel implements MouseListener {
public Lab4Panel(){
}
JRadioButton red = new JRadioButton("Red", true);
JRadioButton yellow = new JRadioButton("Yellow");
JRadioButton green = new JRadioButton("Green");
int height, width;
int radius = 5;
int x = -1;
int y = -1;
protected void paintComponent(Graphics g){
if (x<0 || y<0) {
x = getWidth() / 2 - radius;
y = getHeight() / 2 - radius;
}
super.paintComponent(g);
g.drawRect(x - 5,y - 90, 40, 120);
g.drawOval(x,y - 80, 4 * radius, 4 * radius);
g.drawOval(x,y - 40, 4 * radius, 4 * radius);
g.drawOval(x,y, 4 * radius, 4 * radius);
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == red){
}
else if (e.getSource() == yellow){
}
else if (e.getSource() == green){
}
}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}