public void getSavedLocations() { // System.out.println("inside getSavedLocations"); //CONSOLE * * * * * * * * * * * * * loc.clear(); // clear locations. helps refresh the list when reprinting all the locations BufferedWriter f = null; // just in case file has not been created yet BufferedReader br = null; try { // attempt to open the locations file if it doesn't exist, create it f = new BufferedWriter( new FileWriter("savedLocations.txt", true)); // evaluated true if file does not exist br = new BufferedReader(new FileReader("savedLocations.txt")); String line; // each line is one index of the list loc.add("Saved Locations"); // loop and read a line from the file as long as we don't get null while ((line = br.readLine()) != null) // add the read word to the wordList loc.add(line); } catch (IOException e) { e.printStackTrace(); } finally { try { // attempt the close the file br.close(); // close bufferedwriter } catch (IOException ex) { ex.printStackTrace(); } } }
public void paintComponent(Graphics g) { super.paintComponent(g); File f = new File("Data.txt"); try { textArea.setText(""); reader = new BufferedReader(new FileReader(f)); String str = ""; while (!(str = reader.readLine()).equals("")) { textArea.append(str + "\n"); } reader.close(); } catch (Exception e) { System.out.println(e); } repaint(); }
public static void main(String args[]) throws Exception { final JFrame frame = new JFrame(); final JTextArea textArea = new JTextArea(30, 60); final JScrollPane scrollPane = new JScrollPane(textArea); frame.getContentPane().add(scrollPane); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu menu = new JMenu("File"); ScreenCapture.createImage(menu, "menu.jpg"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("Frame Image"); menu.add(menuItem); menuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // Let the menu close and repaint itself before taking the image new Thread() { public void run() { try { Thread.sleep(50); System.out.println("Creating frame.jpg"); frame.repaint(); ScreenCapture.createImage(frame, "frame.jpg"); } catch (Exception exc) { System.out.println(exc); } } }.start(); }; }); final JButton button = new JButton("Create Images"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.out.println("Creating desktop.jpg"); ScreenCapture.createDesktopImage("desktop.jpg"); System.out.println("Creating frame.jpg"); ScreenCapture.createImage(frame, "frame.jpg"); System.out.println("Creating scrollpane.jpg"); ScreenCapture.createImage(scrollPane, "scrollpane.jpg"); System.out.println("Creating textarea.jpg"); ScreenCapture.createImage(textArea, "textarea.jpg"); System.out.println("Creating button.jpg"); ScreenCapture.createImage(button, "button.jpg"); button.setText("button refreshed"); button.paintImmediately(button.getBounds()); System.out.println("Creating refresh.jpg"); ScreenCapture.createImage(button, "refresh.jpg"); System.out.println("Creating region.jpg"); Rectangle r = new Rectangle(0, 0, 100, 16); ScreenCapture.createImage(textArea, r, "region.png"); } catch (Exception exc) { System.out.println(exc); } } }); frame.getContentPane().add(button, BorderLayout.SOUTH); try { FileReader fr = new FileReader("ScreenCapture.java"); BufferedReader br = new BufferedReader(fr); textArea.read(br, null); br.close(); } catch (Exception e) { } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }