Package fmg.fmg8.pfeilGUI

Source Code of fmg.fmg8.pfeilGUI.PfeilController

/*
* Datei: SteuerFenster.java
* Autor(en):        Lukas K�nig
* Java-Version:     6.0
* Erstellt (vor): 19.12.2008
*
* (c) Lukas K�nig, die Datei unterliegt der LGPL
* -> http://www.gnu.de/lgpl-ger.html
*/

package fmg.fmg8.pfeilGUI;

import fmg.fmg8.graphVis.fensterDialoge.AllgemeinerDialog;
import fmg.fmg8.graphVis.zeichenModi.AusgMerkm;
import fmg.fmg8.graphVis.zeichenModi.PfeilMaster;
import fmg.fmg8.graphVis.zeichenModi.Pfeilspitze;
import fmg.fmg8.pfeilGUI.funktionen2D.Funktion;
import fmg.fmg8.sonstiges.DateiFilter;
import fmg.fmg8.statistik.Parametersatz;

import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Choice;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

/**
* Klasse zur Repr�sentation eines Steuerfensters.
*
* @author Lukas K�nig
*/
public class PfeilController extends Frame implements ActionListener,
        ItemListener, KeyListener {

    /**
     * Die Versions-ID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Die Segmente bei Strichelung des Pfeils.
     */
//    private AusgMerkm[] strichelSegmente;
   
    /**
     * Die Parameter f�r den Lauf.
     */
    private Parametersatz pars;

    /**
     * Die Zeichenfenster.
     */
    private ArrayList<ZeichFen> graphen;

    /**
     * Der aktuell ausgew�hlte Graph.
     */
    private ZeichFen aktGrph;

    /**
     * Eine Textfeld, in dem der Zustandsname steht.
     */
    private JTextField sonst;

    /**
     * Eine Textfeld, in dem die Aktion steht.
     */
    private JTextField gestricheltFelt;

    /**
     * Eine Textfeld, in dem der Parameter steht.
     */
    private JTextField farbeA2;

    /**
     * Die Auswahl der Graphen als Auswahlfeld.
     */
    private Choice grAuswahl;

    /**
     * Die Gestrichelten Farben.
     */
    private JComboBox strichelungen;
   
    /**
     * Graphfensterz�hler.
     */
    private int graphZaehl;

    /**
     * Aktuelle Ausgabemerkmale.
     */
    private AusgMerkm aktAusg;

    /**
     * Die Pfeilanf�nge.
     */
    private Choice pfeilAnfang;

    /**
     * Die Pfeilenden.
     */
    private Choice pfeilEnde;

    /**
     * Multiplikativer Faktor f�r die Gr��e des Pfeilanfangs.
     */
    private JTextField pfeilAnfFakt;

    /**
     * Multiplikativer Faktor f�r die Gr��e des Pfeilendes.
     */
    private JTextField pfeilEndFakt;

    /**
     * Z�hler der neu definierten Spitzen.
     */
    private int numNeueSpitzen;
   
    /**
     * Der Konstruktor.
     *
     * @param parameter
     *            Der Parametersatz f�r den Lauf.
     */
    public PfeilController(final Parametersatz parameter) {
        super(Konstanten.PROG_NAME_PFEILE + " V "
                + Konstanten.PROG_VERSION_PFEILE);
        this.pars = parameter;
        this.aktAusg = null;
        this.reset();
    }

    /**
     * Setzt das Steuerfenster zur�ck.
     */
    public void reset() {
        this.removeAll();

//        this.strichelSegmente = new AusgMerkm[0];
        this.numNeueSpitzen = 0;
        this.graphen = new ArrayList<ZeichFen>();
        this.aktGrph = null;
        this.graphZaehl = 0;

        this.setLocation(800, 0);
        this.setSize(300, 700);
        this.setzeSteuerelemente();
        this.ladeGraphen();
        this.setVisible(true);
        for (Pfeilspitze s : PfeilMaster.getSpitzen()) {
            this.pfeilAnfang.add(s.toString());
        }
        for (Pfeilspitze s : PfeilMaster.getSpitzen()) {
            this.pfeilEnde.add(s.toString());
        }
        this.pfeilAnfang.select(1);
        this.itemStateChanged(null);
    }

    /**
     * L�dt gespeicherte Graphen aus dem im Parameterobjekt angegebenen
     * Verzeichnis.
     */
    private void ladeGraphen() {
        File verz = new File(this.pars.getStdPfad());
        String endung = Konstanten.PFEIL_ENDUNG;
        String[] gespGr = verz.list(new DateiFilter(endung));
        String graphName;
        ZeichFen graphFenster;

        if (gespGr != null) {
            for (int i = 0; i < gespGr.length; i++) {
                graphName = gespGr[i].substring(0, gespGr[i].length()
                        - endung.length() - 1);

                graphFenster = new ZeichFen(Konstanten.ZEICHNUNG_NAME + i,
                        Konstanten.ZEICHNUNG_NAME + i, this.pars, i, this);

                this.grAuswahl.add(graphName);
                this.graphen.add(graphFenster);
                this.graphZaehl++;
            }
        }

        if (this.graphen.size() <= 0 || this.grAuswahl.getItemCount() <= 0) {

            graphFenster = new ZeichFen(Konstanten.ZEICHNUNG_NAME + 0,
                    Konstanten.ZEICHNUNG_NAME + 0, this.pars, 0, this);

            this.grAuswahl.add(Konstanten.ZEICHNUNG_NAME + 0);
            this.graphen.add(graphFenster);
            this.graphZaehl++;
        }

        this.waehleGraph(0);
    }

    /**
     * W�hlt einen Graphen f�r die Darstellung aus.
     *
     * @param nummer
     *            Die Nummer des zu w�hlenden Graphen.
     */
    private void waehleGraph(final int nummer) {
        int altX = 0;
        int altY = 0;

        if (this.aktGrph != null) {
            altX = this.aktGrph.getLocation().x;
            altY = this.aktGrph.getLocation().y;
        }

        this.aktGrph = (ZeichFen) this.graphen.get(nummer);
        this.aktGrph.setLocation(altX, altY);
        this.aktGrph.setVisible(true);
        this.grAuswahl.select(nummer);

        for (int i = 0; i < this.graphen.size(); i++) {
            if (i != nummer) {
                ((ZeichFen) this.graphen.get(i)).setVisible(false);
            }
        }
    }

    /**
     * Setzt alle Steuerelemente und Fenstereigenschaften.
     */
    private void setzeSteuerelemente() {
        this.setBackground(Color.lightGray);

        JButton b;
        Panel buttonPanel = new Panel();
        Panel buttonPanel2 = new Panel();
        Panel buttonPanel3 = new Panel();
        Panel buttonPanel4 = new Panel();
        Panel listPanel = new Panel();
        Panel editPanel = new Panel();
        Panel labelPanel = new Panel();
        listPanel.setLayout(new GridLayout(0, 1));
        editPanel.setLayout(new GridLayout(4, 2));
        labelPanel.setLayout(new GridLayout(4, 1));
        buttonPanel2.setLayout(new GridLayout(4, 2));
        buttonPanel3.setLayout(new GridLayout(6, 3));
        buttonPanel4.setLayout(new GridLayout(2, 1));

        Panel choiPan = new Panel(new GridLayout(1, 1));

        Panel p = new Panel();
        b = new JButton("Beenden");
        b.addActionListener(this);
        buttonPanel.add(b);
        b = new JButton("Modus");
        b.addActionListener(this);
        buttonPanel.add(b);
        b = new JButton("Dicke");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Normal");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Speichern...");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Bezier");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Farbwahl...");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("DickeFunk");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Gestrichelt");
        b.addActionListener(this);
        buttonPanel2.add(b);
        b = new JButton("Neue Spitze");
        b.addActionListener(this);
        buttonPanel2.add(b);

        this.gestricheltFelt = new JTextField("25", 5);
        this.farbeA2 = new JTextField("", 5);
        this.sonst = new JTextField("", 5);
        this.sonst.setEditable(false);

        editPanel.add(this.sonst);
        editPanel.add(this.gestricheltFelt);
        editPanel.add(this.farbeA2);
        this.farbeA2.setText("((x/y*180) mod 30 + 3)*2");
        // this.farbeA2.setText("(((x - x mod 1250)*5 / y + 1) * 30)*(x /
        // y)+10");
        editPanel.add(new Label("-----"));

        this.grAuswahl = new Choice();
        choiPan.add(this.grAuswahl);
        this.grAuswahl.addItemListener(this);

        b = new JButton("11");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("12");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("13");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("14");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("15");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("16");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("17");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("18");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("19");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("20");
        b.addActionListener(this);
        buttonPanel3.add(b);
        b = new JButton("21");
        b.addActionListener(this);
        buttonPanel3.add(b);

        p.add(buttonPanel);
        p.add(listPanel);
        p.add(labelPanel);
        p.add(editPanel);
        p.add(buttonPanel2);
        p.add(choiPan);
        p.add(buttonPanel3);
        p.add(buttonPanel4);

        Panel pp = new Panel(new GridLayout(2, 1));
        this.pfeilAnfang = new Choice();
        this.pfeilEnde = new Choice();
        this.pfeilAnfang.addItemListener(this);
        this.pfeilEnde.addItemListener(this);
        this.pfeilAnfFakt = new JTextField();
        this.pfeilEndFakt = new JTextField();
        pp.add(this.pfeilAnfang);
        pp.add(this.pfeilEnde);
        pp.add(this.pfeilEndFakt);
        pp.add(this.pfeilAnfFakt);
        this.pfeilAnfFakt.addKeyListener(this);
        this.pfeilEndFakt.addKeyListener(this);

        JPanel ppp = new JPanel();
        Object[] s = {"Luke", "Chrissie"};
        this.strichelungen = new JComboBox(s);
        ppp.add(this.strichelungen);
       
        p.add(pp);
        p.add(ppp);
       
        this.add(p);

        this.setResizable(true);
    }

    /**
     * Behandelt die �nderung des Listenelements.
     *
     * @param event
     *            Das Event.
     */
    public void itemStateChanged(final ItemEvent event) {
        this.aktGrph.setAnfang(this.pfeilAnfang.getSelectedItem());
        this.aktGrph.setEnde(this.pfeilEnde.getSelectedItem());
        this.aktGrph.neuZeichnen();
    }

    /**
     * Die actionPerformed Methode.
     *
     * @param event
     *            Das Event.
     */
    public void actionPerformed(final ActionEvent event) {
        Object obj = event.getSource();
        String cmd = event.getActionCommand();
        Iterator<ZeichFen> itG;
        int fehler = 0;

        this.setCursor(new Cursor(Cursor.WAIT_CURSOR));

        itG = this.graphen.iterator();
        while (itG.hasNext()) {
            ((ZeichFen) itG.next()).setCursor(new Cursor(Cursor.WAIT_CURSOR));
        }

        try {
            if (obj instanceof JButton) {
                if (cmd.equals("Beenden")) {
                    WindowClosingAdapter ende = new WindowClosingAdapter(true);
                    ende.schliesseExt(this);
                } else if (cmd.equals("Modus")) {
                    boolean b1;
                    boolean b2;

                    b1 = this.aktGrph.isMarkiereEcken();
                    b2 = this.aktGrph.isZeichneGrundPfeil();

                    if (!b1 && !b2) { // 00
                        b1 = true;
                        b2 = false;
                    } else if (!b1 && b2) { // 01
                        b1 = false;
                        b2 = false;
                    } else if (b1 && !b2) { // 10
                        b1 = true;
                        b2 = true;
                    } else if (b1 && b2) { // 11
                        b1 = false;
                        b2 = true;
                    }

                    this.aktGrph.setMarkiereEcken(b1);
                    this.aktGrph.setZeichneGrundPfeil(b2);

                } else if (cmd.equals("Dicke")) {
                    double dicke = Double.parseDouble(
                            this.gestricheltFelt.getText());
                    this.aktGrph.setAktDicke(dicke);
                } else if (cmd.equals("Normal")) {
                    this.aktGrph.normalisiere();
                } else if (cmd.equals("Bezier")) {
                    double bK = 2;

                    if (!this.gestricheltFelt.getText().equals("")) {
                        bK = Double.parseDouble(this.gestricheltFelt.getText());
                    }

                    this.aktGrph.bezier(bK);
                } else if (cmd.equals("Farbwahl...")) {
                    Color ausg1 = null;
                    Color ausg2 = null;

                    if (this.aktAusg != null) {
                        ausg1 = this.aktAusg.getRahmenFarbe();
                        ausg2 = this.aktAusg.getFuellFarbe();
                    }

                    ausg1 = JColorChooser.showDialog(this,
                            "Rahmenfarbe w�hlen", ausg1);
                    ausg2 = JColorChooser.showDialog(this, "F�llfarbe w�hlen",
                            ausg2);

                    this.aktAusg = new AusgMerkm(ausg1, ausg2, ausg1 != null,
                            ausg2 != null);

                    this.aktGrph.setzeAktSegAusg(this.aktAusg);
                } else if (cmd.equals("Speichern...")) {
                    AllgemeinerDialog diaAllg;
                    FileDialog dia;
                    String oldVerz;
                    ArrayList<String> modi = new ArrayList<String>(4);
                    modi.add("png");
                    modi.add("bmp");
                    modi.add("jpg");
                    modi.add("Abbrechen");
                   
                    diaAllg = new AllgemeinerDialog(
                            this,
                            "",
                            "Skalierung und Dateityp angeben",
                            modi,
                            "1",
                            50,
                            50,
                            true);
                    diaAllg.setVisible(true);
                   
                    if (!diaAllg.getResult().equals("Abbrechen")) {
                        dia = new FileDialog(this, "Pfeil als PNG speichern",
                                FileDialog.SAVE);
   
                        dia.setFilenameFilter(
                                new DateiFilter(diaAllg.getResult()));
   
                        dia.setVisible(true);
   
                        if (dia.getDirectory() != null
                                && dia.getFile() != null) {
                            oldVerz = this.pars.getStdPfad();
                            this.pars.setStdPfad(dia.getDirectory());
                            this.aktGrph.savePNG(
                                    dia.getFile(),
                                    diaAllg.getResult(),
                                    Double.parseDouble(diaAllg.getText()));
                            this.pars.setStdPfad(oldVerz);
                        }
                    }
                } else if (cmd.equals("DickeFunk")) {
                    this.setzeDickenFunk(this.farbeA2.getText());
                } else if (cmd.equals("Gestrichelt")) {
                    this.gestrichelt();
                } else if (cmd.equals("Neue Spitze")) {
                    final String neuerName = "Spitze" + this.numNeueSpitzen;
                    Pfeilspitze spitze = new Pfeilspitze(
                            this.aktGrph.getGrundPolSpitze(),
                            neuerName);
                   
                    this.pfeilAnfang.add(neuerName);
                    this.pfeilEnde.add(neuerName);
                    PfeilMaster.WEITERE_SPITZEN.add(spitze);
                    this.numNeueSpitzen++;
                }
            }

        } catch (Exception e) {
            fehler = 2;
            e.printStackTrace();
        } finally {
            this.aktGrph.neuZeichnen(fehler);

            itG = this.graphen.iterator();
            while (itG.hasNext()) {
                ((ZeichFen) itG.next()).setCursor(new Cursor(
                        Cursor.DEFAULT_CURSOR));
            }
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }

    /**
     * Gestrichelten Pfeil erzeugen.
     */
    private void gestrichelt() {
        this.aktGrph.gestrichelt(
                Double.parseDouble(this.gestricheltFelt.getText()));
    }
   
    /**
     * @param segmente  Die Segmente
     */
    public void setzeSegmentFarben(final AusgMerkm[] segmente) {
//        this.strichelSegmente = segmente;
       
        for (AusgMerkm s : segmente) {
            this.strichelungen.add(new JButton(s.toString()));
        }
    }
       
    /**
     * Setzt die Dicken nach einer Funktion.
     *
     * @param fs
     *            Die Funktion.
     */
    private void setzeDickenFunk(final String fs) {
        Funktion f = (new Funktion()).convertStoFunk(fs);
        final int ypsilon;
        ArrayList<Double> neuDick = new ArrayList<Double>(
                this.aktGrph.getPfeilPol().size());

        Integer anfang = this.aktGrph.getMark1();
        Integer ende = this.aktGrph.getMark2();
       
        if (anfang == null) {
            anfang = 0;
        }
        if (ende == null) {
            ende = this.aktGrph.getPfeilPol().size() - 1;
        }
       
        int zwisch;
        if (anfang > ende) {
            zwisch = ende;
            ende = anfang;
            anfang = zwisch;
        }
       
        ypsilon = ende - anfang + 1;
       
        for (int ix = 0; ix < anfang; ix++) {
            neuDick.add(this.aktGrph.getDicken().get(ix));
        }
       
        for (int ix = anfang; ix <= ende; ix++) {
            neuDick.add(f.eval(ix - anfang, ypsilon));
        }

        for (int ix = ende + 1; ix < this.aktGrph.getPfeilPol().size(); ix++) {
            neuDick.add(this.aktGrph.getDicken().get(ix));
        }

        this.aktGrph.setDicken(neuDick);
    }

    /**
     * Erzeugt einen neuen Graphen mit dem ersten freien Standardnamen.
     *
     * @return Der neue Graph.
     */
    public ZeichFen neuerGraph() {
        String graphName = "";
        int graphNummer = 0;
        ZeichFen graphFenster;
        boolean ok = false;

        while (!ok) {
            graphName = "graph" + graphNummer;
            Iterator<ZeichFen> it = this.graphen.iterator();
            ok = true;
            while (it.hasNext()) {
                if (((ZeichFen) it.next()).toString().equals(graphName)) {
                    ok = false;
                }
            }
            graphNummer++;
        }

        graphFenster = new ZeichFen(Konstanten.ZEICHNUNG_NAME
                + this.kleinsteFreieID(), Konstanten.ZEICHNUNG_NAME
                + this.kleinsteFreieID(), this.pars, this.kleinsteFreieID(),
                this);

        this.grAuswahl.add(graphName);
        this.graphen.add(graphFenster);
        this.graphZaehl++;

        return graphFenster;
    }

    /**
     * Aktuellen Graphen setzen.
     *
     * @param aktGr
     *            The aktGraph to set.
     */
    public void setAktGraph(final ZeichFen aktGr) {
        this.aktGrph = aktGr;
    }

    /**
     * Selektiert das angegebene Graphfenster (macht es sichtbar, etc).
     *
     * @param graph
     *            Das zu selektierende Graphfenster.
     */
    public void selGraph(final ZeichFen graph) {
        int i = 0;

        while (this.graphen.get(i) != graph) {
            i++;
        }

        this.waehleGraph(i);
        this.aktGrph.neuZeichnen();
    }

    /**
     * Gibt die kleinste noch nicht von einem der aktuellen Graphen verwendete
     * ID zur�ck.
     *
     * @return Die kleinste noch nicht verwendete ID.
     */
    public int kleinsteFreieID() {
        int kleinste = 0;
        Iterator<ZeichFen> it = this.graphen.iterator();
        ZeichFen aktGr;

        while (it.hasNext()) {
            aktGr = (ZeichFen) it.next();
            if (kleinste <= aktGr.getId()) {
                kleinste = aktGr.getId() + 1;
            }
        }

        return kleinste;
    }

    /**
     * Zeichnet alle Vis-Fenster neu.
     */
    public void repaintAll() {
        Iterator<ZeichFen> it = this.graphen.iterator();
        while (it.hasNext()) {
            ((ZeichFen) it.next()).neuZeichnen();
        }
    }

    /**
     * Die Startmethode des Frames.
     *
     * @param args
     *            Die Parameter f�r das Parameterobjekt. Sie werden direkt an
     *            die Klasse Parametersatz weitergegeben. Zur Formatierung der
     *            Parameter siehe diese Klasse sowie die Klasse Konstanten des
     *            Pakets statistik.
     */
    public static void main(final String[] args) {
        Parametersatz pr = new Parametersatz(args);
        pr.ergaenze();
        pr.setGraphisch(true);
       
        if (pr.getGraphisch().booleanValue()) {
            PfeilController steuer = new PfeilController(pr);
            // SonstMeth.log(SonstMeth.LOG_DEBUG, pr.toString(), pr, "plain");
            steuer.setVisible(true);
        }
    }

    /**
     * Nicht benutzt.
     *
     * @param arg0
     *            Das Event.
     */
    @Override
    public void keyPressed(final KeyEvent arg0) {

    }

    /**
     * Skalierung.
     *
     * @param arg0
     *            Das Event.
     */
    @Override
    public void keyReleased(final KeyEvent arg0) {
        this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        int fehler = 0;
        Iterator<ZeichFen> itG;
       
        itG = this.graphen.iterator();
        while (itG.hasNext()) {
            ((ZeichFen) itG.next()).setCursor(new Cursor(Cursor.WAIT_CURSOR));
        }
       
        try {
            if (!this.pfeilAnfFakt.getText().equals("")) {
                this.aktGrph.setAnfFakt(Double.parseDouble(this.pfeilAnfFakt
                        .getText()));
            }
            if (!this.pfeilEndFakt.getText().equals("")) {
                this.aktGrph.setEndFakt(Double.parseDouble(this.pfeilEndFakt
                        .getText()));
            }
            if (!this.pfeilAnfFakt.getText().equals("")
                    || !this.pfeilEndFakt.getText().equals("")) {
                this.itemStateChanged(null);
            }

        } catch (Exception e) {
            fehler = 2;
            e.printStackTrace();
        } finally {
            this.aktGrph.neuZeichnen(fehler);

            itG = this.graphen.iterator();
            while (itG.hasNext()) {
                ((ZeichFen) itG.next()).setCursor(new Cursor(
                        Cursor.DEFAULT_CURSOR));
            }
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }

    /**
     * Nichts.
     *
     * @param arg0  Das Event.
     */
    @Override
    public void keyTyped(final KeyEvent arg0) {
       
    }
}
TOP

Related Classes of fmg.fmg8.pfeilGUI.PfeilController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.