import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JComponent;
import javax.swing.JFrame;

class StudentWorkSolution {
	Image img;
	Color backgroundColor;
	ArrayList<cball> balls;

	public void initialize() {
		balls = new ArrayList<cball>();
	}
	
	public void shoot(){
		cball c = new cball(260, 335, 5, 0);
		balls.add(c);
	}
	
	public void update() {
		// cannon physics go here
		for(int i = 0; i < balls.size(); i++){
			cball c = balls.get(i);
			c.x += c.vx;
			c.y += c.vy;
			if(c.x >= 500 || c.x < -50 || c.y >= 500 || c.y < -50){
				balls.remove(i);
			}
		}
	}
	
	public void draw(Graphics g) {
		g.setColor(this.backgroundColor);
		g.fillRect(0, 0, 500, 500);
		
		try
        {
            img = ImageIO.read(new File("cannon.jpg"));
            g.drawImage(img, 0, 300, null);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
		g.setColor(Color.BLACK);
		for(cball c : balls){
			g.fillOval(c.x, c.y, c.sizex, c.sizey);
		}
	}

	public StudentWorkSolution() {
		backgroundColor = Color.white;
	}
	
	void playSound(String soundFile) throws MalformedURLException, UnsupportedAudioFileException, IOException, LineUnavailableException {
	    File f = new File("./" + soundFile);
	    AudioInputStream audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());  
	    Clip clip = AudioSystem.getClip();
	    clip.open(audioIn);
	    clip.start();
	}

}

public class Cannon {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Homework Problem 1");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);
		ContentViewSolution view = new ContentViewSolution();
		frame.add(view);
		frame.addKeyListener(view);
		frame.setResizable(false);
		frame.setVisible(true);
	}
}

class ContentViewSolution extends JComponent implements KeyListener, Runnable {

	StudentWorkSolution student;
	boolean[] keyMap = new boolean[4];

	public ContentViewSolution() {
		student = new StudentWorkSolution();
		student.initialize();
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		while (true) {
			student.update();
			repaint();
			try {
				Thread.sleep(1000/60);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		}

	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_SPACE) {
			student.shoot();
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	public void paintComponent(Graphics g) {
		student.draw(g);
	}

}

class cball {
	int x, y, sizex, sizey, vx, vy, ax, ay;
	public cball(int x, int y, int vx, int vy){
		this.x = x;
		this.y = y;
		sizex = 50;
		sizey = 50;
		this.vx = vx;
		this.vy = vy;
		ax = 0;
		ay = 0;
	}
}
