colorItem.addActionListener(menuHandler);optionsMenu.add(colorItem);JMenuItem bgColorItem = new JMenuItem("Set Background Color...");bgColorItem.addActionListener(menuHandler);optionsMenu.add(bgColorItem);JMenuItem rndCol = new JMenuItem("Random Text Collage...");rndCol.setAccelerator(KeyStroke.getKeyStroke(commandKey + "R"));rndCol.addActionListener(menuHandler);fileMenu.add(rndCol);}return menuBar;}/*** Carry out one of the commands from the menu bar.* @param command the text of the menu command.*/private void doMenuCommand(String command) {if (command.equals("Save...")) { // save all the string info to a fileFile collageFile = fileChooser.getOutputFile(this, "Select thefile to save text collage", "filename.collage");if (collageFile == null)return;try {// Open file writerPrintWriter printWriter = new PrintWriter (collageFile);// Save background colorprintWriter.println ("BACKGROUND COLOR: " + Integer.toString(backgroundColor.getRGB()));printWriter.println("");// Save strings datafor(DrawTextItem s : collageStrings) {printWriter.println("STRING: " + s.getString());printWriter.println("Position: (" + s.getX() + "," + s.getY() + ")");printWriter.println("Font: " + s.getFont().getName());printWriter.println("TextColor: " + Integer.toString(s.getTextColor().getRGB()));printWriter.println("TextTransparency: " + s.getTextTransparency());String tb = "null";if(s.getBackground() != null) tb = Integer.toString(s.getBackground().getRGB());
printWriter.println("TextBackground: " + tb);printWriter.println("BackgroundTransparency: " + s.getBackgroundTransparency());printWriter.println("RotationAngle: " + s.getRotationAngle());printWriter.println("Magnification: " + s.getMagnification());printWriter.println("Border: " + s.getBorder()); printWriter.println("");}// Close file writerprintWriter.close(); }catch (Exception e) {JOptionPane.showMessageDialog(this, "Sorry, an error occurred while trying to save the collage :\n" + e);}}else if (command.equals("Open...")) { // read a previously saved file, and reconstruct the list of strings// Open FileFile collageFile = fileChooser.getInputFile(this, "Select the file to save text collage");if (collageFile == null)return;try {// Open file scannerScanner fileScanner = new Scanner(collageFile);// Set background colorString backgroundColorLine = fileScanner.nextLine().replace("BACKGROUND COLOR: ", "");backgroundColor = new Color(Integer.parseInt(backgroundColorLine));// Read strings datawhile(fileScanner.hasNext()) {String readedLine = fileScanner.nextLine();// Ignore empty linesif(readedLine.replaceAll("\\s+","").equals("")) continue;// Read StringString itemString = readedLine.replace("STRING: ","");// Read positionreadedLine = fileScanner.nextLine();int itemPosX = Integer.parseInt(readedLine.replace("Position: (", "").replace(")","").split(",")[0]);int itemPosY = Integer.parseInt(readedLine.replace("Position: (", "").replace(")","").split(",")[1]);
// Read fontreadedLine = fileScanner.nextLine();Font itemFont = null;String readedFont = readedLine.replace("Font: ","");if(! readedFont.equals("null"))itemFont = Font.decode(readedFont);// Read text color and trasparencyreadedLine = fileScanner.nextLine();Color itemTextColor = new Color(Integer.parseInt(readedLine.replace("TextColor: ","")));readedLine = fileScanner.nextLine();double itemTextTransparency = Double.parseDouble(readedLine.replace("TextTransparency: ",""));// Read text backgroundreadedLine = fileScanner.nextLine();