/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package source;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Radoslaf
 */
public class ImageArrowProccesing {
// Obrázky růžice

    private Image arrow_0;
    private Image arrow_45;
    private Image arrow_90;
    private Image arrow_135;
    private Image arrow_180;
    private Image arrow_225;
    private Image arrow_270;
    private Image arrow_315;

    public Image getArrow_0() {
        if (arrow_0 == null) {
            try {
                arrow_0 = Image.createImage("/img/arrow/arrow_0.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_0;
    }

    public Image getArrow_45() {
        if (arrow_45 == null) {
            try {
                arrow_45 = Image.createImage("/img/arrow/arrow_45.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_45;
    }

    public Image getArrow_90() {
        if (arrow_90 == null) {
            try {
                arrow_90 = Image.createImage("/img/arrow/arrow_90.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_90;
    }

    public Image getArrow_135() {
        if (arrow_135 == null) {
            try {
                arrow_135 = Image.createImage("/img/arrow/arrow_135.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_135;
    }

    public Image getArrow_180() {
        if (arrow_180 == null) {
            try {
                arrow_180 = Image.createImage("/img/arrow/arrow_180.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_180;
    }

    public Image getArrow_225() {
        if (arrow_225 == null) {
            try {
                arrow_225 = Image.createImage("/img/arrow/arrow_225.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_225;
    }

    public Image getArrow_270() {
        if (arrow_270 == null) {
            try {
                arrow_270 = Image.createImage("/img/arrow/arrow_270.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_270;
    }

    public Image getArrow_315() {
        if (arrow_315 == null) {
            try {
                arrow_315 = Image.createImage("/img/arrow/arrow_315.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return arrow_315;
    }
    
    
    private Navigation navigation = null;
    private Calculation calc = null;
    public Image aktarrow = null;
    public short aktangle = 0;

    public ImageArrowProccesing(Navigation navigation, Calculation calc) {
        this.navigation = navigation;
        this.calc = calc;
    }

    
    public Image retrieveImage() {
        aktangle = calc.angle();
        if (aktangle < 22) {
            this.aktarrow = this.getArrow_0();
        }
        if (aktangle >= 23 && aktangle < 67) {
            this.aktarrow = this.getArrow_45();
        }
        if (aktangle >= 68 && aktangle < 112) {
            this.aktarrow = this.getArrow_90();
        }
        if (aktangle >= 113 && aktangle < 157) {
            this.aktarrow = this.getArrow_135();
        }
        if (aktangle >= 158 && aktangle < 202) {
            this.aktarrow = this.getArrow_180();
        }
        if (aktangle >= 203 && aktangle < 247) {
            this.aktarrow = this.getArrow_225();
        }
        if (aktangle >= 248 && aktangle < 292) {
            this.aktarrow = this.getArrow_270();
        }
        if (aktangle >= 293 && aktangle < 337) {
            this.aktarrow = this.getArrow_315();
        }
        if (aktangle >= 338) {
            this.aktarrow = this.getArrow_0();
        }
        this.aktarrow = resizeImage(aktarrow);
        return this.aktarrow;
    }

    private Image resizeImage(Image src) {
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        Image tmp = Image.createImage(this.navigation.getWidth(), srcHeight);
        Graphics g = tmp.getGraphics();
        int ratio = (srcWidth << 16) / this.navigation.getWidth();
        int pos = ratio / 2;

        //Horizontal Resize

        for (int x = 0; x < this.navigation.getWidth(); x++) {
            g.setClip(x, 0, 1, srcHeight);
            g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
            pos += ratio;
        }

        Image resizedImage = Image.createImage(this.navigation.getWidth(), this.navigation.getWidth());
        g = resizedImage.getGraphics();
        ratio = (srcHeight << 16) / this.navigation.getWidth();
        pos = ratio / 2;

        //Vertical resize

        for (int y = 0; y < this.navigation.getWidth(); y++) {
            g.setClip(0, y, this.navigation.getWidth(), 1);
            g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
            pos += ratio;
        }


        return resizedImage;


    }//resize image
}