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

/**class GraphicsDisplay prints text to a graphics window */
class GraphicsDisplay extends JFrame implements Display {
    
    public static final int WIDTH = 600;
    public static final int HEIGHT = 400;

    //posx, posy er positionen fra x,-y
    private int posx = 0;
    private int posy = 0;
    //marg er størrelsen af margen
    private int marg = 0; 
    
    private DisplayModel dm;
    private Graphics pen;
    
    /**constructor GraphicsDisplay
     * @param m the model which contains information of what to be drawn */ 
    public GraphicsDisplay(DisplayModel m) {
	dm = m;
	setTitle("Display of text");
	setSize(WIDTH, HEIGHT);
	setVisible(true);
	addWindowListener(new ExitController());
    } 
    
    /**method paint (re)draws the text on the JFrame
     *@param g the graphics pen */
    public void paint(Graphics g) {
	pen = g; // pennens adresse gemmes i en tilstandsvariabel
	         // til senere brug i metoderne word, newLine, etc
	pen.setFont(new Font("TimesRoman",Font.PLAIN,14));
	setBackground(Color.white);
	posx = 0; posy = 0;
	dm.generateText(this);
    }
    
    /**method word Prints a String in the graphics window
     *@param s the string to be printed */
    public void word(String s) {
	if (posx + pen.getFontMetrics().stringWidth(s) > WIDTH) { newLine(); }
	pen.drawString(s,
		       posx + marg,
		       posy + pen.getFontMetrics().getHeight()
		       );
	posx = posx + pen.getFontMetrics().stringWidth(s);
    }
    
    /**method newLine makes a new line */
    public void newLine() {
	posy = posy + pen.getFontMetrics().getHeight() -
	    (int)(0.15 * pen.getFontMetrics().getHeight());
	posx = 0;
    }
    
    /**method paragraph makes a new paragraph */
    public void paragraph() {
	if (posx != marg) newLine(); 
	newLine();
	posx = marg;
    }
    
    /**method incMargin increases the margin */
    public void incMargin() {
	marg = marg + pen.getFontMetrics().stringWidth("NAR");
    }
    
    /**method decMargin decreases the margin */
    public void decMargin() {
	marg = marg - pen.getFontMetrics().stringWidth("NAR");
    }
}
