// Ted Pawlicki's minimal application framework... // Shows all one needs to get going writing a graphical application... import java.io.*; import java.awt.*; import java.awt.event.*; public class VanillaFrame2 extends Frame implements WindowListener, ActionListener { private TextArea inputTextArea; private Button saveButton , loadButton; private PrintWriter outfile; private BufferedReader infile; protected TextField nameField1, nameField2 ; private Panel p1,p2,p3; private myCanvas c1; public static void main(String [] args){ VanillaFrame2 myFrame = new VanillaFrame2(); myFrame.setSize(500,500); myFrame.init(); myFrame.setVisible(true); } public void init() { // build graphics Canvas c1 = new myCanvas(this); p3 = new Panel(); p3.setLayout(new GridLayout(2,1)); p3.add(c1); // load file controls loadButton = new Button("load"); loadButton.addActionListener(this); nameField1 = new TextField(20); p1 = new Panel(); p1.add(loadButton); p1.add(nameField1); // save file controls saveButton = new Button("save"); saveButton.addActionListener(this); nameField2 = new TextField(20); p2 = new Panel(); p2.add(saveButton); p2.add(nameField2); //text area inputTextArea = new TextArea(20,30); inputTextArea.setEditable(true); p3.add(inputTextArea); // put it on the frame add(p1,BorderLayout.NORTH); add(p3,BorderLayout.CENTER); add(p2,BorderLayout.SOUTH); // be sure we can exit addWindowListener(this); repaint(); c1.repaint(); } public void actionPerformed(ActionEvent evt) { // process the load button if (evt.getSource() == loadButton){ String fileName = nameField1.getText(); try{ infile = new BufferedReader ( new FileReader(fileName)); inputTextArea.setText(""); String line ; while ((line = infile.readLine()) != null) { inputTextArea.append(line+"\n"); } infile.close(); } catch (IOException e) { System.err.println("File error: " + e.toString()); } } // process the save button if (evt.getSource() == saveButton){ String fileName = nameField2.getText(); try{ outfile = new PrintWriter ( new FileWriter(fileName, true)); outfile.print(inputTextArea.getText()); outfile.close(); } catch (IOException e) { System.err.println("File error: " + e.toString()); } } repaint(); c1.repaint(); } // we have to be able to get out public void windowClosing(WindowEvent e) { System.exit(0); } // required methods public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } class myCanvas extends Canvas { private VanillaFrame2 myFrameHandle; public myCanvas(VanillaFrame2 f) { myFrameHandle = f ; } public void paint(Graphics g) { g.setXORMode(Color.green); g.setColor(new Color(255,0,0)); g.fillOval(50,50,100,100); g.setColor(new Color(0,0,255)); g.fillOval(100,100,100,100); g.setColor(Color.black); g.drawString("Input file :" + myFrameHandle.nameField1.getText(),250,100); g.drawString("Output file :" + myFrameHandle.nameField2.getText(),250,150); g.drawString("Graphics Area",125,125); } }