//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


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


public class gui_oop2 extends JFrame {

  JButton BnTest1 = new JButton("Test1");
  JButton BnTest2 = new JButton("Test2");
  JButton BnTest3 = new JButton("Test3");
  JButton BnTest4 = new JButton("Test4");
  JButton BnTest5 = new JButton("Test5");

  JTextArea editor = new JTextArea();

  public gui_oop2() {
    setSize(500, 530);
    setLocation(200,100);
    setTitle("GUI mit OOP2");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {

    editor.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest1.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest2.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest3.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest4.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTest5.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().setLayout( new BorderLayout() );

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnTest1);
	pnButton.add(BnTest2);
	pnButton.add(BnTest3);
	pnButton.add(BnTest4);
	pnButton.add(BnTest5);

    this.getContentPane().add( pnButton, BorderLayout.NORTH );

    BnTest1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
           BnTest1_Click();
        }
    });
    BnTest2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
           BnTest2_Click();
        }
    });
    BnTest3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
           BnTest3_Click();
        }
    });
    BnTest4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
           BnTest4_Click();
        }
    });
    BnTest5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
           BnTest5_Click();
        }
    });

    this.getContentPane().add( new JScrollPane(editor), BorderLayout.CENTER );

  } // setGUI

   private void editadd(Object obj) {
     editor.append("\n"+obj);
   }

    private void BnTest1_Click() {
        editor.setText("Test1");
        PX p1, p2, p3;
        p1 = new PX(11);
        p2 = new PX(22,"p2");
        p3 = new PX( p1 );
        p3.addX(-3);
        p3.setName("p3");
    
		
        editadd( p1 );
        editadd( p2 );
        editadd( p3 );
        editadd( "Abstand p1 zu p2: "+p1.getDistance(p2) );
    }  // BnTest1_Click

    private void BnTest2_Click() {
        editor.setText("Test2");
        PX p0;
        PXY p1, p2, p3;
        p0 = new PX(33);

        p1 = new PXY(11,22);
        p2 = new PXY(33,44,"p2");
        p3 = new PXY( p1 );
        p3.addX(-3);
        p3.addY(+1);
        p3.setName("p3");
    
        editadd( p1 );
        editadd( p2 );
        editadd( p3 );
        editadd( "Abstand p1 zu p2: "+p1.getDistance(p0) );
    }  // BnTest2_Click

    private void BnTest3_Click() {
        editor.setText("Test3");
        PXYZ p1, p2, p3;
        p1 = new PXYZ(1,2,3);
        p2 = new PXYZ(3,4,5,"p2");
        p3 = new PXYZ( p1 );
        p3.addX(-3);
        p3.addY(+1);
        p3.addZ(+2);
        p3.setName("p3");
    
        editadd( p1 );
        editadd( p2 );
        editadd( p3 );
        editadd( "Abstand p1 zu p2: "+p1.getDistance(p2) );
    }  // BnTest3_Click

    private void BnTest4_Click() {
    }  // BnTest4_Click

    private void BnTest5_Click() {
    }  // BnTest5_Click

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

}  // gui_oop2


abstract class P0 {
    abstract public String toString();
    abstract public double getDistance(Object obj);
}

// -----------------------------------------
// -----------------------------------------
class PX  extends P0 {
  protected int x;
  protected String name;

  public PX(int x) {
    this(x,"noname");
  }

  public PX(int x, String name) {
    this.x=x;
    this.name = name;
  }

  public PX(PX p) {
    x=p.x;
    this.name = p.name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void addX(int deltax) {
    this.x+=deltax;
  }

  public int getX() {
    return x;
  }

  public String toString() {
    return "Name: "+name+"  Value x: "+x;
  }

  public double getDistance(Object obj) {
    PX p = (PX) obj;
    double d1 = this.x - p.x;
    return d1;
  }


}  // PX


// ---------------------------------------
// ---------------------------------------
class PXY  extends PX {
  protected int y;

  public PXY() {
    this(1,1,"");
  }

  public PXY(int x, int y) {
    this(x,y,"");
  }
  public PXY(int x, int y, String name) {
    super(x,name);
    this.y=y;
  }

  public PXY(PXY p) {
    super( (PX) p);
    y=p.y;
  }

  public double getDistance(Object obj) {
    PXY p = (PXY) obj;
    double d1 = this.x - p.x;
    double d2 = this.y - p.y;
    double erg = Math.sqrt(   d1*d1 + d2*d2);
    return erg;
  }



  public void addY(int deltay) {
    this.y+=deltay;
  }

  public int getY() {
    return y;
  }


  public String toString() {
    return super.toString() + "  Value y: "+y;
  }

} // PXY


// ---------------------------------------
// ---------------------------------------
class PXYZ  extends PXY {
  protected int z;

  public PXYZ() {
    this(1,1,1,"");
  }

  public PXYZ(int x, int y,int z) {
    this(x,y,z,"");
  }
  public PXYZ(int x, int y, int z, String name) {
    super(x,y,name);
    this.z=z;
  }

  public PXYZ(PXYZ p) {
    super( (PXYZ) p);
    z=p.z;
  }

  public double getDistance(Object obj) {
    PXYZ p = (PXYZ) obj;
    double d1 = this.x - p.x;
    double d2 = this.y - p.y;
    double d3 = this.y - p.y;
    double erg = Math.sqrt(   d1*d1 + d2*d2+ d3*d3);
    return erg;
  }



  public void addZ(int deltaz) {
    this.z+=deltaz;
  }

  public int getZ() {
    return z;
  }


  public String toString() {
    return super.toString() + "  Value z: "+z;
  }

} // PXYZ

