import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame{
Color [] colors= {Color.RED,Color.YELLOW,Color.MAGENTA,Color.BLUE};
JPanel text = new JPanel();
ImagePanel image= new ImagePanel();
JTextField [] TextT= new JTextField[4];
JLabel [] percentI= new JLabel[4];
JLabel [] percentI1= new JLabel[4];
JLabel [] percentI2= new JLabel[4];
int i;
public Main() {
setTitle("파이 차트 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c= getContentPane();
c.setLayout(null);
c.add(text);
text.setBackground(Color.GRAY);
text.setSize(500,30);
text.setLocation(0,0);
text.setLayout(new FlowLayout());
c.add(image);
image.setBackground(Color.LIGHT_GRAY);
image.setSize(500,470);
image.setLocation(0,30);
image.setLayout(new FlowLayout());
String [] names= {"apple"," cherry"," strawberry"," prune"};
JLabel [] LabelT= new JLabel[4];
for( i=0;i<4;i++) {
LabelT[i] = new JLabel(names[i]);
percentI[i] = new JLabel(names[i]);
percentI[i].setForeground(colors[i]);
percentI1[i]= new JLabel();
percentI1[i].setForeground(colors[i]);
percentI2[i]= new JLabel("%");
percentI2[i].setForeground(colors[i]);
TextT[i] = new JTextField(3);
TextT[i].addActionListener(new MyActionListener());
text.add(LabelT[i]);
text.add(TextT[i]);
image.add(percentI[i]);
image.add(percentI1[i]);
image.add(percentI2[i]);
}
setSize(500,500);
setVisible(true);
}
String t;
int t1,c=0,sum=0,p=0;
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
c++;
if(c>3) {
c=0;
sum=0;
}
t= e.getActionCommand();
t1=Integer.parseInt(t);
sum+=t1;
p=100*t1/sum;
percentI1[c-1].setText(Integer.toString(p));
image.repaint();
}
}
class ImagePanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
}
}
public static void main(String[] args) {
new Main();
}
}