﻿// MyGraph203.java                                              2003.05.19
//                                                              2003.05.04
//                                                      by M.Yanaka
//      大きさを指定して正方形または円を描く（大きなScrollbarを用いる）
//		（Layout マネージャを用いず、
//		コンポーネントの位置とサイズを指定して配置する方法）

import  java.awt.*;
import  java.awt.event.*;

public class MyGraph203 extends Frame implements ActionListener {

        int r;
        int sw;
        Scrollbar scrollbar;
        String[] str = {"正方形", "円", "終了"};
        Button[] bt = new Button[str.length];

        public MyGraph203(String title) {
                super(title);
                setSize(512,512);
                setBackground(Color.white);
                setLayout(null);

                sw = 0; // set rectangle
                r = 100;
                for (int i = 0; i < str.length; i++) {
                        bt[i] = new Button(str[i]);
			bt[i].setBounds(16 + i*64, 32, 56, 32);
                        bt[i].addActionListener(this);
                        add(bt[i]);
                }
                scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, r, 32, 0, 256);
                add(scrollbar);
		scrollbar.setBounds(256, 32, 256, 32);
                scrollbar.addAdjustmentListener(new ScrollbarListener());
                addWindowListener(new MyWindowAdapter());
                setVisible(true);
        }

        public static void main(String argv[]) {
                MyGraph203 app = new MyGraph203("円と正方形(MyGraph203)");
        }

        public void paint(Graphics g) {
                super.paint(g);
                if (sw == 0)
                        g.drawRect(256-r, 256-r, r*2+1, r*2+1);
                else
                        g.drawOval(256-r, 256-r, r*2+1, r*2+1);
        }

        public void actionPerformed(ActionEvent evt) {
                if (evt.getSource() == bt[0]) {
                        sw = 0;
                        repaint();
                } else if (evt.getSource() == bt[1]) {
                        sw = 1;
                        repaint();
                } else if (evt.getSource() == bt[2]) {
                        dispose();
                        System.exit(0);
                }
        }

        class MyWindowAdapter extends WindowAdapter {
                public void windowClosing(WindowEvent evt) {
                        dispose();
                }
                public void windowClosed(WindowEvent evt) {
                        System.exit(0);
                }
        }

        class ScrollbarListener implements AdjustmentListener {
                public void adjustmentValueChanged(AdjustmentEvent evt) {
                        if (evt.getSource() == scrollbar) {
                                r = scrollbar.getValue();
                                repaint();
                        }
                }
        }
}
