package _umgebung;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public abstract class _Panel extends JPanel
{
	private static final long serialVersionUID = 1L;

	private String ausgabe = "";
	private static Exception e;
	
	public void print(String ausgabe)
	{
		if (!ausgabe.equals(""))
		{
			this.ausgabe = this.ausgabe + ausgabe;
		}
	}
	
	public void println(String ausgabe)
	{
		if (!ausgabe.equals(""))
		{
			this.ausgabe = this.ausgabe + ausgabe;
		}
		this.ausgabe = this.ausgabe + "\n";
	}
	
	public void println()
	{
		this.ausgabe = this.ausgabe + "\n";
	}
	
	public void print(Character ausgabe)	{print(ausgabe.toString());}  
	public void println(Character ausgabe)	{println(ausgabe.toString());}
	public void print(Integer ausgabe)		{print(ausgabe.toString());}  
	public void println(Integer ausgabe)	{println(ausgabe.toString());}
	public void print(Double ausgabe)		{print(ausgabe.toString());}  
	public void println(Double ausgabe)		{println(ausgabe.toString());}
	
	private void textReset()
	{
		this.ausgabe = "";
	}
	
	private void textAnzeige(Graphics g)
	{
		Color c 	= g.getColor();
		Font f 		= g.getFont();
		g.setColor(Color.BLACK);
		g.setFont(new Font("Courier", Font.PLAIN, 20));
		
		String[] text = ausgabe.split("\n");
		
		for(int i=0; i<text.length; i++)
		{
			g.drawString(text[i], 10, 20*(i+1));
		}
		g.setFont(f);
		g.setColor(c);
	}
	
		
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);			
	
		// Programm kontrolliert starten
		try 
		{	
			this.textReset();
			int index = _Eingabe.getIndex(); 
			meinProgramm(_Start.args, (Graphics2D) g, index, _Eingabe.getString(index), _Eingabe.getChar(index), _Eingabe.getDouble(index), _Eingabe.getInt(index));
			this.textAnzeige(g);;
		}
		catch(Exception e)
		{	// Im Fehlerfall: Fehler anzeigen
			_Panel.e = e;
			javax.swing.SwingUtilities.invokeLater(new Runnable()
			{	// Threadsicheren MessageDialog anzeigen
				public void run()
				{
					JOptionPane.showMessageDialog(null,
							"Im Programm ist ein Fehler aufgetreten!\n" 
			            	+ _Panel.e.toString() + "\n",
			            	"FEHLER", JOptionPane.ERROR_MESSAGE);
				}
			});
		}
	}
	
	public abstract void meinProgramm(
			String[] 	args,
			Graphics2D 	g,
			int			eingabeIndex,
			String 		sEingabe, 
			char 		cEingabe, 
			double 		dEingabe, 
			int 		iEingabe);
}
