Thomas SMETS
Posts: 307
Nickname: tsmets
Registered: Apr, 2002
|
|
Re: Download image in a java program
|
Posted: Apr 8, 2002 9:18 AM
|
|
Does this example help ? Unless you cannot use Swing, this should do !
import java.awt.BorderLayout;
import java.io.Reader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
/**
* This class just demonstrate how to render an HTML document in a
* JFrame.
* Three different cases are proposed.
*
* @see http://manning.com/sbe/files/uts2/Chapter27html/Chapter27.htm
* @see http://www.ibiblio.org/javafaq/slides/sd2000east/webclient/25.html
* @see http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/
*
* See respective authors for Copyright issues.
* This document is provided "as is".
* @see javax.swing.text.html.HTMLDocument
*
* @version 0.15 - final
* @author <a href="mailto:tsmets@altern.org">Thomas SMETS</a>
*
*/
public class HTMLRendering
extends JFrame
{
Document d = null;
JEditorPane t = null;
Category log = null;
HTMLRendering (String aName)
{
log = Category.getInstance(HTMLRendering.class);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
setTitle( aName );
t = new JEditorPane ();
t.setEditable(false);
JScrollPane p = new JScrollPane (t);
p.getViewport().add (t);
p.setBounds (0, 0, 150,150);
p.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(p, BorderLayout.CENTER);
setSize(350, 750);
setVisible(true);
log.info ("HTMLRendering created");
}
public void initializeURL (String anURL)
{
log.info ("HTMLRendering::initializeURL ()");
try
{
log.info("Loading the URL");
t.setPage(anURL);
log.info("Loaded");
}
catch (Exception ex)
{
log.warn("Exception " + ex.getClass().getName(), ex);
}
return;
}
public void initializeFile (String aFileName)
{
log.info ("HTMLRendering::initializeFile");
try
{
FileReader fr = new FileReader( aFileName );
t.read(fr, aFileName);
} catch (IOException ioe)
{
log.warn("Exception thrown " + ioe.getClass(), ioe);
}
}
public void initializeHTMLFile (String aFileName)
{
log.info ("HTMLRendering::initializeHTMLFile");
try
{
t.setEditorKit(new HTMLEditorKit());
FileReader fr = new FileReader( aFileName );
t.read(fr, aFileName);
} catch (IOException ioe)
{
log.warn("Exception thrown " + ioe.getClass(), ioe);
}
}
public static void main (String[] args)
{
BasicConfigurator.resetConfiguration();
BasicConfigurator.configure();
HTMLRendering html = new HTMLRendering ("Example - HTML file");
html.initializeHTMLFile ("SimpleHTML.html");
html = new HTMLRendering ("Example - HTML from URL");
html.initializeURL ("http://tsmets.lautre.net");
html = new HTMLRendering ("Example - HTML Viewed as pure text");
html.initializeFile ("SimpleHTML.html");
}
}
Thomas, tsmets @ lautre . net
|
|