// Student information for assignemnt.
// Student 1 Name:
// Student 1 UTEID:


// Slip days used:

// replace <Student1> with your name
// stating this is your own work.
// On my honor <Student1>  this
// assignmentis my own work.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class LilacChaserMain {
    public static void main(String[] args) { 
        LilacChaserFrame f = new LilacChaserFrame(); 
        f.start(); 
    } 
}

class LilacChaserFrame extends JFrame {
    
    private static final int TIMER_DEFAULT_DELAY = 100;
    private static final int MAX_FRAMES_PER_SECOND = 25;
    
    private LilacChaserPanel panel;
    private Timer timer;
    
    public LilacChaserFrame(){
        super("Lilac Chaser");
        setSize(500, 500);
        setLocation(100, 100);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new LilacChaserPanel();
        add(panel);
        add(new TitlePanel(), "North");
        addTimer();
        addSlider();
        
        pack();
    }
    
    // post: create and add a slider to control time delay
    // based on code from Stuart Reges at UWCS
    private void addSlider() {
        // slider to control frames per second
        final JSlider slider = new JSlider(1, MAX_FRAMES_PER_SECOND);
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                timer.setDelay(1000 / slider.getValue());
            } });
        
        //  start at 10 frames per second 
        slider.setValue(10);
        
        //  create a panel for the slider and its slow/fast labels 
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(500, 30));
        p.add(new JLabel("slow"));
        p.add(slider);
        p.add(new JLabel("fast"));
        add(p, "South");
    }
    
    
    // post: Timer created
    private void addTimer(){
        timer = new Timer(TIMER_DEFAULT_DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // timer hs gone off, repaint panel
                panel.repaint();
            } }); 
    }
    
    // pre: all componenets initialized
    // post: frame set to visible
    public void start(){
        setVisible(true);
        timer.start();
    }
}

class LilacChaserPanel extends JPanel {
    
    public static final int PREFERRED_DIMENSION = 400;
    // CS324e students, add constants and instance variables here
    

    
    public LilacChaserPanel(){
         setPreferredSize(new Dimension(PREFERRED_DIMENSION, PREFERRED_DIMENSION));
         // CS324e students, add code as necessary
    }
    
    // CS324e students, add methods as necessary to create a clean
    // modular solution

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        // CS324e students, add code as necessary
    }
}

class TitlePanel extends JPanel{
    
    private static final int PREFERRED_HEIGHT = 50;
    // CS324e students, add constants and instance variables here
    
    private Color fontColor;
    private Font f;
    
    public TitlePanel(){
        setPreferredSize(new Dimension(LilacChaserPanel.PREFERRED_DIMENSION, PREFERRED_HEIGHT));
        setBackground(Color.WHITE); 
        // CS324e students, add code as necessary
    }
    
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        // CS324e students, add code as necessary
    }
}