import java.io.*; /* * A program for inputting a line of text and framing it inside an EditGrid. * The purpose is to illustrate how to integrate two classes. Also it shows * how to use the EditGrid class we provided. */ public class FrameText { /* Receive the input text and frame it. */ public static void main(String[] args) { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("Please enter the text to be framed >> "); frameText(buf.readLine()); } catch(IOException e) { System.out.println("Trouble reading the input."); e.printStackTrace(); } } /* Create and display an EditGrid around the given text. */ public static void frameText(String txt) { /* * The EditGrid should have extra rows above and below the word * and extra columns to the left and rigth of it. */ EditGrid eg = new EditGrid(txt.length() + 1, 3); putWord(eg, txt); // Write the word in the grid. frameTopOfWord(eg, txt); // Draw top of frame. frameLeftOfWord(eg, txt); // Draw left of frame. frameRightOfWord(eg, txt); // Draw right of frame. frameBottomOfWord(eg, txt); // Draw bottom of frame. eg.printGridContents(); // Display the framed word. } /* Write the word in the EditGrid. */ public static void putWord(EditGrid eg, String txt) { for (int i = 0; i < txt.length(); i++) { /* Write the letters between the dots. */ eg.setGridContents(3, 2*i+3, txt.charAt(i)); } } /* Write the top boundary of the EditGrid. */ public static void frameTopOfWord(EditGrid eg, String txt) { for (int i = 1; i < txt.length() + 1; i++) { /* Write the edges between the dots. */ eg.setGridContents(1, 2*i+1, '_'); } } /* Write the left boundary of the EditGrid. */ public static void frameLeftOfWord(EditGrid eg, String txt) { eg.setGridContents(2, 2, '|'); eg.setGridContents(4, 2, '|'); } /* Write the right boundary of the EditGrid. */ public static void frameRightOfWord(EditGrid eg, String txt) { eg.setGridContents(2, txt.length()*2 + 2, '|'); eg.setGridContents(4, txt.length()*2 + 2, '|'); } /* Draw a line in the grid below the row where we want the text. */ public static void frameBottomOfWord(EditGrid eg, String txt) { for (int i = 1; i < txt.length() + 1; i++) { /* Write the edges between the dots. */ eg.setGridContents(5, 2*i+1, '_'); } } }