I wrote codes that loads images into JEditorPane
using HTMLEditorKit
. I know how to resize the image using HTML. But the problem is the loaded image losses quality. I am trying to find ways to resize without losing quality.

- 168,117
- 40
- 217
- 433

- 289
- 2
- 7
-
Maybe like [this](http://stackoverflow.com/a/4216635/230513)? – trashgod Jul 03 '15 at 16:42
-
Well , I use insertHTML() method of htmlEditorKit . I need the image that JEditorPane shows , so that I can resize it. But I can't find how. – Rahul Jul 03 '15 at 16:47
-
load the image into BufferedImage and then resize as said in the linkby trashgod then put it to your htmlEditorkit – Madhan Jul 03 '15 at 20:26
-
1*"I know how to resize the image using HTML."* Do you know how to resize an image smoothly using pure Java code? If not, see the advice of @trashgod. Once you've sorted that out, you'll need to extend the `HTMLEditorKit` and override the `public View create(Element element) ` method of the `HTMLFactory` to use the new functionality (based on what I was just looking at in an old project that added support for the `applet` element). – Andrew Thompson Jul 03 '15 at 22:56
2 Answers
As Andrew Thompson suggested ,
extend the HTMLEditorKit
and override public View create(Element element)
of HTMLFactory
.
extend from ImageView
and override the public void paint(Graphics g, Shape a)
method. Get the image and resize it.
getScaledInstance(WIDTH, HEIGHT, Image.SCALE_AREA_AVERAGING)
with your favourite scaling HINT
and finally draw.

- 289
- 2
- 7
I'm unclear on the exact use-case the OP is after, but I've used the following simpler alternative for an HTML help viewer – override the paint()
method of the JEditorPane
to set a rendering hint:
JEditorPane editor = new JEditorPane() {
@Override public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
super.paint(g2);
g2.dispose();
} else {
// Print and other non-Graphics2D contexts.
super.paint(g);
}
}
};
This provides nice and easy improvement with non-integral scaling factor hidpi screens, for example. The quality would still be suboptimal if an image is scaled more than half down of the original. For that use-case a custom HTMLEditorKit
/ HTMLVactory
producing a custom ImageView
would be necessary, as others have pointed out.

- 116
- 1
- 8