Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

save an image of a JtextPane

807607Dec 11 2006
Hello

In first , please , excuse me for my englich ....
I would like to save in a jpeg file , the image of a JtextPane. In JtextPane there is a web page (html) . My probleme is that I can't saving the image if th web page is not load in the JTextPane (if I do it my image is black, without the web page...)
Anybody knows how I could :
- save the image without wait the load web page
- or (better) save the image without the web page was not visible

I put my java code , if anybody has an idea ... there is a button for saving the image (woth this methode I'm sure that the web page is load in the JtextPane. but I'm not satisfy!)

thanks a lot

import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

/**
* Created by IntelliJ IDEA.
* User: JSC
* Date: 8 d�c. 2006
* Time: 17:00:18
* To change this template use File | Settings | File Templates.
*/
public class Test {
private JTextPane editorPane;
private JFrame frame;

public static void main(String[] args) {
Test t = new Test();
t.run();
}

private void run() {
editorPane = new JTextPane();
editorPane.setEditable(false);
URL helpURL = null;
try {
helpURL = new URL("http://www.google.fr");
}
catch (MalformedURLException e) {
e.printStackTrace();
}
if (helpURL != null) {
try {
/*
FileInputStream fin = new FileInputStream("Google.htm");
File f = new File("Google.htm");
byte[] res = new byte[(int) f.length()];
fin.read(res);
String sres = new String(res);
System.err.println("RES : " + sres);
*/
editorPane.setPage(helpURL);
}
catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + helpURL);
}
}
else {
System.err.println("Couldn't find file: http://www.google.fr");
}

//Put the editor pane in a scroll pane.
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));
frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(editorScrollPane, BorderLayout.CENTER);
JButton button = new JButton(new AbstractAction() {
public void actionPerformed(ActionEvent e) {

saveImage();
}
});
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setSize(800, 600);
frame.show();

}

private void saveImage() {
RenderedImage image = getImage(frame);
try {
// Save as PNG
File file = new File("test.jpg");
ImageIO.write(image, "jpg", file);
}
catch (IOException e) {
e.printStackTrace();
}
}

public RenderedImage getImage(Component component) {
if (component == null) {
return null;
}
int width = 800;
int height = 600;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
component.setSize(800, 600);
//component.setPreferredSize(new Dimension(800,600));
Graphics2D g = image.createGraphics();
component.paintAll(g);
g.dispose();
image.flush();
return image;
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2007
Added on Dec 11 2006
0 comments
473 views