//Titel:            Testbeispiel für Grafiken
//Version:          2.0
//Copyright:        HS-Harz, FB AI, 2007
//Autor:            Dipl.-Inf. Dipl.-Ing. Michael Wilhelm
//Firma:

// Einfaches Beispiel für ein Mini-Grafikprogramm
// liste mit Grafik-Objekten (Linie, Rechteck, Kreis)
// Anzeigen
// noch nicht fertig

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.Vector;

public class jpaint1 extends JFrame  {

    // Menü Variablen
  private JMenuBar menuBar1;
  private JMenu MainFile;
  private JMenuItem MnNew;
  private JMenuItem MnOpen;
  private JMenuItem MnClose;

  private JMenu MainGraphic;
  private JMenuItem MnLine;
  private JMenuItem MnCircle;
  private JMenuItem MnRect;
  private JMenuItem MnClear;

  private CCanvas ccanvas;

  private Vector liste = new Vector();


  // Konstruktor
  public jpaint1() {
    setGUI();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void setGUI() {
    this.getContentPane().setLayout( new BorderLayout() );

    ccanvas = new CCanvas(liste);
    this.getContentPane().add(ccanvas, BorderLayout.CENTER );

    this.setSize( 800, 600);
    this.setVisible(true);

    setMenus();


  }  // setGUI


    // setzt die Menüs
  private void setMenus() {
    menuBar1 = new JMenuBar();

      // DATEI
    MainFile = new JMenu("Datei");
    MnNew = new JMenuItem("Neu");
    MnOpen = new JMenuItem("Öffnen");

    MnClose = new JMenuItem("Schließen");


    MnNew.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnNew_click();
      }
    });
    MnNew.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    MainFile.add(MnNew);


    MnOpen.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnOpen_actionPerformed(e);
      }
    });
    MnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    MainFile.add(MnOpen);

    menuBar1.add(MainFile);

    MainFile.addSeparator();

    // --------------------------
    MnClose.addActionListener(new ActionListener()  {

      public void actionPerformed(ActionEvent e) {
        MnClose_actionPerformed(e);
      }
    });
    MainFile.add(MnClose);

    // --------------------------
  MainGraphic = new JMenu("Grafik");
  menuBar1.add(MainGraphic);
  MnLine = new JMenuItem("Linie");
  MnCircle = new JMenuItem("Kreis");
  MnRect = new JMenuItem("Rechteck");

  MnClear = new JMenuItem("Liste löschen");

  MnLine.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnLine_click();
      }
    });
    MainGraphic.add(MnLine);
  MnRect.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnRect_click();
      }
    });
    MainGraphic.add(MnRect);
  MnCircle.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnCircle_click();
      }
    });
    MainGraphic.add(MnCircle);

  MnClear.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnClear_click();
      }
    });
    MainGraphic.addSeparator();
    MainGraphic.add(MnClear);


    this.setJMenuBar(menuBar1);
  } // setMenus

  void MnLine_click() {
      CLine line = new CLine( 100,100,200,200);
      liste.add(line);
      ccanvas.updateUI();
      this.setVisible(true);
  }

  void MnRect_click() {
  }
  void MnCircle_click() {
  }

  void MnClear_click() {
    liste.clear();
    ccanvas.updateUI();
    this.setVisible(true);
  }

  void MnClose_actionPerformed(ActionEvent e) {
    System.exit(0);
  }


    // Löschen der Liste, Neue Datei
  void MnNew_click() {
   System.out.println("in New");
  }


  // Datei lesen, noch nicht implementiert
  // es fehlt auch das Speichern
  void MnOpen_actionPerformed(ActionEvent e) {
   System.out.println("in Open");
  }  // MnOpen





    // MAIN
  static public void main(String[] args) {
    jpaint1 frame = new jpaint1();
    frame.setVisible(true);
  }

}


//
class CCanvas extends JPanel {

  Vector _liste = new Vector();

  public CCanvas(Vector liste) {
    _liste = liste;
  }

  public void setVector(Vector liste) {
    _liste = liste;
  }

  public void paint (Graphics g) {
    Object obj;

     if (_liste.isEmpty() ) {
       g.drawString("Canvas",50,50);
     }
     else {
        for (int i=0; i<_liste.size(); i++) {
           obj = _liste.elementAt(i);
           if (obj instanceof CLine) {
             CLine l = (CLine) obj;
             g.drawLine( l.x1, l.y1, l.x2, l.y2 );
           }
        } // for
     } // else
  }  // paint

}



class CLine {
  public int x1,y1, x2,y2;

  public CLine(int x1,int y1, int x2,int y2) {
    this.x1=x1;
    this.y1=y1;
    this.x2=x2;
    this.y2=y2;
  }

}

class CRect {
  public int x1,y1, x2,y2;


}

class CCircle {
  public int x,y, r;


}




