java jeditorpane 图片_java图片嵌套合成

这是一个Java类,用于将包含HTML内容的JEditorPane转换为图像,并能够保存为HTML文件,同时生成链接映射。该类支持加载URL、设置页面大小、加载HTML字符串并保存为图片或HTML文件。
摘要由CSDN通过智能技术生成

packagecom.sr.biofunet.web.controller.utils;importjava.awt.ComponentOrientation;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Rectangle;importjava.awt.image.BufferedImage;importjava.beans.PropertyChangeEvent;importjava.beans.PropertyChangeListener;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.net.URL;importjava.util.Iterator;importjava.util.List;importjava.util.Map.Entry;importjavax.imageio.ImageIO;importjavax.swing.JEditorPane;public classHtmlImageGenerator {private JEditorPane editorPane = this.createJEditorPane();static final Dimension DEFAULT_SIZE = new Dimension(800, 800);publicHtmlImageGenerator() {

}publicComponentOrientation getOrientation() {return this.editorPane.getComponentOrientation();

}public voidsetOrientation(ComponentOrientation orientation) {this.editorPane.setComponentOrientation(orientation);

}publicDimension getSize() {return this.editorPane.getSize();

}public voidsetSize(Dimension dimension) {this.editorPane.setSize(dimension);

}public voidloadUrl(URL url) {try{this.editorPane.setPage(url);

}catch(IOException var3) {throw new RuntimeException(String.format("Exception while loading %s", newObject[]{url}), var3);

}

}public voidloadUrl(String url) {try{this.editorPane.setPage(url);

}catch(IOException var3) {throw new RuntimeException(String.format("Exception while loading %s", newObject[]{url}), var3);

}

}public voidloadHtml(String html) {this.editorPane.setText(html);this.onDocumentLoad();

}publicString getLinksMapMarkup(String mapName) {

StringBuilder markup= newStringBuilder();

markup.append("\n");

Iterator i$= this.getLinks().iterator();while(i$.hasNext()) {

LinkInfo link=(LinkInfo)i$.next();

List bounds=link.getBounds();

Iterator i$1 =bounds.iterator();while(i$1.hasNext()) {

Rectangle bound= (Rectangle)i$1.next();int x1 = (int)bound.getX();int y1 = (int)bound.getY();int x2 = (int)((double)x1 +bound.getWidth());int y2 = (int)((double)y1 +bound.getHeight());

markup.append(String.format("

Iterator i$2 =link.getAttributes().entrySet().iterator();while(i$2.hasNext()) {

Entry entry= (Entry)i$2.next();

String attName=(String)entry.getKey();

String value=(String)entry.getValue();

markup.append(" ").append(attName).append("=\"").append(value.replace("\"", """)).append("\"");

}

markup.append(">\n");

}

}

markup.append("\n");returnmarkup.toString();

}public ListgetLinks() {

LinkHarvester harvester= new LinkHarvester(this.editorPane);returnharvester.getLinks();

}public voidsaveAsHtmlWithMap(String file, String imageUrl) {this.saveAsHtmlWithMap(newFile(file), imageUrl);

}public voidsaveAsHtmlWithMap(File file, String imageUrl) {

FileWriter writer= null;try{

writer= newFileWriter(file);

writer.append(""-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");

writer.append("\n

\n");

writer.append("

\n");

String e= this.getLinksMapMarkup("map");

writer.write(e);

writer.append("p

writer.append(imageUrl);

writer.append("\"/>\n");

writer.append("\n");

}catch(IOException var12) {throw new RuntimeException(String.format("Exception while saving \'%s\' html file", newObject[]{file}), var12);

}finally{if(writer != null) {try{

writer.close();

}catch(IOException var11) {

;

}

}

}

}public voidsaveAsImage(String file) {this.saveAsImage(newFile(file));

}public voidsaveAsImage(File file) {

BufferedImage img= this.getBufferedImage();try{

String e=FormatNameUtil.formatForFilename(file.getName());

ImageIO.write(img, e, file);

}catch(IOException var4) {throw new RuntimeException(String.format("Exception while saving \'%s\' image", newObject[]{file}), var4);

}

}protected voidonDocumentLoad() {

}publicDimension getDefaultSize() {returnDEFAULT_SIZE;

}publicBufferedImage getBufferedImage() {

Dimension prefSize= this.editorPane.getPreferredSize();

BufferedImage img= new BufferedImage(prefSize.width, this.editorPane.getPreferredSize().height, 2);

Graphics graphics=img.getGraphics();this.editorPane.setSize(prefSize);this.editorPane.paint(graphics);returnimg;

}protectedJEditorPane createJEditorPane() {

JEditorPane editorPane= newJEditorPane();

editorPane.setSize(this.getDefaultSize());

editorPane.setEditable(false);

SynchronousHTMLEditorKit kit= newSynchronousHTMLEditorKit();

editorPane.setEditorKitForContentType("text/html", kit);

editorPane.setContentType("text/html");

editorPane.addPropertyChangeListener(newPropertyChangeListener() {public voidpropertyChange(PropertyChangeEvent evt) {if(evt.getPropertyName().equals("page")) {

HtmlImageGenerator.this.onDocumentLoad();

}

}

});returneditorPane;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值