<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silveira Neto &#187; imageview</title>
	<atom:link href="http://silveiraneto.net/tag/imageview/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>JavaFX, Acessando Recursos de Dentro do JAR</title>
		<link>http://silveiraneto.net/2009/04/10/javafx-acessando-recursos-de-dentro-do-jar/</link>
		<comments>http://silveiraneto.net/2009/04/10/javafx-acessando-recursos-de-dentro-do-jar/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:59:27 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[português]]></category>
		<category><![CDATA[fileinputstream]]></category>
		<category><![CDATA[imageview]]></category>
		<category><![CDATA[JAR]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[sanduíche]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2477</guid>
		<description><![CDATA[Translation: there&#8217;s a English version of this article. Para algumas classes como o javafx.scene.image.Image é fácil abrir imagens de uma localidade remotada com: ImageView &#123; image: Image &#123; url: &#34;http://example.com/minhaFigura.png&#34; &#125; &#125; ou uma imagem local com a constante __DIR: ImageView &#123; image: Image &#123; url: &#34;{__DIR__}/minhaFigura.png&#34; &#125; &#125; Mas para outras classes abrir recursos [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><img src="http://silveiraneto.net/wp-content/uploads/2008/10/us.png" alt="us flag" /><small><strong>Translation:</strong> there&#8217;s a <a href="http://silveiraneto.net/2009/04/10/javafx-getting-resources-of-inside-your-jar/">English version</a> of this article.</small></p></blockquote>
<p>Para algumas classes como o <a href="http://java.sun.com/javafx/1/docs/api/javafx.scene.image/javafx.scene.image.Image.html">javafx.scene.image.Image</a> é fácil abrir imagens de uma localidade remotada com:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://example.com/minhaFigura.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>ou uma imagem local com a constante __DIR:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">ImageView <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{__DIR__}/minhaFigura.png&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Mas para outras classes abrir recursos internos (de dentro do próprio arquivo jar) não é tão direto. Por exemplo, no artigo <a title="JavaFX XML" href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">Parsing a XML Sandwich with JavaFX</a> eu tive que colocar o arquvio XML dentro de um diretório temporário. Uma maneira mais elegante teria sido:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">handlexml</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.data.pull.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.ext.swing.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Resource<span style="color: #009900;">&#123;</span>
    function getUrl<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    function getStream<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">:</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var list <span style="color: #339933;">=</span> SwingList <span style="color: #009900;">&#123;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">600</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#125;</span>
&nbsp;
var myparser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
    documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
    onEvent<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var item <span style="color: #339933;">=</span> SwingListItem <span style="color: #009900;">&#123;</span>text<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;event {e}&quot;</span><span style="color: #009900;">&#125;</span>;
        insert item into list.<span style="color: #006633;">items</span>;
    <span style="color: #009900;">&#125;</span>
    input<span style="color: #339933;">:</span> Resource<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>.<span style="color: #006633;">getStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my.xml&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
myparser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Map&quot;</span>
    scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> list
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Com um simples arquivo XML chamadov<em>my.xml</em> dentro do seu pacote.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bread</span> <span style="color: #000066;">sesame</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catchup</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hamburguer</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cheese</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;chedar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maionese</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lettuce</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bread<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p><center><img src="http://silveiraneto.net/wp-content/uploads/2009/04/fileplace.png" alt="fileplace" title="fileplace" width="210" height="107" class="alignnone size-full wp-image-2474" /></center><br />
E temos os mesmos resultados de antes, mas com todos seus aquivos dentro de seus Jars.<br />
<center><img alt="" src="http://silveiraneto.net/wp-content/uploads/2008/12/javafx_xml_sandwich.png" title="XML Sandwich" class="alignnone" width="625" height="354" /></center></p>
<p>Referencias:</p>
<ul>
<ol>Artigo do Hildeberto <a href="http://www.hildeberto.com/2008/12/acessing-resources-inside-of-jar-files.html">Acessing Resources Inside of JAR Files</a></ol>
<ol><a href="http://java.sun.com/javafx/1/tutorials/core/">JavaFX Core Tutorial</a></ol>
<ol>Fórum do JavaFX, <a href="http://forums.sun.com/thread.jspa?threadID=5367468&#038;tstart=2">esta</a> e <a href="http://forums.sun.com/thread.jspa?threadID=5340352">esta</a> thread.</ol>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/04/10/javafx-acessando-recursos-de-dentro-do-jar/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

