Results 1 to 5 of 5
  1. #1
    cilaes is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Simple: Text to an image & save.

    I have some former experience with Java but not a lot... Now that I'm on a Mac, I have started to try and pick up some more on Java to create more cross-platform capabilities...

    I am simply trying to take selected text from a text file and create a plain white image with specific dimensions and paste the text to the image in courier new font, then save the image. What would be a good reference point on starting out to do this?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,419
    Rep Power
    25

    Default

    The GUI should be straight forward.
    Extend a JPanel and override its paintComponent method to draw the text (look at Graphics methods).
    ?? How to get an image from the graphics drawn above???
    See BufferedImage class
    Look at ImageIO class for writing the image to disk.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.DefaultEditorKit;
    
    public class TextToImage implements ActionListener {
        JTextArea textArea;
        Dimension size = new Dimension(400,300);
    
        public void actionPerformed(ActionEvent e) {
            save();
        }
    
        private void save() {
            int w = size.width;
            int h = size.height;
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            textArea.paint(g2);
            g2.dispose();
            try {
                String ext = "jpg";  // bmp, gif, png okay
                File file = new File("textToImage." + ext);
                javax.imageio.ImageIO.write(image, ext, file);
            } catch(IOException e) {
                System.out.println("write error: " + e.getMessage());
            }
        }
    
        private JMenuBar getMenuBar() {
            JMenu menu = new JMenu("Edit");
            Action action = new DefaultEditorKit.CutAction();
            action.putValue(Action.NAME, "Cut");
            menu.add(action);
            action = new DefaultEditorKit.CopyAction();
            action.putValue(Action.NAME, "Copy");
            menu.add(action);
            action = new DefaultEditorKit.PasteAction();
            action.putValue(Action.NAME, "Paste");
            menu.add(action);
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
            return menuBar;
        }
    
        private JScrollPane getContent() {
            textArea = new JTextArea();
            textArea.setFont(new Font("Courier New", Font.PLAIN, 16));
            textArea.setMargin(new Insets(5,5,5,5));
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.setPreferredSize(size);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(textArea, new GridBagConstraints());
            return new JScrollPane(panel);
        }
    
        private JPanel getControls() {
            SpinnerNumberModel widthModel =
                new SpinnerNumberModel(size.width, 150, 600, 1);
            final JSpinner widthSpinner = new JSpinner(widthModel);
            SpinnerNumberModel heightModel =
                new SpinnerNumberModel(size.height, 150, 600, 1);
            final JSpinner heightSpinner = new JSpinner(heightModel);
            ChangeListener cl = new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    JSpinner spinner = (JSpinner)e.getSource();
                    int value = ((Integer)spinner.getValue()).intValue();
                    if(spinner == widthSpinner) {
                        size.width = value;
                    }
                    if(spinner == heightSpinner) {
                        size.height = value;
                    }
                    textArea.revalidate();
                }
            };
            widthSpinner.addChangeListener(cl);
            heightSpinner.addChangeListener(cl);
            JButton button = new JButton("save");
            button.addActionListener(this);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            gbc.weightx = 1.0;
            addComponents("width",  widthSpinner,  panel, gbc);
            addComponents("height", heightSpinner, panel, gbc);
            gbc.anchor = GridBagConstraints.CENTER;
            panel.add(button, gbc);
            return panel;
        }
    
        private void addComponents(String s, JComponent jc, Container c,
                                   GridBagConstraints gbc) {
            gbc.anchor = GridBagConstraints.EAST;
            c.add(new JLabel(s), gbc);
            gbc.anchor = GridBagConstraints.WEST;
            c.add(jc, gbc);
        }
    
        public static void main(String[] args) {
            TextToImage test = new TextToImage();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setJMenuBar(test.getMenuBar());
            f.add(test.getContent());
            f.add(test.getControls(), "Last");
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,419
    Rep Power
    25

    Default

    Thanks for that. I'd forgotten the technique of creating an image, getting a graphics object to draw on that image and then having a component draw itself on the image by calling its paint() method.
    Can you could generalize your code to make an image file for any component by passing a Component to save()?

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    make an image file for any component by passing a Component to save()?
    Probably.
    You could also use the Robot createScreenCapture method.

Similar Threads

  1. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 10:07 AM
  2. Writing text into an image and save it
    By elcapi in forum Java 2D
    Replies: 6
    Last Post: 09-18-2009, 05:47 PM
  3. how to save image to disk after using pixelgrabber
    By shishirg in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 02:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •