Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Communication Patterns: A Guide for Developers and Architects
this week in the
Design and Architecture
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Swing / AWT / SWT
Images
Richard West
Ranch Hand
Posts: 127
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi everyone,
I have a bit of a problem converting a JTextPane to an image.
I am able to convert the JTextPane to a PNG image but its quality is really bad. Is there a way that i can convert my JTextPane to a high quality image.
The type of image format does not matter
Here is what i have so far
public void export(JComponent Component) { File f1 = new File("C:\\Temp.PNG"); Dimension Size5 = Component.getSize(); BufferedImage Image1 = new BufferedImage(Size5.width, Size5.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = Image1.createGraphics(); g2.setRenderingHint (RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC); Component.paint(g2); try { ImageIO.write(Image1, "PNG", f1); } catch (Exception e) { e.printStackTrace(); } }
I hope someone can help with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.text.*; public class ExportTest { public ExportTest() { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); initStyles(doc); initDoc(doc); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(getUIPanel(textPane), "North"); f.getContentPane().add(new JScrollPane(textPane)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } /** * copied from TextSamplerDemo in java tutorial */ private void initStyles(StyledDocument doc) { Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontSize(def, 18); Style s = doc.addStyle("large", regular); StyleConstants.setFontSize(s, 22); s = doc.addStyle("italic", regular); StyleConstants.setItalic(s, true); s = doc.addStyle("pink", regular); StyleConstants.setBackground(s, Color.pink); s = doc.addStyle("underline", regular); StyleConstants.setUnderline(s, true); s = doc.addStyle("sansSerif", regular); StyleConstants.setFontFamily(s, "lucida sans demibold"); s = doc.addStyle("red", regular); StyleConstants.setForeground(s, Color.red); } /** * copied from TextSamplerDemo in java tutorial */ private void initDoc(StyledDocument doc) { String[] textData = { "A text component that can be marked up with attributes that " + "are represented graphically. ", "You can find how-to information and examples of using text " + "panes in Using Text Components, a section in The Java Tutorial.\n", "This component models paragraphs that are composed of runs " + "of character level attributes.", " ", "Each paragraph may have a logical style attached to it which " + "contains the default attributes to use", " if not overridden by attributes set on the paragraph or " + "character run. ", "Components and images may be embedded in the flow of text." }; String[] styles = { "large", "italic", "pink", "regular", "underline", "sansSerif", "red" }; try { for(int j = 0; j < textData.length; j++) doc.insertString(doc.getLength(), textData[j], doc.getStyle(styles[j])); } catch(BadLocationException ble) { System.err.println("initDoc: " + ble.getMessage()); } } private JPanel getUIPanel(final JTextPane textPane) { JButton save = new JButton("save"); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { export(textPane); } }); JPanel panel = new JPanel(); panel.add(save); return panel; } private void export(Component c) { File file = new File("c:/jexp/textPane.png"); Dimension d = c.getSize(); BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); c.paint(g2); g2.dispose(); try { ImageIO.write(image, "png", file); } catch(IOException ioe) { System.err.println("image write: " + ioe.getMessage()); } } public static void main(String[] args) { new ExportTest(); } }
Hey! You're stepping on my hand! Help me tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Creating a image file using TextLayout. Need to find right startYpoint
How can I reduce the image size of a PNG file
Copying JComponent into an Image
Java Mail with attached Image
BufferedImage questions.
More...