How to display a scaled down HTML page in an applet

RayGun
RayGun used Ask the Experts™
on
Hi experts

I'm creating a site wich allows people unfamiliar with web development to create functionable and nice looking homepages etc. But I have a problem. I want the users to see a scaled down image of the HTML page as a preview function and I was thinking that a java applet might be the way to go. When using Java in the past I have used JEditorPanes to display pure HTML pages but I cannot recall it having a scaling function. Does anyone have any ideas? I'm not very familiar with Java so some code will be highly appreciated and rewarded.

Thnx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
Can you render the JEditorPane to a BufferedImage, then scale the image?
Top Expert 2004

Commented:
Graphics g = image.getGraphics() ;
pane.paintComponent( g ) ;

then scale image

Author

Commented:
>>Can you render the JEditorPane to a BufferedImage, then scale the image?
Not sure I follow. What I need is for the applet to correctly display a scaled down page basically(a thumbnail), based only on an url to the page. Do you have to somehow convert the page into an image to scale it? Sorry, as I mentioned I'm no Java expert..:(
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Top Expert 2004

Commented:
> Do you have to somehow convert the page into an image to scale it?

Yes :-(

It's like IE, making the browser window smaller does not make the text smaller or the images shrink...

You would have to load the HTML into a JEditorPane, then render the JEditorPane to an Image, then scale and return that image...
Top Expert 2016

Commented:
>>Yes :-(


Why not just create a copy of the Graphics and set a scale transform?

Author

Commented:
>>Why not just create a copy of the Graphics and set a scale transform?

Could you ellaborate, prefferably with some code, I will award extra points accordingly.
Top Expert 2016

Commented:
Something like:

Graphics2D cloneAndScale(Graphics g) {
      Graphics cloned = g.create();
      Graphics2D clonedAndScaled = (Graphics2D)cloned;
                // 50%
      clonedAndScaled.scale(0.5, 0.5);
      return clonedAndScaled;
}

//................

yourScaledPanel.paint(cloneAndScale(yourOriginalPanel.getGraphics()));

Author

Commented:
ok, sounds reasonable. But it's not working properly. I'm getting a java.security.AccessConrtolException
This is my code:
public class Preview extends Applet
{
      JEditorPane jep,scep;
      URL url;

      public void init()
      {
            jep = new JEditorPane("text/html","html_text");
            jep.setEditable(false);
      }

      public void start()
      {
            try
            {
                  url = new URL("http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20723544.html");
                  jep.setPage(url);
            }
            catch(IOException e)
            {
                  e.printStackTrace();
                  jep.setContentType("text/html");
                  jep.setText("<html>Could not load page</html>");
            }
            scep.paint(cloneAndScale(jep.getGraphics()));
      }
      Graphics2D cloneAndScale(Graphics g)
      {
          Graphics cloned = g.create();
          Graphics2D clonedAndScaled = (Graphics2D)cloned;
          clonedAndScaled.scale(0.1, 0.1);
          return clonedAndScaled;
      }
}
I'm sorry I'm so helpless, I have limited experience with java and none with applets.
Top Expert 2004

Commented:
Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.....applet

The applet cant look at the url http://www.experts-exchange.com

Unless you sign it, applets can only talk to the server they are downloaded from (getCodeBase())

You could write a servlet to get the html for any URL, then return it to the applet?  Then the applet will be calling the servlet (which will be ok, as the servlet will be running on the same machine as the applet was downloaded from)...

Tim.
Top Expert 2016

Commented:
Tim's right about the security thing. You could also alter the policy file with policytool. http://java.sun.com/products/jdk/1.2/docs/tooldocs/solaris/policytool.html

Author

Commented:
I see. Actually the security shouldn't be an issue in the end product. Since the preview html pages can be placed in the same location as the applet, then it should work right? Other than that does the code seem to be the correct syntax to you, cause I did a quick test by placing the applet and a html file on the server and I got an error(shocker!). Applet could not be loaded.
Top Expert 2016

Commented:
Please post any stack trace from the plugin

Author

Commented:
I'm getting a class not found exception when running the applet from my local machine. This is the html:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<applet
code="http://192.168.123.11:105/Preview.class"
width = 200
height = 300>
</applet>
</BODY>
</HTML>

Author

Commented:
The class is there though
Top Expert 2016

Commented:
>>code="http://192.168.123.11:105/Preview.class"

Should be

code="Preview.class"

Make sure class file in same directory as html

Author

Commented:
>>code="Preview.class"

They are in same directory, but I'm getting access denied.
Top Expert 2016

Commented:
You must post the actual stack trace
Top Expert 2004
Commented:
do you have the correct JRE plugin for your browser?

sounds like you are compiling with 1.4, but only have the 1.1 standard browser plugin...

http://java.sun.com/getjava

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial