import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;

// can't redraw and pick apples at same time.
public class DrawTree extends JPanel {
    //	 draws trees and apples.  whenever a button is pushed, call repaint	
    private Graphics g;
    private int appleWidth;
    private int middleX ;
    private int treeRadius;
    private int treeX ;
    private int treeY;
    private int middleY ;
    private AppleTree appleTree;
		 
    public DrawTree(AppleTree appleTree) {
	super(); // create a JPanel
	this.appleTree = appleTree;
	repaint();
    }
		 
    public int getTreeY() {
	return treeY;
    }
		 
    public int getTreeX() {
	return treeX;
    }
		 
    public int getCenterX() {
	return treeRadius+treeX-appleWidth/2;
    }
		 
    public int getCenterY() {
	return treeRadius+treeY-appleWidth/2;
    }
		 
    public int getAppleWidth() {
	return appleWidth;
    }
		 
    public int getRadius() {
	return treeRadius;
    }
		 
		 
    protected void paintComponent(Graphics g) {
	super.paintComponent(g);
			 
	//copy necessary data to fields
	this.g = g;
	appleWidth = getWidth()/20;
	middleX = getWidth()/2;
	treeRadius = getWidth()/3;
	treeX = middleX-treeRadius;
	treeY = appleWidth;
	middleY = getHeight()/2;
			
	Color brown = new Color(128,64,0); // because no constant for brown
	//draw trunk
	g.setColor(brown);
	g.fillRect(middleX-appleWidth, middleY, appleWidth,
		   middleY);
	// draw tree
	g.setColor(Color.GREEN);
	g.fillOval(treeX, treeY, treeRadius*2, 
		   treeRadius*2);
	// System.out.println("" + applePlaces.size() + " apples on tree");
	// draw any apples
	g.setColor(Color.RED);
            
	for (int i=0;i<appleTree.getTreeSize();i++) {
	    Point p=appleTree.getApple(i);
	    g.fillOval(p.x, p.y, appleWidth, appleWidth );
	}
    }
            
    public void erase(Point p) {
	g.setColor(getBackground());
	g.fillOval(p.x, p.y, appleWidth, appleWidth);
	repaint();
    }
}
