수업 용 신호등을 만들려고합니다. 우리는 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){}
}
각 원이 동일한 색상으로 변경되지 않습니까? – Robert
예, 각 그리기의 컨텍스트 색상을 변경해야합니다. – colbadhombre
아, 그래, 나중에있어 교통 신호등처럼 보이게하려면 어떻게해야합니까? – Robert