<?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>Schabby&#039;s Blog &#187; Java</title>
	<atom:link href="http://schabby.de/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://schabby.de</link>
	<description>OpenGL, Java, Cassandra and other stuff that totally makes the world go round</description>
	<lastBuildDate>Mon, 16 Apr 2012 10:15:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>For- und ForEach Schleifen in Java</title>
		<link>http://schabby.de/java-for/</link>
		<comments>http://schabby.de/java-for/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 16:44:29 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1037</guid>
		<description><![CDATA[Kurze Einführung in For-Schleifen in Java mit Beispielen. Die For-Schleife und For-Each Schleife sind essentielle Bestandteile der Programmiersprache Java.]]></description>
			<content:encoded><![CDATA[<p>In diesem post widme ich mich folgender Fragestellungen:</p>
<ul>
<li>Beispiele und Erklärung der For- und ForEach Schleife</li>
<li>Was ist mit For bzw. ForEach Loop gemeint?</li>
<li>Was sind Java Laufvariablen und wie benutze ich sie?</li>
</ul>
<p><span id="more-1037"></span>Es werden erst die klassische For-Schleifen besprochen, bevor wir uns den moderneren For-Each Schleifen widmen.</p>
<h2>Einführung und Definition</h2>
<p>Die For-Schleife dient zum wiederholten ausführen eines Code-Blocks, wobei in jeder Iteration ein Schritt-Operation (zB. Hochzählen einer Variable) ausgeführt wird. Etwas weniger allgemein formuliert kann man sagen, dass sie zum Hoch- oder Runterzählen von Variablen verwendet werden kann. Der englische Begriffe der Schleife ist Loop, daher sind beide Begriffe synonym zu verstehen.</p>
<p>Das folgende Beispiel zählt die variable i von 5 bis 10 hoch:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// die Laufvariable</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ausgeführter Code der die Laufvariable i verwenden kann, zB.</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;i = &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wir sehen, dass in dem eingeklammerten Teil vor dem <tt>for</tt> etwas kryptisches stehts, was offensichtlich zum Verwenden der For-Schleife nötig ist. Dieser Teil läßt sich in drei Teile zerlegen, die jeweils mit einem Semmikolon getrennt sind:<br/></p>
<p><tt>for(</tt> <b>Start</b> <tt>; </tt> <b>Ende</b> <tt>; </tt> <b>In-/Dekrementieren</b> <tt>)</tt><br/></p>
<p>Im obigen Beispiel ist besteht der erste Teil (Start) aus dem setzen des Startwertes <tt>i = 5</tt>, also dass die Schleife bei 5 anfangen soll zu zählen. Häufig wird an dieser Stelle die Deklaration der Variable  Der zweite Teil ist das Abbruchkriterium, nämlich <tt>i &lt;= 10</tt>, was so viel heißt wie, dass <tt>i</tt> solange hochgezählt wird, bis i kleiner-gleich 10 ist. Der dritte Teil besagt was nach jeder Iteration passieren soll. In diesem Fall wird <tt>i</tt> um 1 erhöht. Eine häufige Notation die man an dieser Stelle sieht ist <tt>i++</tt>, was einfach nur eine Kurzform von <tt>i = i + 1</tt> ist. Entsprechend ist übrigens <tt>i--</tt> nur die Kurzform von <tt>i = i - 1</tt>. Ein weiteres Beispiel einer For-Schleife sie wie folgt aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ausgeführter Code der die Laufvariable i verwenden kann, zB.</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;i = &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Diese Schleife zählt ganz offensichtlich runter von 10 auf 5. In diesem Beispiel deklarieren wir <tt>i</tt> &#8220;inline&#8221; gleich mit in der Start-Definition der For-Schleife. Das spart uns eine Zeile vorher und es ist klar, dass <tt>i</tt> nur im Scope des Schleifen-Blocks liegt. Die Definition der Abbruchbedingung ist entsprechend abgewandelt zum ersten Beispiel. Der letzte Teil ist die dekrementierung von <tt>i</tt>.</p>
<p>Generell kann man also den eingeklammerten Teil der Schleifendefinition wie folgt lesen:</p>
<p><tt>for( </tt><b>Starte bei <tt>i=10</tt> und führe</b><tt> ;  </tt><b>solange wie <tt>i&gt;=5</tt> gilt,</b><tt> ;  </tt><b>den Code <tt>i--</tt> aus</b><tt> )</tt><br/></p>
<p>Der Unterschied zu einer <tt>while</tt>-Schleife liegt also darin, dass im eingeklammerten Teil nicht nur die Abbruchbedingung festgelegt wird.</p>
<p>Schleifen können auch verschachtelt werden und so ist es nicht unüblich, dass man zum iterieren über Bilder oder Matrizen zwei For-Schleifen anwendet um mit zwei Laufvariablen einen 2-Dimensionalen Raum abzuwandern. Im folgenden Beispiel iterieren wir über das Google Logo und geben dabei Pixelweise die Farbe aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// lade Google Logo</span>
<span style="color: #003399;">BufferedImage</span> image <span style="color: #339933;">=</span> ImageIO.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.google.de/images/srpr/logo3w.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// x iteriert über die Breite des Bildes</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> image.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// y iteriert über die Höhe des Bildes</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> image.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> y<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Gib Pixelfarben als Red, Green, Blue, alpha Komponenten auf der Konsole aus.</span>
		<span style="color: #000066; font-weight: bold;">int</span> red   <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> green <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGreen</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> blue  <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBlue</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> alpha <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBlue</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;Position (&quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;): [&quot;</span><span style="color: #339933;">+</span>red<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>green<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>blue<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>alpha<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Natürlich können auch andere Werte als Zahlen als Laufvariabe und zur Iteration verwendet werden. So war es früher beispielsweise üblich, dass man Iterator-Objekte verwendete, die den aktuellen Status der Laufvariable wiederspiegelte und mit denen über unterschiedliche Datenstrukturen iteriert werden konnte. Im Prinzip speichern solche Iterator Objekte intern auch nur einen index ab, an welcher Stelle man sich aktuell in der Datenstruktur befindet. Ein Beispiel macht deutlich, was gemeint ist:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// dynamische Liste, so wie sie früher häufig verwendet wurde</span>
Vector<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> vector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// wir hängen ein paar Namen an die Liste an</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Klaus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Vector.iterator() gibt ein Objekt vom Type Iterator zurück</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> Iterator<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> vector.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// it.next() holt den nächsten Wert aus dem Vector ab</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>it.<span style="color: #006633;">next</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: #009900;">&#125;</span></pre></div></div>

<p>Die Ausgabe ist erwartungsgemäß:</p>
<pre>
Peter
Klaus
Frieda
Manfred
</pre>
<p>Das interessant an diesem Beispiel ist, dass der letzte Part des in Klammern eingeschlossenen Teils leer bleibt. Das heißt es gibt keinen Iterationsteil. Dieser wird nämlich im For-Block selbst ausgeführt (<tt>it.next()</tt>).</p>
<p>So weit, so gut. Wollen wir nun zum nächsten Level vordringen.</p>
<h2>ForEach Schleifen mit Beispielen</h2>
<p>For-Each Schleifen funktionieren ganz ähnlich wie die gewöhnlichen For-Schleifen und sind quasi eine Weiterentwicklung. Im Gegensatz zu den normalen, verwenden ForEach Schleifen aber keine nach außen sichtbare Laufvariable oder Iterator-Objekt, sondern gibt in jeder Iteration immer direkt den Wert zurück. Das heißt aber auch, dass immer über eine entsprechende Datenstruktur iteriert werden muss die das ermöglicht. Schauen wir uns ein Beispiel an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// definition eine Datenstruktur, hier ein Array mit 9 Werten</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ForEach Schleife</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> k<span style="color: #339933;">:</span> array <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;k = &quot;</span><span style="color: #339933;">+</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Ausgabe ist</p>
<pre>
k = 4
k = 8
k = 4
k = 2
k = 2
k = 1
k = 1
k = 5
k = 9
</pre>
<p>Man sieht also, dass <tt>k</tt> nicht die Laufvariable hält, sondern den direkten Wert. ForEarch iteriert also über alle Elemente und ruf den Block auf. Daher kann auch der Name abgeleitet werden: <i>For Each element, do this and that</i>. Intern wird dennoch ein Iterator-Objekt gehalten in dem der State der Iteration festgehalten wird. Da man als Entwickler aber quasi nie am Index der Laufvariable interessiert ist, sondern eigentlich nur an den Werten, macht ForEach jede Menge Sinn. Ein paar Beispiele für weitere Verwendungen von ForEach-Schleifen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Set<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> set <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #339933;">:</span> set<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>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In diesem Beispiel ist die Reihenfolge der Ausgabe nicht definiert, da es sich um eine Menge (Set) handelt und nicht um eine Liste oder andere order-preserving Datenstruktur. Das Gegenbeispiel dazu ist die Verwendung der ArrayList:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #339933;">:</span> list<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>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Und das wars <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Ich denke Ihr habt nun eine sehr gute Vorstellung davon wie diese Schleifen zu verwenden sind. Wenn ich etwas vergessen habe oder einen Fehler eingebaut habt, laßt es mich bitte wissen. Wenn Euch der Post gefallen, dann laßt auch gern einen Kommentar da.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Arrays</title>
		<link>http://schabby.de/java-arrays/</link>
		<comments>http://schabby.de/java-arrays/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 23:12:33 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1077</guid>
		<description><![CDATA[Dieser Artikel behandelt die folgenden Themen Java Arrays mit Beispielen Arrays in Java in mehreren Dimensionen Arrays sind die niedrigsts (low-level) aber auch performanteste Datenstruktur in Java. Sie folgen sehr nah der Hardwarefunktionalität im Speicher des Rechners und sind somit eigentlich ein Überbleibsel aus der C/C++ Welt. Dennoch kann man Arrays in vielen Java-Anwendungen heutzutage [...]]]></description>
			<content:encoded><![CDATA[<p>Dieser Artikel behandelt die folgenden Themen</p>
<ul>
<li>Java Arrays mit Beispielen</li>
<li>Arrays in Java in mehreren Dimensionen</li>
</ul>
<p><span id="more-1077"></span><br />
Arrays sind die niedrigsts (low-level) aber auch performanteste Datenstruktur in Java. Sie folgen sehr nah der Hardwarefunktionalität im Speicher des Rechners und sind somit eigentlich ein Überbleibsel aus der C/C++ Welt. Dennoch kann man Arrays in vielen Java-Anwendungen heutzutage noch finden.</p>
<h2>Grundlagen</h2>
<p>Das klassische Array ist eine Folge von Zahlen fester Länge. Diese Folge von Zahlen liegt als Block im Speicher. Schauen wir uns die folgenden zwei Arrays an.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 24 Integerwerte halten kann. Zu beginn sind die alle 0.</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">24</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// die eckigen Klammern können auch an die Variable geschrieben werden</span>
<span style="color: #000066; font-weight: bold;">int</span> someOtherArray<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Man definiert die Länge des Arrays, also wie viele Zahlen in das Array passen, wenn man das Array erstellt. Dabei wird der benötigte Speicher reserviert. Der Speicherblock kann nicht ohne weiteres wachsen, was wiederum die Länge des Array. Da die Zahlenarten in dem Array immer nur von einem Typ sind, ist zu beginn klar, wieviel Speicher reserviert werden muss. Für <tt>int</tt>-Arrays müssen zum Beispiel immer 4 bytes pro Wert vorgehalten werden. Das heißt ein <tt>int</tt>-Array das 24 Werte halten kann, benötigt 24*4=96 bytes. Streng genommen kommen da sogar nochmal 4 Bytes dazu, weil die Länges des Arrays im selben Speicherblock liegt (Backreferenzen für den Garbage Collector werden kurz mal ausgeblendet). </p>
<p>Die Zahl der Werte eines Arrays (oder eben die &#8220;Länge&#8221; eines Arrays), kann mit der Konstanten <tt>length</tt> abgefragt werden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</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>test.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: 7</span></pre></div></div>

<p>Um auf die Werte des Arrays zuzugreifen, muss lediglich der Speicherindex angegeben werden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 32</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>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe -5</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Um Werte zuzuweisen, wird genauso verfahren</span>
test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">78</span><span style="color: #339933;">;</span>
test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">98</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 78</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>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 98</span></pre></div></div>

<p>Als wichtiger Sonderfall sein bemerkt, dass bei jedem Zugriff auf das Array geprüft wird, ob der Index noch innerhalb der Länge des Arrays liegt. Wenn der Index kleiner 0 oder größer als <tt>length</tt> ist, wird eine <tt>IndexOutOfBoundsException</tt> geworfen. Siehe zB:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// schmeisst IndexOutOfBoundsExcpetion!!!</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>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5423</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Arrays aus verschiedenen Datentypen</h2>
<p>In den Grundlagen haben wir auf Arrays aus natürlichen Zahlen gearbeitet. Prinzipiell sind natürlich auch andere Datentypen möglich. Schauen wir uns als erstes Beispiele mit primitiven Datentypen an.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> intArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> floatArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> doubleArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">521</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Es können auch Arrays aus Objekten gebaut werden. Dabei sei zu beachten, dass beim erstellen des Arrays nicht die Objekte selbst erstellt werden, sondern nur die Pointer auf mögliche Objektinstanzen.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// erstelle 10 Pointer auf Object. Die Pointer sind</span>
<span style="color: #666666; font-style: italic;">// erstmal alle NullPointer.</span>
<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> objectArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// wir instatitieren ein neues Objekt mit &quot;new&quot; und lassen den Pointer an </span>
<span style="color: #666666; font-style: italic;">// Index 0 und 3 auf das neue Objekt zeigen.</span>
objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// es wird die toString() Methode des gerade instatitierten </span>
<span style="color: #666666; font-style: italic;">// Objektes ausgegeben</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>objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// NullPointerException, weil an Index 2 kein valider Pointer</span>
<span style="color: #666666; font-style: italic;">// auf ein Objekt zeigt.</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>objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Eine interessante Mischform nimmt dabei wieder der String ein, da er sich wie immer wie ein Objekt verhält, das aber nicht mit new instatiiert werden muss:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> stringArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hallo&quot;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Juhuuu&quot;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blablalb&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: hallo</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>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: Blablalb</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// NullPointerException!</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>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So viel zu den Basisics über Arrays. Wenn Euch was fehlt oder Ihr den Post gut fandet, laßt es mich gern in Form eines Kommentars wissen.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL Picking in 3D</title>
		<link>http://schabby.de/picking-opengl-ray-tracing/</link>
		<comments>http://schabby.de/picking-opengl-ray-tracing/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 22:25:54 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=964</guid>
		<description><![CDATA[There are two common ways to accomplish picking: Switching OpenGL to "selection mode" and tracing a line shot from your 2D view screen through the 3D scene. In this opengl picking tutorial I cover the latter and provide a code example.]]></description>
			<content:encoded><![CDATA[<p>This blog post explains a common and versatile approach to OpenGL picking called &#8220;ray picking&#8221;. An example in pseudo-code is given below.</p>
<p>Picking is the process of finding objects in you scene based on user input. Most commonly, you want to determine what object a user has clicked with his mouse. In this case the 2D mouse coordinates serve as a reference on the view port to identify the projected objected that has been clicked. A similar scenario is a first person shooter, where the gun serves as the picking pointer, however in world space coordinates. If you shoot your gun, the trajectory of the bullet is traced through your scene and collisions are detected.<span id="more-964"></span></p>
<div id="attachment_996" class="wp-caption aligncenter" style="width: 473px"><img src="http://schabby.de/wp-content/uploads/2011/09/screenshot-opengl-picking.png" alt="Screenshot OpenGL Picking" title="Screenshot OpenGL Picking" width="463" height="363" class="size-full wp-image-996" /><p class="wp-caption-text">A screenshot showing a hex map in an LWJGL application with a selected tile as an example for 3D picking</p></div>
<h3>OpenGL Selection Mode vs. Ray Picking</h3>
<p>There are two common ways of implementing picking. The first one makes use of a special OpenGL feature in which you render your scene in a so-called &#8220;selection mode&#8221;. In selection mode, OpenGL does not render to the frame buffer but gathers depths information of the objects with is later read at the point where your picking occurs. The second approach is called &#8220;Ray Picking&#8221; and is independent from OpenGL and simulates a line that is shot through the scene until it hits an object. I personally prefer the second approach for the following reasons:</p>
<ul>
<li>You don&#8217;t need an additional render pass. A render pass can take some considerable amount of time and special clipping/culling code is required to only render the small part of the scene which you want to perform the picking on. In addition, you render a less complex scene (omitting textures, lighting, normals, etc) in selection mode if possible which can add a high degree of complexity to your drawing routines (imagine breaking up display lists). Ray picking obviously requires some code to trace the ray through your scene, but this is usually less complex.</li>
<li>Ray picking can be done independently from the rest of the game code as a simultaneous and &#8220;read-only&#8221; operation on the scene graph. In some environments such as AWT/JOGL where the input devices are handled by a separate thread, your picking code can run as part of the handler thread and does not impact the performance of the rest of the game. This comes in handy especially in scenarios where you need to perform picking very often (eg. mouse hovering, target laser on the in-game gun).</li>
<li>In pretty much all Java environments, calling OpenGL takes a considerable amount of time due to JNI. It is therefore beneficial to minimize the number OpenGL calls. Rendering in selection mode would mean additional OpenGL calls that I rather substitute with ray picking.</li>
</ul>
<p>However, there are scenarios where ray picking is simply inferior or impractical. Consider picking in a scene with a lot of small details (eg. tree with little branches and leaves) or if you need to know the individual polygon/triangle that you picked. I will therefore cover both approaches, starting with ray picking in this post.</p>
<p><a href="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png"><img class="alignright" title="Picking by Ray Tracing" src="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png" alt="Picking by Ray Tracing" width="465" height="289" /></a></p>
<h3>Ray Picking</h3>
<p>Ray picking is the process of shooting a line (ie. &#8220;ray&#8221;) from the camera through the 2D viewscreen (where the 3D scene is projected on) into the scene until it hits an object. To do so, we need to know the camera and the point on the viewscreen (eg. mouse cursor position). The first point is trivial, however the second point is a bit more difficult to determine. Let&#8217;s say we have a 2D point on the viewscreen (x, y) and now want to map this point into world coordinates. One way would be to make use of the inverted viewing matrix, but inverting a matrix is usually an expensive undertaking. Instead, we compute the position of the plane of the viewscreen in world space and map the 2D point on this plane and from there into world space.</p>
<p>The algorithm in pseudo code follows below. Note that <tt>cameraLookAt</tt> is the 3D point where the camera looks at (as used on <tt>glLookat</tt>), <tt>cameraPosition</tt> is the current position of the camera in world space and <tt>cameraUp</tt> is the up vector of the camera.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">x = getMouseX() // scalar
y = getMouseY() // scalar

view = cameraLookAt - cameraPosition // 3D float vector
normalize view

h = crossProduct( view, cameraUp ) // 3D float vector
normalize h

v = crossProduct( h, view) // 3D float vector
normalize v</pre>
<p>ok, so far for <tt>h</tt> and <tt>v</tt>. Note that <tt>v</tt> is actually pointing in the same direction like the camera-up vector, but we compute it via the cross product to illustrate the method.</p>
<p>The vectors <tt>h</tt> and <tt>v</tt> are currently normalized to the length of 1. We now need to compute the lengths of <tt>h</tt> and <tt>v</tt> in the view port. In my example I assume that you set up your frustum by a field of view angle in degrees (<tt>fovy</tt>) as commonly the case with <tt>glFrustum</tt>. Let <tt>nearClippingDistance</tt> be the distance to the near clipping plane and <tt>width/height</tt> the ratio of the view port width divided by the viewport heigth.</tt></tt></p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// convert fovy to radians
rad = fovy * PI / 180
vLength = tan( rad / 2 ) * nearClippingPlaneDistance
hLength = vLength * (width / height)

scale v by vLength
scale h by hLength</pre>
<p>The two scalars <tt>vLength</tt> and <tt>hLength</tt> in combinaton with <tt>v</tt> and <tt>h</tt> help spanning the view port plane extending from the center point. At this point it is worth mentioning that it may make sense to only compute <tt>h</tt> and <tt>v</tt> when you update the camera orientation because the normalization involves a square root which is a relatively expensive operation (actually <tt>tan</tt> may also be avoided).</p>
<p>Now it is time to map the 2D mouse coordinates onto the view port plane.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// translate mouse coordinates so that the origin lies in the center
// of the view port
x -= width / 2
y -= height / 2

// scale mouse coordinates so that half the view port width and height
// becomes 1
y /= (height / 2)
x /= (width / 2)

// linear combination to compute intersection of picking ray with
// view port plane
pos = cameraPos + view + h*x + v*y

// compute direction of picking ray by subtracting intersection point
// with camera position
dir = pos - cameraPos</pre>
<p>That's pretty much it. We now got <tt>pos</tt> as the intersection point of the picking ray in the view port plane and the picking ray direction <tt>dir</tt>. This describes a line that can now be used to intersect with the individual objects of the scene.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// brute force
for all objects in the scene
  test for intersection and keep closest
end for</pre>
<p>So by intersecting the picking ray with all your visible objects in your scene while keeping track of the distance, you determine the object that is hit by your mouse cursor.<br />
Checking all objects in our scene may not be very efficient though. So in a first attempt I recommend to test only objects that lie (partly) within your viewing frustum. Under normal circumstances you got that code already in your scene graph as part of the software culling routine when pumping obects into the render pipeline.</p>
<p>I hope this helped. If you liked the post or if you got questions, ideas hints or found a problem, please leave a comment. I am trying to answer as quickly as possible. Also, if you want me to send you the source code of this example please let me know, I am happy to share the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/picking-opengl-ray-tracing/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>JOGL in Eclipse</title>
		<link>http://schabby.de/jogl-example-hello-world/</link>
		<comments>http://schabby.de/jogl-example-hello-world/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 14:25:38 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=858</guid>
		<description><![CDATA[This post describes how to set up JOGL (Java OpenGL Binding) in Eclipse on Windows. It provides example code to get started with JOGL and Eclipse and a video walk-through covering the setup.]]></description>
			<content:encoded><![CDATA[<p>This getting started-tutorial is intended for JOGL users that are mere beginners. It helps to setup a recent JOGL installation in Eclipse on Windows. There is a video-walk-through further down the post, in case you dont feel like reading.<br />
<span id="more-858"></span><br />
First you need to download JOGL. This is actually more difficult than it sounds because the precompiled binary packages are a bit hidden in the new project site. Fortunately, I figured out <a href="http://schabby.de/jogl-download/">where you can download recent JOGL binaries</a>.</p>
<p>For this tutorial, I am using</p>
<ul>
<li><tt>jogl-b633-2012-01-23_20-37-13</tt>, and</li>
<li><tt>gluegen-b480-2012-01-23_16-49-04</tt>.</li>
</ul>
<p>This is how your project setup should look like:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png" alt="JOGL Project Setup" title="JOGL Project Setup" width="310" height="346" class="aligncenter size-full wp-image-1033" /></a></p>
<p>So one you got the binaries, unpack the ZIP file somewhere. You will notice the <tt>jar</tt> folder with a number of JAR files. All you need is in that <tt>jar</tt> folder, because the DLL binaries are included in the JARs.</p>
<p>Create a new eclipse project (name doesnt matter) and add the following JARs on the classpath:</p>
<ul>
<li><tt>gluegen-rt-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
<li><tt>jogl-all-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
</ul>
<p>These files are the native part of the OpenGL binding and are linked via Java Native Interface (JNI). Since the DLLs are precompiled as part of the JAR files, you are almost ready to go.</p>
<p>Once you got that set up, create a package <tt>de.schabby.jogl.helloworld</tt> and copypaste the following two classes in the newly created package.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLCapabilities</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.awt.GLCanvas</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld 
<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><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #666666; font-style: italic;">// setup OpenGL Version 2</span>
    	GLProfile profile <span style="color: #339933;">=</span> GLProfile.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>GLProfile.<span style="color: #006633;">GL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	GLCapabilities capabilities <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCapabilities<span style="color: #009900;">&#40;</span>profile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// The canvas is the widget that's drawn in the JFrame</span>
    	GLCanvas glcanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCanvas<span style="color: #009900;">&#40;</span>capabilities<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">addGLEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Renderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">JFrame</span> frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JFrame</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Hello World&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> glcanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// shutdown the program on windows close event</span>
        frame.<span style="color: #006633;">addWindowListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">WindowAdapter</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosing<span style="color: #009900;">&#40;</span><span style="color: #003399;">WindowEvent</span> ev<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><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        frame.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">setVisible</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: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <rr>Render.java</tt> class looks like follows. It implements <tt>GLEventListener</tt> which is the call-back implementaiton by JOGL to do all OpenGL rendering.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GL2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLAutoDrawable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.glu.GLU</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Renderer</span> <span style="color: #000000; font-weight: bold;">implements</span> GLEventListener 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> GLU glu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLU<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> display<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClear</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_COLOR_BUFFER_BIT</span> <span style="color: #339933;">|</span> GL2.<span style="color: #006633;">GL_DEPTH_BUFFER_BIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.5f, 0.0f, <span style="color: #339933;">-</span>6.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_TRIANGLES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>0.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span>3.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_QUADS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glFlush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> displayChanged<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">boolean</span> modeChanged, <span style="color: #000066; font-weight: bold;">boolean</span> deviceChanged<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;displayChanged called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<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;init() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClearColor</span><span style="color: #009900;">&#40;</span>0.0f, 0.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glShadeModel</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_FLAT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> reshape<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y, <span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<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;reshape() called: x = &quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, y = &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, width = &quot;</span><span style="color: #339933;">+</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, height = &quot;</span><span style="color: #339933;">+</span>height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// avoid a divide by zero error!</span>
        <span style="color: #009900;">&#123;</span>
            height <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">float</span> h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> width <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> height<span style="color: #339933;">;</span>
&nbsp;
        gl.<span style="color: #006633;">glViewport</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, width, height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_PROJECTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        glu.<span style="color: #006633;">gluPerspective</span><span style="color: #009900;">&#40;</span>45.0f, h, <span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">20.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_MODELVIEW</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispose<span style="color: #009900;">&#40;</span>GLAutoDrawable arg0<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;dispose() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By right-clicking on the <tt>main()</tt> method and chosing "run as application", Eclipse will execute the program. You should see something like this:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png" alt="Jogle Hello World" title="Jogle Hello World" width="320" height="300" class="aligncenter size-full wp-image-1034" /></a></p>
<p>For the lazy, I provide a video-walkthough.<br />
<iframe width="620" height="415" src="http://www.youtube.com/embed/2EaaQrgvsL0" frameborder="0" allowfullscreen></iframe></p>
<p>Common problems and solutions:</p>
<ul>
<li>If you get <tt>Can't load AMD 64-bit .dll on a IA 32-bit platform</tt>, then you are using an 64bit DLL (eg. gluegen-rt.dee) with an JRE that is 32bit. In most cases you can just switch to a 64bit JRE if you are running a 64bit system. Make sure that Eclipse is using the right JRE when you execute the code.</li>
<li>If you get <tt>no gluegen-rt in java.library.path</tt>, then you may need to add <tt>gluegen-rt-natives-windows-amd64.jar</tt> to you classpath.</li>
<li>If you get <tt>no nativewindow_awt in java.library.path</tt>, then you may need to add <tt>jogl-all-natives-windows-amd64.jar</tt> to you classpath.</li>
</ul>
<p>That's it so far. Please let me know if you have questions or comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/jogl-example-hello-world/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>JOGL Download</title>
		<link>http://schabby.de/jogl-download/</link>
		<comments>http://schabby.de/jogl-download/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 09:35:40 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=850</guid>
		<description><![CDATA[Just can't find the JOGL downloads (Java OpenGL bindings)? Well, I figured out where to download JOGLs most recent binary distribution such as 2011 releases :)]]></description>
			<content:encoded><![CDATA[<p>You are right in this page if you are on the quest for one of the following:</p>
<ul>
<li>recent JOGL binary distribution download</li>
<li>precompiled Windows Java OpenGL bindings</li>
<li>you just can not find the place where to download JOGL <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p><span id="more-850"></span><br />
I had a couple of hours on my own recently and though it would be a good thing to catch up with the latest developments in the Java+OpenGL environment. So I looked for the download of JOGL (Java Bindings for OpenGL) just to realize, that I is actually quite difficult to find a recent binary distribution of JOGL!</p>
<p><strong>Eventually, after a while I found it: <a href="http://jogamp.org/deployment/autobuilds/master/" rel="nofollow">http://jogamp.org/deployment/autobuilds/master/</a>.</strong></p>
<p>There you find an extensive list of directories containing builds, mostly for different binding projects such as <em>gluegen</em>, <em>joal</em>, etc. Search for &#8220;jogl&#8221; to jump to the JOGL builds. </p>
<p>The downloads are basically a list of (probably automated) builds. So I hope that this will be the place for the next months to get update-to-date JOGL binaries. The builds that have the suffix &#8220;-master&#8221; in their directory name appear to contain only documentation. </p>
<p>Please also note that for running JOGL you also need gluegen. I personally use the 2012-01-23 release at the moment. This is</p>
<ul>
<li>gluegen-b480-2012-01-23_16-49-04</li>
<li>jogl-b633-2012-01-23_20-37-13</li>
</ul>
<p>which seem to work.</p>
<p>So the guys at JogAmp.org do actually provide precompiled binaries, they just make it difficult to find them. <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now that you downloaded JOGL, you may be interested in my <a href="http://schabby.de/jogl-example-hello-world/">JOGL Hello World example</a> (includes how to set up the classpath and DLLs right).</p>
<p>Please leave a comment if it helps or my resource gets out of date.</p>
<p>2010-10-20 EDIT: updated download URL.<br />
2010-11-03 EDIT: updated download URL again to point to autobild directory.<br />
2010-11-19 EDIT: added reference to my hello world example with JOGL<br />
2011-07-27 EDIT: updated link to JOGL downloads<br />
2012-01-27 EDIT: updated link to JOGL downloads, added note about gluegen</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/jogl-download/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Keine Sessions in Tomcat / JSP</title>
		<link>http://schabby.de/keine-sessions-in-tomcat-jsp/</link>
		<comments>http://schabby.de/keine-sessions-in-tomcat-jsp/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 19:27:41 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=827</guid>
		<description><![CDATA[Ab einem gewissen Trafficvolumumen beginnt die Sessionstabelle die der Tomcat auf der Platte hält stetig zu wachsen und täglich größere Ausmaße anzunehmen. Da der Tomcat einen großen Teil der serialisierten Sessiontabelle im Speicher hält, kann das zu einem akkuten Crashrisiko werden, wenn der Tomcat bis zum Limit wächst (oder das OS zu swappen beginnt). Ausserdem [...]]]></description>
			<content:encoded><![CDATA[<p>Ab einem gewissen Trafficvolumumen beginnt die Sessionstabelle die der Tomcat auf der Platte hält stetig zu wachsen und täglich größere Ausmaße anzunehmen. Da der Tomcat einen großen Teil der serialisierten Sessiontabelle im Speicher hält, kann das zu einem akkuten Crashrisiko werden, wenn der Tomcat bis zum Limit wächst (oder das OS zu swappen beginnt). <span id="more-827"></span>Ausserdem ist es ineffizient für jede css-, JavaScript oder Bilddatei einen Sessionstempel im Header mitzusenden. Das gilt inbesondere für Seiten die stark und regelmäßig von Bots gecrawled werden, da Bots naturgemäß keine Session halten und der Tomcat bei jedem Zugriff des Bots von einem neuen User ausgehen muss und eine neue Session anlegt.</p>
<p>Es stellt sich also folgende Frage:</p>
<ul>
<li>Session unterdrücken in Tomcat / JSP</li>
<li>Tomcat keine Sessions</li>
<li>JSP / Tomcat ohne Sessions</li>
</ul>
<p>Im Prinzip ist die Lösung sehr einfach. Man muss nur zwei einfache Regeln befolgen.</p>
<ol>
<li>Mache nur nur <tt>request.getSession()</tt> calls wenn Du wirklich eine neue oder die bestehende Session brauchst (Login, etc.). Überlege Dir auch, ob nicht <tt>request.getSession(false)</tt> reicht (= nur eine bereits bestehende Session zurückgeben, aber keine neue Anlegen falls noch keine existiert).</li>
<li>Setze in allen JSPs die Direktive <tt><%@ page session="false" %></tt> um das automatische Erzeugen einer Session im JSP-Code zu unterbinden. Wenn Du die Session doch in einer JSP brauchen solltest, erstelle sie vorher in einem Controller.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/keine-sessions-in-tomcat-jsp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BufferedReader &#8211; kurz Erklärt</title>
		<link>http://schabby.de/bufferedreader-kurz-erklart/</link>
		<comments>http://schabby.de/bufferedreader-kurz-erklart/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:53:37 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=825</guid>
		<description><![CDATA[Der BufferedReader bereitet so manchem Java-Experten noch Kopfschmerzen. In diesem Artikel wird kurz und knapp der BufferedReader mit einem Beispiel erläutert sowie seine Funktionsweise erklärt.]]></description>
			<content:encoded><![CDATA[<p>Moin, moin! Heute nun eine kurze Einweisung in die richtige Verwendung von BufferedReaders und den Grund warum der BufferedReader so populär ist. Im Grunde gibt es zwei Gründe:<span id="more-825"></span></p>
<ol>
<li>BufferedReader verbessert die Lesegeschwindigkeit aus anderen Readern (z.B. FileReader um von der Festplatte zu lesen) drastisch,</li>
<li>BufferedReader hat praktischer Weise eine readLine() Methode, mit deren Hilfe man ganze Zeilen lesen kann.</li>
</ol>
<p>In Prinzip ist der BufferedReader auch nur ein Reader. Das heisst er hat die typischen read(&#8230;) Methoden. Intern aber, arbeitet der BufferedReader auf einem Speicherbereich (dem Buffer), den er erst füllt, bevor seine read(&#8230;) Methoden darauf arbeiten können. Das heisst, dass der BufferedReader größere Datenblöcke auf einmal lesen kann und die dann nach und nach an den äußeren Reader weiterleiten kann. Das ist insbesondere sinnvoll bei Readern die von wiederum von Low-Level Readern lesen, die jeweils bei größeren Datenblöcken performant sind, wie typischer Weise von der Festplatte oder vom Socket.</p>
<p>Ein typischer Gebrauch eines BufferedReaders sieht so aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">InputStreamReader</span> inputStreamReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span> <span style="color: #009900;">&#40;</span>textFile<span style="color: #009900;">&#41;</span>, encoding <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">BufferedReader</span> bufferedReader <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>inputStreamReader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://schabby.de/bufferedreader-kurz-erklart/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java JDK Download</title>
		<link>http://schabby.de/java-jdk-download/</link>
		<comments>http://schabby.de/java-jdk-download/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:07:19 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=820</guid>
		<description><![CDATA[Kurze, klare Erklaerung welcher Java JDK der richtige zum herunterladen ist und was SE, EE und ME bedeutet. Der offizielle Download des Java JDKs gibt es naemlich nur an einer Stelle.]]></description>
			<content:encoded><![CDATA[<p>Seltsamer Weise scheinen viele meiner freundlichen Mitmenschen sich zu fragen, wo die richtige Quelle zum downloaden des Java JDKs bzw. des Java SDKs ist. Nun, da gibt es eine einfache Antwort: offiziell bei <span id="more-820"></span><a href="http://java.sun.com/">SUN</a> <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ok, neuerdings Oracle, da Oracle SUN mitsamt MySQL gekauft hat, aber das steht auf einem anderen Blatt.</p>
<p>Uebrigens, JDK heisst Java Development Kit, und SDK Software Development Kit. Es nimmt sich aber defacto nichts.</p>
<p>Ist man nun bei SUN gelandet, gibt es drei Varianten zum downloaden.</p>
<ul>
<li>Java SE: steht fuer Standard Edition ist ziemlich sicher das was Du suchst wenn Du das klassische Javapaket fuer den Deskop runterladen willst (JRE und JDK)</li>
<li>Java ME: steht fuer Mobile Edition und beinhaltet eine auf fuer Handies zugeschnittene Java Plattform (mit Compiler) mit reduzierten Bibliotheken</li>
<li>Java EE: steht fuer Enterprise Edition und ist deutlich umfangreicher als die Standard Edition und enthaelt zusaetzliche, business nahe Tools wie den Glassfish Application Server</li>
</ul>
<p>Ich hoffe das hilft.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-jdk-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven Tests Ignorieren</title>
		<link>http://schabby.de/maven-tests-ignorieren/</link>
		<comments>http://schabby.de/maven-tests-ignorieren/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 09:02:47 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=816</guid>
		<description><![CDATA[Es kommt auch in den besten Familien vor, dass mal ein unit test nicht läuft. Da muss sich nicht schämen, nur ein bisschen ärgern. Ähnliche Probleme sind Maven tests ausschalten mvn ignoriere test fehler Wenn man trotzdem seinen maven snapshot bauen möchte, müssen aber die tests failures ignoriert werden. Das wird mittels einer Property direkt [...]]]></description>
			<content:encoded><![CDATA[<p>Es kommt auch in den besten Familien vor, dass mal ein unit test nicht läuft. Da muss sich nicht schämen, nur ein bisschen ärgern. Ähnliche Probleme sind</p>
<ul>
<li>Maven tests ausschalten</li>
<li>mvn ignoriere test fehler</li>
</ul>
<p><span id="more-816"></span><br />
Wenn man trotzdem seinen maven snapshot bauen möchte, müssen aber die tests failures ignoriert werden. Das wird mittels einer Property direkt auf der Kommandezeile bewerkstelligt. Da ich dieses Property andauernd vergesse, möchte ich es mir hier selbst und der Allgemeinheit nochmals vor Augen führen:</p>
<p><tt><br />
mvn -Dmaven.test.failure.ignore=true install<br />
</tt></p>
<p>Hope that helps&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/maven-tests-ignorieren/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cassandra Installation and Configuration</title>
		<link>http://schabby.de/cassandra-installation-configuration/</link>
		<comments>http://schabby.de/cassandra-installation-configuration/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:44:42 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Cassandra]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=786</guid>
		<description><![CDATA[This is the second post on my little &#8220;Cassandra &#8211; Getting Started&#8221; series covering the installation and basic configuration of Cassandra. Cassandra is extremely easy to set up, especially compared to HBase. All you got to do is to download, extract, edit a single XML-file and run. But let us take it step by step. [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second post on my little &#8220;Cassandra &#8211; Getting Started&#8221; series covering the installation and basic configuration of Cassandra. Cassandra is extremely easy to set up, especially compared to <a href="http://hadoop.apache.org/hbase/">HBase</a>. All you got to do is to download, extract, edit a single XML-file and run. But let us take it step by step.<br />
<span id="more-786"></span><br />
You can <a href="http://incubator.apache.org/cassandra/">download Cassandra</a> directly from it&#8217;s (her?) website. At the time of the submission of this post, version 0.4.1 was the most recent stable. Note that you need <a href="http://java.sun.com/javase/downloads/index.jsp">Java 6</a> installed to run Cassandra which I assume here as properly installed. </p>
<p>After extracting Cassandra to some folder (on my Windows box I placed it directly in <tt>D:\cassandra</tt>), the only file you need to edit is <tt>conf/storage-conf.xml</tt>. While Cassandra is engineered to run on a large number of machines in a network, we start it here as a single node with the default parameter set, so that most of the settings are ok for now. </p>
<p>If your are <b>not</b> on a Unix-like system, you need to update the folders where Cassandra is supposed to store the data. If your using Windows (like me), then find the following lines in <tt>conf/storage-conf.xml</tt> and change the paths to something sensible</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/commitlog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/callouts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/bootstrap<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/lib/cassandra/staging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>like for example my settings:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/commitlog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CommitLogDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataFileDirectories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/callouts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CalloutLocation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/bootstrap<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/BootstrapFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>D:/cassandra/data/staging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StagingFileDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Let&#8217;s take Cassandra for a spin and check if she starts up correctly. For Mac OS, Linux, etc. users, simply change to the <tt>bin</tt> directory of Cassandra and run <tt>./cassandra</tt>. As an aside for the impatient, I start Cassanda with <tt>sudo</tt> to avoid trouble with the Cassandras <tt>system.log</tt>.</p>
<p>Windows users, however, that use the command line (meaning not <a href="http://www.cygwin.com/">Cygwin</a>) cannot start it just like that. The <tt>cassandra.bat</tt> didnt work for me on my Vista box if executed with <tt>bin</tt> being the current working directory (probably due to the CASSANDRA_HOME environment variable that get&#8217;s incorrectly set in the batch file). BUT it works perfect if you call <tt>bin\cassandra.bat</tt> from Cassandra&#8217;s main directory above <tt>bin</tt>. So if you are on Windows, change to the directory where you extracted Cassandra and execute <tt>bin\cassandra.bat</tt>.</p>
<p>Cassandras output on startup will look similar to this (here on Mac OS):</p>
<pre style="font-size:10px">
Schabbys-MacBook-Pro:bin johannes$ sudo ./cassandra
Schabbys-MacBook-Pro:bin johannes$ Listening for transport dt_socket at address: 8888
DEBUG - Loading settings from ./../conf/storage-conf.xml
DEBUG - Syncing log with a period of 1000
DEBUG - opening keyspace Keyspace1
DEBUG - adding Super1 as 0
DEBUG - adding Standard2 as 1
DEBUG - adding Standard1 as 2
DEBUG - adding StandardByUUID1 as 3
DEBUG - adding LocationInfo as 4
DEBUG - adding HintsColumnFamily as 5
DEBUG - opening keyspace system
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-1-Data.db: 0 ms.
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-2-Data.db: 0 ms.
DEBUG - INDEX LOAD TIME for /Users/johannes/cassandra/data/system/LocationInfo-3-Data.db: 0 ms.
INFO - Replaying /Users/johannes/cassandra/commitlog/CommitLog-1257980407451.log
DEBUG - Replaying /Users/johannes/cassandra/commitlog/CommitLog-1257980407451.log starting at 117
DEBUG - Reading mutation at 117
DEBUG - replaying mutation for system.L: {ColumnFamily(LocationInfo [Generation,])}
INFO - Flushing Memtable(LocationInfo)@228828460
DEBUG - Submitting LocationInfo for compaction
INFO - Completed flushing Memtable(LocationInfo)@228828460
INFO - Compacting [/Users/johannes/cassandra/data/system/LocationInfo-1-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-2-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-3-Data.db,/Users/johannes/cassandra/data/system/LocationInfo-4-Data.db]
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-1-Data.db   : 256
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-2-Data.db   : 512
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-3-Data.db   : 768
DEBUG - index size for bloom filter calc for file  : /Users/johannes/cassandra/data/system/LocationInfo-4-Data.db   : 1024
DEBUG - Expected bloom filter size : 1024
INFO - Compacted to /Users/johannes/cassandra/data/system/LocationInfo-5-Data.db.  0/255 bytes for 0/1 keys read/written.  Time: 150ms.
DEBUG - collecting Generation:false:4@3
DEBUG - collecting Token:false:16@0
INFO - Saved Token found: 160533723849634883377008460059010504450
DEBUG - Starting to listen on 127.0.0.1:7001
DEBUG - Binding thrift service to localhost:9160
</pre>
<p>I think that&#8217;s it. Leave a comment if you run in trouble or check the nice <a href="http://wiki.apache.org/cassandra/GettingStarted#if_something_goes_wrong">If Something Goes Wrong</a> page in the Cassandra Wiki.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/cassandra-installation-configuration/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

