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

        // Class to provide a GUI for the
        // Ship examples.
        // The GUI consists of a Ocean, in which the ships' courses
        // are plotted; a TextArea for their reports and a Panel
        // for control buttons (Quit).
class SeaGUI extends JFrame {
    public SeaGUI(){
        super("Sea");
        setTitle("Sea");
        final int width = 400, height = 400;
        setSize(width,height);
        setBackground(Color.white);

        // Construct three areas.
        // Place the ocean above the reporting area, which is
        // above the button(s).
        final Container contents = getContentPane();
        contents.setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        // Common constraints.
        constraints.gridx = 0;
        constraints.weightx = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.BOTH;

        // The Ocean.
        constraints.gridy = 0;
        Ocean ocean = getOcean();
        // Give most extra space to the ocean.
        constraints.weighty = 5;
        contents.add(ocean,constraints);

        // The Reporting Area.
        constraints.gridy++;
        constraints.weighty = 1;
        getReportingArea().setEditable(false);
        contents.add(new JScrollPane(getReportingArea(),
                                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
                     constraints);

        // The Quit Button.
        constraints.gridy++;
        constraints.weighty = 0;
        contents.add(new QuitJPanel(),constraints);

        final int xPosition = 100, yPosition = 100;
        setLocation(xPosition,yPosition);
        setVisible(true);
    }

        // Place this ship on the ocean.
    public void add(Ship ship){
        getOcean().add(ship);
        // Get the ship to report into the text area.
        ship.setWriter(getWriter());
    }

        // Allow ships to report in the reporting area.
    public void report(String s){
        if(s != null){
            getReportingArea().append(s);
        }
    }

        // Append a newline to the reporting area.
    public void reportln(){
        getReportingArea().append("\n");
    }

        // Is position within the bounds of the sea?
    public boolean inBounds(Position p){
        return getOcean().inBounds(p);
    }

    protected PrintWriter getWriter(){
        return writer;
    }

    protected Ocean getOcean(){
        return ocean;
    }

    protected JTextArea getReportingArea(){
        return reportingArea;
    }

    // The drawing area showing the ships.
    private final Ocean ocean = new Ocean();
    // Reports from the ships to be displayed in the reportingArea.
    private static final int rows = 4, cols = 40;
    private final JTextArea reportingArea = new JTextArea("",rows,cols);
    // Where ships should report to.
    private final PrintWriter writer = new TextComponentPrintWriter(reportingArea);
}
