Jmenuitem bgcoloritem new

This preview shows page 4 - 7 out of 8 pages.

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 file File collageFile = fileChooser.getOutputFile(this, "Select the file to save text collage", "filename.collage"); if (collageFile == null) return; try { // Open file writer PrintWriter printWriter = new PrintWriter (collageFile); // Save background color printWriter.println ("BACKGROUND COLOR: " + Integer.toString(backgroundColor.getRGB())); printWriter.println(""); // Save strings data for(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());
Image of page 4
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 writer printWriter.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 File File collageFile = fileChooser.getInputFile(this, "Select the file to save text collage"); if (collageFile == null) return; try { // Open file scanner Scanner fileScanner = new Scanner(collageFile); // Set background color String backgroundColorLine = fileScanner.nextLine().replace("BACKGROUND COLOR: ", ""); backgroundColor = new Color(Integer.parseInt(backgroundColorLine)); // Read strings data while(fileScanner.hasNext()) { String readedLine = fileScanner.nextLine(); // Ignore empty lines if(readedLine.replaceAll("\\s+","").equals("")) continue; // Read String String itemString = readedLine.replace("STRING: ", ""); // Read position readedLine = fileScanner.nextLine(); int itemPosX = Integer.parseInt(readedLine.replace("Position: (", "").replace(")","").split(",")[0]); int itemPosY = Integer.parseInt(readedLine.replace("Position: (", "").replace(")","").split(",")[1]);
Image of page 5
// Read font readedLine = fileScanner.nextLine(); Font itemFont = null; String readedFont = readedLine.replace("Font: ",""); if(! readedFont.equals("null")) itemFont = Font.decode(readedFont); // Read text color and trasparency readedLine = fileScanner.nextLine(); Color itemTextColor = new Color(Integer.parseInt(readedLine.replace("TextColor: ",""))); readedLine = fileScanner.nextLine(); double itemTextTransparency = Double.parseDouble(readedLine.replace("TextTransparency: ","")); // Read text background readedLine = fileScanner.nextLine();
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 8 pages?

  • Three '18
  • Menu bar

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture