E9

import java.awt.Color;

import sk.upjs.jpaz2.Pane;

public class Tvar {

        protected double x;
        protected double y;
        protected Color farba;

        public Tvar(double x, double y, Color farba) {
                super();
                this.x = x;
                this.y = y;
                this.farba = farba;
        }

        public double getX() {
                return x;
        }

        public void setX(double x) {
                this.x = x;
        }

        public double getY() {
                return y;
        }

        public void setY(double y) {
                this.y = y;
        }

        public Color getFarba() {
                return farba;
        }

        public void setFarba(Color farba) {
                this.farba = farba;
        }

        public void vykresliSa(Pane plocha) {
                System.err.println("Tvar sa nedá vykresliť");
        }

        public boolean jeVnutornyBod(double x, double y) {
                return (this.x == x && this.y == y);
        }

        public void posun(double dx, double dy) {
                this.x += dx;
                this.y += dy;
        }

        public String popisDoSuboru() {
                return "T " + this.x + " " + this.y;
        }
}
import java.awt.Color;

import sk.upjs.jpaz2.Pane;
import sk.upjs.jpaz2.Turtle;


public class Kruh extends Tvar {

        private double priemer;

        public Kruh(double x, double y, Color farba, double priemer) {
                super(x, y, farba);
                this.priemer = priemer;
        }

        public double getPriemer() {
                return priemer;
        }

        public void setPriemer(double priemer) {
                this.priemer = priemer;
        }

        @Override
        public void vykresliSa(Pane plocha) {
                Turtle k = new Turtle();
                plocha.add(k);

                k.setPosition(x, y);
                k.setFillColor(farba);
                k.dot(priemer);

                plocha.remove(k);
        }

        @Override
        public boolean jeVnutornyBod(double x, double y) {
                if (priemer/2 >
                        Math.sqrt((this.x - x)*(this.x - x) + (this.y - y)*(this.y - y)))
                        return true;
                return false;
        }

        public String popisDoSuboru() {
                return "K " + this.x + " " + this.y + " " + this.farba.getRGB() + " " + this.priemer;
        }
}
import java.awt.Color;

import sk.upjs.jpaz2.Pane;
import sk.upjs.jpaz2.Turtle;

public class Obdlznik extends Tvar {

        private double a;
        private double b;

        public Obdlznik(double x, double y, Color farba, double a, double b) {
                super(x, y, farba);
                this.a = a;
                this.b = b;
        }

        public double getA() {
                return a;
        }

        public void setA(double a) {
                this.a = a;
        }

        public double getB() {
                return b;
        }

        public void setB(double b) {
                this.b = b;
        }

        @Override
        public void vykresliSa(Pane plocha) {
                Turtle k = new Turtle();
                plocha.add(k);

                k.setPosition(x - a/2, y + b/2);
                k.setFillColor(farba);
                k.openPolygon();
                for (int i = 0; i < 2; i++) {
                        k.step(b);
                        k.turn(90);
                        k.step(a);
                        k.turn(90);
                }
                k.closePolygon();

                plocha.remove(k);
        }

        @Override
        public boolean jeVnutornyBod(double x, double y) {
                if (Math.abs(this.x - x) > a/2
             || Math.abs(this.y - y) > b/2)
                        return false;
                return true;
        }

        public String popisDoSuboru() {
                return "O " + this.x + " " + this.y + " " + this.farba.getRGB() + " " + this.a + " " + this.b;
        }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import sk.upjs.jpaz2.Pane;

public class ZoznamTvarov {

        private Tvar[] tvary = new Tvar[0];

        public void pridajTvar(Tvar t) {
                Tvar[] noveTvary = new Tvar[tvary.length + 1];
                System.arraycopy(tvary, 0, noveTvary, 0, tvary.length);
                noveTvary[tvary.length] = t;
                tvary = noveTvary;
        }

        public void nakresliVsetky(Pane plocha) {
                for (int i = 0; i < tvary.length; i++) {
                        tvary[i].vykresliSa(plocha);
                }
        }

        public void posun(double dx, double dy) {
                for (int i = 0; i < tvary.length; i++) {
                        tvary[i].posun(dx, dy);
                }
        }

        public Tvar bodTvaru(double x, double y) {
                for (int i = 0; i < tvary.length; i++) {
                        if (tvary[i].jeVnutornyBod(x, y))
                                return tvary[i];
                }
                return null;
        }

        public void odstranTvar(Tvar t) {
                int index = -1;
                for (int i = 0; i < tvary.length; i++) {
                        if (tvary[i] == t)
                                index = i;
                }

                if (index < 0)
                        return;

                Tvar[] novyZoznam = new Tvar[tvary.length - 1];
                System.arraycopy(tvary, 0, novyZoznam, 0, index);
                System.arraycopy(tvary, index + 1, novyZoznam, index, tvary.length - index - 1);
                tvary = novyZoznam;
        }

        public void ulozDoSuboru(String nazovSuboru) {
                PrintWriter pw = null;
                try {
                        pw = new PrintWriter(new File(nazovSuboru));
                        for (int i = 0; i < tvary.length; i++) {
                                pw.write(tvary[i].popisDoSuboru() + "\n");
                        }
                } catch (FileNotFoundException e) {
                        System.err.println("Chyba pri zapise");
                } finally {
                        if (pw != null)
                                pw.close();
                }
        }
}
import java.awt.Color;
import java.awt.event.MouseEvent;

import sk.upjs.jpaz2.WinPane;

public class PlochaTvarov extends WinPane {

        private ZoznamTvarov tvary;

        private double dragX;
        private double dragY;
        private boolean tahanie = false;

        public PlochaTvarov() {
                this.tvary = new ZoznamTvarov();
        }

        @Override
        protected void onMouseClicked(int x, int y, MouseEvent detail) {
                if (detail.getButton() == MouseEvent.BUTTON1) {
                                Color farba = new Color((int)(Math.random()*256),
                                                                                (int)(Math.random()*256),
                                                                                (int)(Math.random()*256));

                        if (detail.isAltDown()) {
                                this.tvary.pridajTvar(new Kruh(x,y,farba,Math.random()*100));
                        } else {
                                this.tvary.pridajTvar(new Obdlznik(x, y, farba,
                                                Math.random()*100, Math.random()*100));
                        }
                }

                if (detail.getButton() == MouseEvent.BUTTON3) {
                        Tvar naOdstranenie = this.tvary.bodTvaru(x, y);
                        if (naOdstranenie != null) {
                                this.tvary.odstranTvar(naOdstranenie);
                        }
                }

                this.clear();
                this.tvary.nakresliVsetky(this);
        }


        @Override
        protected void onMousePressed(int x, int y, MouseEvent detail) {
                if (detail.getButton() == MouseEvent.BUTTON1) {
                        this.tahanie = true;
                        this.dragX = x;
                        this.dragY = y;
                }
        }

        @Override
        protected void onMouseDragged(int x, int y, MouseEvent detail) {
                if (this.tahanie) {
                        tvary.posun(x - this.dragX, y - this.dragY);
                        this.dragX = x;
                        this.dragY = y;
                        this.clear();
                        this.tvary.nakresliVsetky(this);
                }
        }

        @Override
        protected void onMouseReleased(int x, int y, MouseEvent detail) {
                if (detail.getButton() == MouseEvent.BUTTON1) {
                        this.tahanie = false;
                }
        }
}
import java.awt.Color;

import sk.upjs.jpaz2.WinPane;


public class Spustac {

        /**
         * @param args
         */

        public static void main(String[] args) {
                WinPane plocha = new WinPane();
                Tvar t = new Tvar(14, 25, Color.black);
                Kruh k = new Kruh(50, 50, Color.red, 30);
                Obdlznik o = new Obdlznik(200, 150, Color.blue, 100, 50);

                t.vykresliSa(plocha);
                k.vykresliSa(plocha);
                o.vykresliSa(plocha);

                System.out.println(t.jeVnutornyBod(14, 25));
                System.out.println(t.jeVnutornyBod(15, 25));
                System.out.println(k.jeVnutornyBod(31, 51));
                System.out.println(k.jeVnutornyBod(0, 0));
                System.out.println(o.jeVnutornyBod(249, 174));
                System.out.println(o.jeVnutornyBod(249, 176));

                ZoznamTvarov z = new ZoznamTvarov();
                z.pridajTvar(t);
                z.pridajTvar(k);
                z.pridajTvar(o);
                z.ulozDoSuboru("tvary.txt");

//              PlochaTvarov plocha = new PlochaTvarov();
        }

}