The Artima Developer Community
Sponsored Link

Java Answers Forum
Download image in a java program

1 reply on 1 page. Most recent reply: Apr 8, 2002 9:18 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Galuc

Posts: 1
Nickname: galuc
Registered: Apr, 2002

Download image in a java program Posted: Apr 6, 2002 2:19 PM
Reply to this message Reply
Advertisement
Hi!

I have a problem. I view a html site in a EditorPane, and I want to download an image.
But I don't now how to get the URL from the image.


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Download image in a java program Posted: Apr 8, 2002 9:18 AM
Reply to this message Reply
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

Flat View: This topic has 1 reply on 1 page
Topic: Customized HTMLEditorKit Previous Topic   Next Topic Topic: having trouble with java applets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use