/*
 * CircleAnimator.java
 *
 * Copyright 2013 Ajay Bhatia <ajay@dumb-box>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CircleAnimator extends JFrame implements ActionListener {
    private CircleLabel circle;
    private JButton btnShrink;
    private JButton btnEnlarge;
    private JPanel pnlBottom;
  
    CircleAnimator() {
        circle = new CircleLabel();
        btnEnlarge = new JButton("Enlarge");
        btnShrink = new JButton("Shrink");
        pnlBottom = new JPanel();
      
        pnlBottom.setLayout(new FlowLayout());
        pnlBottom.add(btnShrink);
        pnlBottom.add(btnEnlarge);

        setLayout(new BorderLayout());
      
        add(circle, BorderLayout.CENTER);
        add(pnlBottom, BorderLayout.SOUTH);
  
        btnEnlarge.addActionListener(this);
        btnShrink.addActionListener(this);
    }
  
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnEnlarge) {
            circle.enlarge();
        } else {
            circle.shrink();
        }
    }
  
    class CircleLabel extends JLabel {
        private int radius = 25;
      
        public void enlarge() {
            radius += 5;
            repaint();
        }

        public void shrink() {
            radius -= 5;
            repaint();
        }
      
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
          
            g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius, 2 * radius, 2 * radius);
        }
    }
  
    public static void main (String args[]) {
        CircleAnimator frame = new CircleAnimator();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);  
    }
  
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments