Results 1 to 5 of 5
Thread: Simple: Text to an image & save.
- 09-06-2008, 12:50 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
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?
- 09-06-2008, 03:06 AM #2
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.
- 09-06-2008, 06:08 AM #3
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); } }
- 09-06-2008, 03:53 PM #4
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()?
- 09-06-2008, 06:16 PM #5
make an image file for any component by passing a Component to save()?
Probably.
You could also use the Robot createScreenCapture method.
Similar Threads
-
how to save image as jpg?
By katie in forum New To JavaReplies: 4Last Post: 04-15-2010, 10:07 AM -
Writing text into an image and save it
By elcapi in forum Java 2DReplies: 6Last Post: 09-18-2009, 05:47 PM -
how to save image to disk after using pixelgrabber
By shishirg in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 02:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks