Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Customize the background color and transparency of generated im… #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gui.ava.html.image;

import gui.ava.html.Html2Image;
import gui.ava.html.image.util.FormatNameUtil;
import gui.ava.html.image.util.SynchronousHTMLEditorKit;
import gui.ava.html.link.LinkInfo;
Expand All @@ -23,12 +24,18 @@
*/
public class HtmlImageGenerator {
private JEditorPane editorPane;
private Color backgroundColor = Color.WHITE;
static final Dimension DEFAULT_SIZE = new Dimension(800, 800);

public HtmlImageGenerator() {
editorPane = createJEditorPane();
}

public HtmlImageGenerator(Color backgroundColor) {
this.backgroundColor = backgroundColor;
editorPane = createJEditorPane();
}

public ComponentOrientation getOrientation() {
return editorPane.getComponentOrientation();
}
Expand Down Expand Up @@ -133,14 +140,15 @@ public void saveAsImage(String file) {
public void saveAsImage(File file) {
BufferedImage image = getBufferedImage();

BufferedImage bufferedImageToWrite = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedImageToWrite.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
BufferedImage bufferedImageToWrite = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
bufferedImageToWrite.createGraphics().drawImage(image, 0, 0, this.backgroundColor, null);

final String formatName = FormatNameUtil.formatForFilename(file.getName());

try {
if (!ImageIO.write(bufferedImageToWrite, formatName, file))
if (!ImageIO.write(bufferedImageToWrite, formatName, file)) {
throw new IOException("No formatter for specified file type [" + formatName + "] available");
}
} catch (IOException e) {
throw new RuntimeException(String.format("Exception while saving '%s' image", file), e);
}
Expand All @@ -155,6 +163,7 @@ public Dimension getDefaultSize() {

public BufferedImage getBufferedImage() {
JFrame frame = new JFrame();
frame.getRootPane().setOpaque(false);
frame.setPreferredSize(editorPane.getPreferredSize());
frame.setUndecorated(true);
frame.add(editorPane);
Expand All @@ -176,6 +185,7 @@ protected JEditorPane createJEditorPane() {
final JEditorPane editorPane = new JEditorPane();
editorPane.setSize(getDefaultSize());
editorPane.setEditable(false);
editorPane.setBackground(this.backgroundColor);
final SynchronousHTMLEditorKit kit = new SynchronousHTMLEditorKit();
editorPane.setEditorKitForContentType("text/html", kit);
editorPane.setContentType("text/html");
Expand Down