<?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; Java</title>
	<atom:link href="http://silveiraneto.net/tag/java/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>Android screen height and width</title>
		<link>http://silveiraneto.net/2011/03/12/android-screen-height-and-width/</link>
		<comments>http://silveiraneto.net/2011/03/12/android-screen-height-and-width/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 00:30:26 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[screensize]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3705</guid>
		<description><![CDATA[Context ctx = getContext&#40;&#41;; Display display = &#40;&#40;WindowManager&#41;ctx.getSystemService&#40;ctx.WINDOW_SERVICE&#41;&#41;.getDefaultDisplay&#40;&#41;; int width = display.getWidth&#40;&#41;; int height = display.getHeight&#40;&#41;; Yes, there are easier ways to retrieve the screen width on Android but there are cases that this long code is the only solution. You may already have the Context. WindowManager or the Display and so it would be [...]]]></description>
			<content:encoded><![CDATA[<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #003399;">Context</span> ctx <span style="color: #339933;">=</span> getContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
Display display <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>WindowManager<span style="color: #009900;">&#41;</span>ctx.<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span>ctx.<span style="color: #006633;">WINDOW_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDefaultDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">int</span> width <span style="color: #339933;">=</span> display.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">int</span> height <span style="color: #339933;">=</span> display.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>Yes, there are easier ways to retrieve the screen width on Android but there are cases that this long code is the only solution. You may already have the Context. WindowManager or the Display and so it would be smaller, but this code is more general.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2011/03/12/android-screen-height-and-width/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java: invoking a method by name</title>
		<link>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/</link>
		<comments>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:29:33 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3364</guid>
		<description><![CDATA[import java.lang.reflect.*; &#160; public class Foo &#123; public void bar&#40;int param&#41;&#123; System.out.println&#40;param&#41;; &#125; &#160; public static void main&#40;String args&#91;&#93;&#41;&#123; Object f = new Foo&#40;&#41;; try &#123; Method m = f.getClass&#40;&#41;.getMethod&#40;&#34;bar&#34;, int.class&#41;; m.invoke&#40;f, 42&#41;; &#125; catch &#40;Exception e&#41;&#123; System.err.println&#40;e&#41;; &#125; &#125; &#160; &#125; $ java Foo 42]]></description>
			<content:encoded><![CDATA[<p><center><br />
<a href="http://silveiraneto.net/wp-content/uploads/2010/04/Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood.jpg"><img src="http://silveiraneto.net/wp-content/uploads/2010/04/Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood.jpg" alt="" title="Film_star_Helen_Twelvetrees_ca_1936_7_photograph_by_Sam_Hood" width="388" height="500" class="alignnone size-full wp-image-3365" /></a></center></p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.*</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Object</span> f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Method</span> m <span style="color: #339933;">=</span> f.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bar&quot;</span>, <span style="color: #000066; font-weight: bold;">int</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>;
			m.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>f, <span style="color: #cc66cc;">42</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<blockquote><p>$ java Foo<br />
42
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/04/29/java-invoking-a-method-by-name/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>calling commands in Java</title>
		<link>http://silveiraneto.net/2010/04/08/calling-commands-in-java/</link>
		<comments>http://silveiraneto.net/2010/04/08/calling-commands-in-java/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 16:09:11 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[getruntime]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[runtime]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3291</guid>
		<description><![CDATA[I don&#8217;t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands. import java.io.*; import java.util.*; public class Exec &#123; public static void main&#40;String args&#91;&#93;&#41; throws IOException &#123; Process proc = Runtime.getRuntime&#40;&#41;.e xec&#40;args&#41;; BufferedReader [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span>;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Exec <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">Process</span> proc <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">e</span> xec<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span>;
      <span style="color: #003399;">BufferedReader</span> br <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>proc.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #003399;">String</span> line;
      <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> br.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Usage:</p>
<blockquote><p>java Exec VALID_COMMAND</p></blockquote>
<p>Example: </p>
<blockquote><p>$ java Exec echo hello<br />
hello</p></blockquote>
<p>ps: I had to write &#8220;e xec&#8221; instead of exec because it was triggering some very strange security protection in the blog engine here. If you need to compile this code change that. =P Also there&#8217;s no error handling, you should pass a valid command when executing this code.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/04/08/calling-commands-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterating over a HashMap</title>
		<link>http://silveiraneto.net/2010/04/08/iterating-over-a-hashmap/</link>
		<comments>http://silveiraneto.net/2010/04/08/iterating-over-a-hashmap/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 14:22:33 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[enhanced loop]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[hashmap]]></category>
		<category><![CDATA[iterating]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3288</guid>
		<description><![CDATA[Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent. import java.util.HashMap; import java.util.Map; &#160; class Foo &#123;&#125; &#160; public class Main &#123; &#160; public static void main&#40;String args&#91;&#93;&#41;&#123; Map&#60;Byte, Foo&#62; mHash; &#160; mHash = new HashMap&#60;Byte, Foo&#62;&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      Map<span style="color: #339933;">&lt;</span>Byte, Foo<span style="color: #339933;">&gt;</span> mHash;
&nbsp;
      mHash <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>Byte, Foo<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      mHash.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
      mHash.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">2</span>, <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
      mHash.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">3</span>, <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
      <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Foo f<span style="color: #339933;">:</span> mHash.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/04/08/iterating-over-a-hashmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting enviroment information on Android</title>
		<link>http://silveiraneto.net/2010/03/16/getting-enviroment-information-on-android/</link>
		<comments>http://silveiraneto.net/2010/03/16/getting-enviroment-information-on-android/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 13:18:32 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[who am i]]></category>
		<category><![CDATA[whoami]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3226</guid>
		<description><![CDATA[This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users. Download: WhoAmI.tar.bz2 &#8211; Eclipse project. It&#8217;s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions. WhoAmI.apk &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple program I wrote called <strong>Who Am I</strong> that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users.</p>
<p style="text-align: center;"><img class="size-full wp-image-3227 aligncenter" title="whoami" src="http://silveiraneto.net/wp-content/uploads/2010/03/whoami.png" alt="" width="320" height="475" /></p>
<p><strong>Download</strong>:</p>
<ul>
<li><a href="http://silveiraneto.net/downloads/WhoAmI.tar.bz2">WhoAmI.tar.bz2</a> &#8211; Eclipse project. It&#8217;s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions.</li>
<li><a href="http://silveiraneto.net/downloads/WhoAmI.apk">WhoAmI.apk</a> &#8211; Application installation Android package.</li>
</ul>
<p>Main Activity source code:</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;">net.silveiraneto.whoami</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Build</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WhoAmI <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> EditText mEditor;
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span>;
&nbsp;
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">whoami</span><span style="color: #009900;">&#41;</span>;
&nbsp;
        mEditor <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">editor</span><span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> properties <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.BOARD&quot;</span>, Build.<span style="color: #006633;">BOARD</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.BRAND&quot;</span>, Build.<span style="color: #006633;">BRAND</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.CPU_ABI&quot;</span>, Build.<span style="color: #006633;">CPU_ABI</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.DEVICE&quot;</span>, Build.<span style="color: #006633;">DEVICE</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.DISPLAY&quot;</span>, Build.<span style="color: #006633;">DISPLAY</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.FINGERPRINT&quot;</span>, Build.<span style="color: #006633;">FINGERPRINT</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.HOST&quot;</span>, Build.<span style="color: #006633;">HOST</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.ID&quot;</span>, Build.<span style="color: #006633;">ID</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.MANUFACTURER&quot;</span>, Build.<span style="color: #006633;">MANUFACTURER</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.MODEL&quot;</span>, Build.<span style="color: #006633;">MODEL</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.PRODUCT&quot;</span>, Build.<span style="color: #006633;">PRODUCT</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.TAGS&quot;</span>, Build.<span style="color: #006633;">TAGS</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.TIME&quot;</span>, Build.<span style="color: #006633;">TIME</span><span style="color: #009900;">&#125;</span>,
        	<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Build.USER&quot;</span>, Build.<span style="color: #006633;">USER</span><span style="color: #009900;">&#125;</span>,
        <span style="color: #009900;">&#125;</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> prop<span style="color: #339933;">:</span> properties<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        	mEditor.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, prop<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>, prop<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>And its Android Manifest:</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;manifest</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;net.silveiraneto.whoami&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span> <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;Who Am I&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;WhoAmI&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.action.MAIN&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.LAUNCHER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/03/16/getting-enviroment-information-on-android/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>The Caps Lock Java Socket Server</title>
		<link>http://silveiraneto.net/2010/02/27/the-caps-lock-java-socket-server/</link>
		<comments>http://silveiraneto.net/2010/02/27/the-caps-lock-java-socket-server/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 00:39:39 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[caps lock]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[upcase]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3204</guid>
		<description><![CDATA[Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior. Features: A client should enter a string and the server would answer the same string, with each symbol in up case, when possible. Default port at 8080. One [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-3205 aligncenter" title="caps lock megaphone yelling" src="http://silveiraneto.net/wp-content/uploads/2010/02/capslock.jpg" alt="" width="390" height="262" /></p>
<p>Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior.</p>
<p>Features:</p>
<ul>
<li>A client should enter a string and the server would answer the same string, with each symbol in up case, when possible.</li>
<li>Default port at 8080.</li>
<li>One client at time.</li>
<li>No multi threading. I said its a simple server.</li>
</ul>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStreamWriter</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.ServerSocket</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.Socket</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Server <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> <span style="color: #000000; font-weight: bold;">DEFAULT</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8080</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Server<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">DEFAULT</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Server<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> port<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">ServerSocket</span> sock;
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			sock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ServerSocket</span><span style="color: #009900;">&#40;</span>port<span style="color: #009900;">&#41;</span>;
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Listening on port %d.&quot;</span>, port<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003399;">Socket</span> client <span style="color: #339933;">=</span> sock.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A new connection was accepted.&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
					<span style="color: #003399;">BufferedReader</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span>
							<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>client.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #003399;">OutputStreamWriter</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">OutputStreamWriter</span><span style="color: #009900;">&#40;</span>client
							.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
&nbsp;
					<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>input.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;exit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						input <span style="color: #339933;">=</span> in.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
						<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>input.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shutdown&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span>0<span style="color: #009900;">&#41;</span>;
						<span style="color: #009900;">&#125;</span>
						out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>input.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
						out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #009900;">&#125;</span>
&nbsp;
					in.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
					out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
					client.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection closed.&quot;</span><span style="color: #009900;">&#41;</span>;
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> npe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection closed by client.&quot;</span><span style="color: #009900;">&#41;</span>;
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> ioe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>ioe<span style="color: #009900;">&#41;</span>;
			<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">new</span> Server<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Usage:</p>
<blockquote><p>$ javac Server.java<br />
$ java Server<br />
Listening on port 8080.
</p></blockquote>
<p>In another terminal:</p>
<blockquote><p>$ telnet localhost 8080<br />
Trying ::1&#8230;<br />
Connected to localhost.<br />
Escape character is &#8216;^]&#8217;.<br />
hi<br />
HI<br />
The quick brown fox jumps over the lazy dog.<br />
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.<br />
exi<br />
EXI<br />
exit<br />
EXIT<br />
Connection closed by foreign host.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/02/27/the-caps-lock-java-socket-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Beware the locale</title>
		<link>http://silveiraneto.net/2010/02/22/beware-the-locale/</link>
		<comments>http://silveiraneto.net/2010/02/22/beware-the-locale/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 20:16:12 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[locale]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[teste unitário]]></category>
		<category><![CDATA[toString]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3189</guid>
		<description><![CDATA[Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C&#8217;s like printf formatter. @Override public String toString&#40;&#41; &#123; return String.format&#40;&#34;VO[a: %.1f, b: %.1f, c: %.1f]&#34;, a, b, a+b&#41;; &#125; %.1f means a float with one digit precision after the dot separator. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.flickr.com/photos/seeminglee/4139558009/"><img class="aligncenter" src="http://farm3.static.flickr.com/2699/4139558009_4a8c9bea31.jpg" alt="See-ming Lee 李思明 SML Photo" width="500" height="333" /></a></p>
<p>Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C&#8217;s like printf formatter.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</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: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;VO[a: %.1f, b: %.1f, c: %.1f]&quot;</span>, a, b, a<span style="color: #339933;">+</span>b<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>%.1f means a float with one digit precision after the dot separator. The code produces something like:</p>
<pre><strong>VO[a: 1.0, b: 2.0, c: 3.0]</strong></pre>
<p>The problem arises when running a JUnit test on this method wrote using a regular expression to extract the values from the String to test it correctness. We cannot assume that the dot will be always the separator for displaying a float value, in my locale pt_BR would be a comma. So the output would be:</p>
<pre><strong>VO[a: 1,0, b: 2,0, c: 3,0]</strong></pre>
<p>For a predictable output we can set a <a title="Java Locale API" href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html">Locale</a> for String.format:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #003399;">Locale</span> en <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Locale</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>en, <span style="color: #0000ff;">&quot;VO[a: %.1f, b: %.1f, c: %.1f]&quot;</span>, a, b, a<span style="color: #339933;">+</span>b<span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>So it will always use the dot as common separator. Of course you should follow and respect the localization and internationalization efforts in others moments but in this toString case we are using it internally for debug and unitary testing so we can set a English default locale for safety reasons.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/02/22/beware-the-locale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Font List</title>
		<link>http://silveiraneto.net/2010/01/05/java-font-list/</link>
		<comments>http://silveiraneto.net/2010/01/05/java-font-list/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 21:10:00 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[AWT]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[Larabie]]></category>
		<category><![CDATA[Larabie Fonts]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3102</guid>
		<description><![CDATA[Here&#8217;s a program that lists fonts available in your JVM. You can also set the environment variable JAVA_FONTS to specify the font directory. import java.awt.GraphicsEnvironment; &#160; public class ListFonts &#123; public static void main&#40;String args&#91;&#93;&#41;&#123; GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment&#40;&#41;; for&#40;String font:e.getAvailableFontFamilyNames&#40;&#41;&#41;&#123; System.out.println&#40;font&#41;; &#125; &#125; &#125; By using pipes you can count how many fonts you [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a program that lists fonts available in your JVM. You can also set the environment variable <a title="IBM JVM Enviromment variables" href="http://publib.boulder.ibm.com/infocenter/javasdk/v5r0/index.jsp?topic=/com.ibm.java.doc.diagnostics.50/diag/appendixes/env_var/env_jvm.html">JAVA_FONTS</a> to specify the font directory.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.GraphicsEnvironment</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ListFonts <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">GraphicsEnvironment</span> e <span style="color: #339933;">=</span> <span style="color: #003399;">GraphicsEnvironment</span>.<span style="color: #006633;">getLocalGraphicsEnvironment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> font<span style="color: #339933;">:</span>e.<span style="color: #006633;">getAvailableFontFamilyNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>font<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>By using pipes you can count how many fonts you have:</p>
<blockquote><p>java ListFonts|wc -l</p></blockquote>
<p>On my Ubuntu machine here I got 556 because I use those excellent, free and indispensable <a href="http://www.larabiefonts.com/">Larabie Fonts</a>.</p>
<p>For looking up for a font with &#8220;sans&#8221; in its name, using a case insensitive grep:</p>
<blockquote><p>java ListFonts|grep -i &#8220;sans&#8221;</p></blockquote>
<p>I get a list like this:</p>
<blockquote><p>
DejaVu Sans<br />
DejaVu Sans Condensed<br />
DejaVu Sans Light<br />
DejaVu Sans Mono<br />
FreeSans
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2010/01/05/java-font-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tiled TMX Map Loader for Pygame</title>
		<link>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/</link>
		<comments>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 10:15:48 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Tiled]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3063</guid>
		<description><![CDATA[I&#8217;m using the Tiled Map Editor for a while, I even wrote that tutorial about it. It&#8217;s a general purpose tile map editor, written in Java but now migrating to C++ with Qt, that can be easily used with my set of free pixelart tiles. A map done with Tiled is stored in a file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using the <a href="http://mapeditor.org/">Tiled Map Editor</a> for a while, I even wrote <a href="http://silveiraneto.net/2009/01/11/game-map-edition-using-tiled/">that tutorial about it</a>. It&#8217;s a general purpose tile map editor, written in Java but now migrating to C++ with Qt, that can be easily used with <a title="pixel art work" href="http://silveiraneto.net/tag/pixelart">my set of free pixelart tiles</a>.</p>
<p><center><a href="../wp-content/uploads/2009/01/map_editor_tiles_tileset_game-deveopment.png"><img title="map editor tiles tileset game deveopment" src="../wp-content/uploads/2009/01/map_editor_tiles_tileset_game-deveopment-500x343.png" alt="map editor tiles tileset game deveopment" width="500" height="343" /></a></center></p>
<p>A map done with Tiled is stored in a file with TMX extension. It&#8217;s just a XML file, easy to understand.</p>
<p>As I&#8217;m creating a map loader for my owns purposes, the procedure I&#8217;m doing here works we need some simplifications. I&#8217;m handling orthogonal maps only. I&#8217;m not supporting tile properties as well. I also don&#8217;t want to handle base64 and zlib encoding in this version, so in the Tiled editor, go at the menu <em>Edit → Preferences</em> and in the <em>Saving</em> tab unmark the options &#8220;Use binary encoding&#8221; and &#8220;Compress Layer Data (gzip)&#8221;, like this:</p>
<p style="text-align: center;"><img class="size-full wp-image-3064 aligncenter" title="Tiled Preferences Window" src="http://silveiraneto.net/wp-content/uploads/2009/12/Tiled_preferences.png" alt="Tiled Preferences Window" width="370" height="309" /></p>
<p>When saving a map it will produce a TMX file like this:</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: #00bbdd;">&lt;!DOCTYPE map SYSTEM &quot;http://mapeditor.org/dtd/1.0/map.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">orientation</span>=<span style="color: #ff0000;">&quot;orthogonal&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">tilewidth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">tileheight</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Author&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Silveira Neto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Year&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2009&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tileset</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mytiles&quot;</span> <span style="color: #000066;">firstgid</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">tilewidth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">tileheight</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;image</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;free_tileset_version_10.png&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layer</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;grass&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;261&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;260&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    ...
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;160&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p>For processing it on Python I&#8217;m using the <a href="http://docs.python.org/whatsnew/2.0.html#sax2-support">event oriented SAX approach for XML</a>. So I create a ContentHandler that handles events the start and end of XML elements. In the first element, map, I know enough to create a <a href="http://www.pygame.org/docs/ref/surface.html">Pygame surface</a> with the correct size. I&#8217;m also storing the map properties so I can use it later for add some logics or effects on the map. After that we create a instance of the Tileset class from where we will get the each tile by an gid number. Each layer has it&#8217;s a bunch of gids in the correct order. So it&#8217;s enough information to mount and draw a map.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Author: Silveira Neto</span>
<span style="color: #808080; font-style: italic;"># License: GPLv3</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>, pygame
<span style="color: #ff7700;font-weight:bold;">from</span> pygame.<span style="color: #008000;">locals</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pygame <span style="color: #ff7700;font-weight:bold;">import</span> Rect
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span> <span style="color: #ff7700;font-weight:bold;">import</span> sax
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Tileset:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">file</span>, tile_width, tile_height<span style="color: black;">&#41;</span>:
        image = pygame.<span style="color: black;">image</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>.<span style="color: black;">convert_alpha</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> image:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Error creating new Tileset: file %s not found&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">file</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = tile_width
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = tile_height
        <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>image.<span style="color: black;">get_height</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>/<span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> column <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>image.<span style="color: black;">get_width</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>/<span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span><span style="color: black;">&#41;</span>:
                pos = Rect<span style="color: black;">&#40;</span>
                        column<span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>,
                        line<span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>,
                        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>,
                        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> <span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>image.<span style="color: black;">subsurface</span><span style="color: black;">&#40;</span>pos<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_tile<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, gid<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span><span style="color: black;">&#91;</span>gid<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TMXHandler<span style="color: black;">&#40;</span>sax.<span style="color: black;">ContentHandler</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">width</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">height</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">lines</span>  = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">properties</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">image</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> startElement<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, attrs<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># get most general map informations and create a surface</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> name == <span style="color: #483d8b;">'map'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'width'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">lines</span>  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'height'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tilewidth'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tileheight'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">width</span> = <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">height</span> = <span style="color: #008000;">self</span>.<span style="color: black;">lines</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">image</span> = pygame.<span style="color: black;">Surface</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">height</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">convert</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># create a tileset</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name==<span style="color: #483d8b;">&quot;image&quot;</span>:
            source = attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'source'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span> = Tileset<span style="color: black;">&#40;</span>source, <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># store additional properties.</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'property'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">properties</span><span style="color: black;">&#91;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'value'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># starting counting</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'layer'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">line</span> = 0
            <span style="color: #008000;">self</span>.<span style="color: black;">column</span> = 0
        <span style="color: #808080; font-style: italic;"># get information of each tile and put on the surface using the tileset</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'tile'</span>:
            gid = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gid'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> gid <span style="color: #66cc66;">&lt;</span>0: gid = 0
            tile = <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span>.<span style="color: black;">get_tile</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
            pos = <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">column</span><span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">line</span><span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">image</span>.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>tile, pos<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #008000;">self</span>.<span style="color: black;">column</span> += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">column</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #008000;">self</span>.<span style="color: black;">columns</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">column</span> = 0
                <span style="color: #008000;">self</span>.<span style="color: black;">line</span> += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># just for debugging</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> endDocument<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">height</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">properties</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">image</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">!</span>=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Usage:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>{0} filename'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
    pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">800</span>, <span style="color: #ff4500;">480</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span> = sax.<span style="color: black;">make_parser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    tmxhandler = TMXHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">setContentHandler</span><span style="color: black;">&#40;</span>tmxhandler<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> event <span style="color: #ff7700;font-weight:bold;">in</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: #008000;">type</span> == QUIT:
                <span style="color: #ff7700;font-weight:bold;">return</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> event.<span style="color: #008000;">type</span> == KEYDOWN <span style="color: #ff7700;font-weight:bold;">and</span> event.<span style="color: black;">key</span> == K_ESCAPE:
                <span style="color: #ff7700;font-weight:bold;">return</span>
        screen.<span style="color: black;">fill</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        screen.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>tmxhandler.<span style="color: black;">image</span>, <span style="color: black;">&#40;</span>0,0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        pygame.<span style="color: black;">display</span>.<span style="color: black;">flip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">delay</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>/<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>Here is the result for opening a four layers map file:</p>
<p><center><a href="http://silveiraneto.net/wp-content/uploads/2009/12/netbeans_python_openning_map.png"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/netbeans_python_openning_map-500x375.png" alt="netbeans python openning map" title="netbeans python openning map" width="500" height="375" class="alignnone size-medium wp-image-3067" /></a> </center></p>
<p>That&#8217;s it. You can get this code and adapt for your game because next versions will be a lot more coupled for my own purposes and not so general.</p>
<p><strong>Download:</strong><a href="http://silveiraneto.net/downloads/maploader.tar.bz2"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/package_32x32.png" alt="package" title="package" width="32" height="32" class="alignnone size-full wp-image-3069" />maploader.tar.bz2</a> It&#8217;s the Netbeans 6.7 (Python EA 2) project file but that can be opened or used with another IDE or without one. Also contains the village.tmx map and the tileset.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Java: Accessing Private Members</title>
		<link>http://silveiraneto.net/2009/11/14/java-accessing-private-members/</link>
		<comments>http://silveiraneto.net/2009/11/14/java-accessing-private-members/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 07:27:14 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Douglas Adams]]></category>
		<category><![CDATA[Field]]></category>
		<category><![CDATA[getclass]]></category>
		<category><![CDATA[getDeclaredField]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Object-Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2942</guid>
		<description><![CDATA[Using reflection to change the accessibility of a private object field and access it at runtime. import java.lang.reflect.Field; &#160; class Life &#123; private int meaning = 42; &#125; &#160; class Hack &#123; public static void main&#40;String args&#91;&#93;&#41;&#123; Life life = new Life&#40;&#41;; try &#123; Field field = life.getClass&#40;&#41;.getDeclaredField&#40;&#34;meaning&#34;&#41;; field.setAccessible&#40;true&#41;; System.out.println&#40;field.get&#40;life&#41;&#41;; &#125; catch &#40;NoSuchFieldException e&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Using reflection to change the accessibility of a private object field and access it at runtime.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Field</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Life <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> meaning <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Hack <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        Life life <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Life<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Field</span> field <span style="color: #339933;">=</span> life.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;meaning&quot;</span><span style="color: #009900;">&#41;</span>;
            field.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>field.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>life<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchFieldException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalAccessException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Output:</p>
<blockquote><p>42</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/11/14/java-accessing-private-members/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

