<?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>Andy Gibson &#187; JTexgen</title>
	<atom:link href="http://www.andygibson.net/blog/tag/jtexgen/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andygibson.net/blog</link>
	<description>Open Source Projects &#38; Technical Writings</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:33:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Glass Button Tutorial &#8211; In Java</title>
		<link>http://www.andygibson.net/blog/tutorial/glass-button-tutorial-in-java/</link>
		<comments>http://www.andygibson.net/blog/tutorial/glass-button-tutorial-in-java/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:01:55 +0000</pubDate>
		<dc:creator>Andy Gibson</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JTexgen]]></category>

		<guid isPermaLink="false">http://www.andygibson.net/blog/?p=446</guid>
		<description><![CDATA[Create glass effect buttons using nothing but Java code and JTexGen, a procedural texture library for Java. First create a new project in your favorite IDE and add the JTextGen jar file, or create a new maven project and add the JTexgen dependency if you installed it from the source distribution into your local repository. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/blog/wp-content/uploads/2009/08/mainButton.png" class="leftImg" /><br />
Create glass effect buttons using nothing but Java code and <a href="http://www.andygibson.net/blog/index.php/2009/08/02/jtexgen-procedural-texture-library-released/">JTexGen</a>, a procedural texture library for Java. First create a new project in your favorite IDE and add the JTextGen jar file, or create a new maven project and add the JTexgen dependency if you installed it from the source distribution into your local repository.<br />
<span id="more-446"></span><br />
Our glass button will be round-ish, and will have a margin at the edge where we will render the shadow. We&#8217;ll start by writing our main method and creating the glass button class and rendering it. First create a new class called <code>GlassButton.java</code> and make it extend <code>AbstractTexture</code>. We&#8217;ll add a constructor that takes the base color of the button and returns it in the <code>getColor</code> method.</p>
<pre class="brush: java;">
public class GlassButton extends AbstractTexture {

	private final RGBAColor color;

	public GlassButton(RGBAColor color) {
		this.color = new RGBAColor(color);
	}

	public void getColor(double u, double v, RGBAColor value) {
		value.setColor(color);
	}
}
</pre>
<p>Next we&#8217;ll create our main class and add some code to create and render our texture.</p>
<pre class="brush: java;">
public class Main {

	public static void main(String[] args) {
		RGBAColor color = RGBAColor.red();
		Texture texture = new Background(new GlassButton(color),RGBAColor.white());
		TextureViewer.show(texture);
	}
}
</pre>
<p>Ok, a fairly simple start, we create our texture with a red color over a white background and pass it to the viewer. If you save and run this, you should get a window with a start button, which will render the red texture when clicked. Let&#8217;s add a method to our texture to calculate where on the button the textures <code>u,v</code> co-ordinates are. We do this by calculating the distance from the center of the texture the U,V co-ordinates are. The value returned will range from 0..1 inside the circle and outside the circle, it will return 1+ and grow larger the further it gets from the center. This lets us determine what should appear for any given point on the texture. Since we want a border around the edge of the button, we will have to scale the <code>u,v</code> values so when the result ramps up to 1 before getting to the edge of the texture. Add the following method to the <code>GlassButton</code> class.</p>
<pre class="brush: java;">
protected double calculateCircleRange(double u,double v) {
	//displace point
	u = u - 0.5;
	v = v - 0.5;
	//calculate distance
	double distance = Math.sqrt((u*u)+(v*v));
	//double it since the range will only be from 0 to 0.5
	return distance * 2;
}
</pre>
<p>We can verify this works by changing our get value method to return this value in the alpha channel.</p>
<pre class="brush: java;">
public void getColor(double u, double v, RGBAColor value) {
	double range = calculateCircleRange(u, v) * 1.2;
	value.setColor(color);
	value.setAlpha(1-range);
}
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/coefAlpha.png" class="leftImg" /><br />
We inverted the signal (<code>1-range</code>) so the color would be stronger at the center and weaker at the edge. Since the range goes beyond 1, then the value is truncated to the range 0..1 which is why the pattern repeats at the edge of the circle. </p>
<p>What we need to do is calculate the color based on the return value of this function. Values greater than 1 will result in rendering the shadow with a strength inversely proportional to the distance from the edge. For 0 to 1 values, we will render the button color and overlay the highlight onto it. We will also add an inner shadow in the range 0.7 to 1 with a strength based on the distance from the edge. The button color will consists of a base color which slowly transforms into a darker version of that base color the further down the button we go. The highlight is added as a lighter version of the base color with the alpha channel decreasing the further down the button we go. This has the effect of fading out the highlight the further down we go. Let&#8217;s flesh out a simple version to indicate where each piece is going. </p>
<pre class="brush: java;">
public void getColor(double u, double v, RGBAColor value) {
	double range = calculateCircleRange(u, v) * 1.2;

	if (range &gt; 1) {
		//render shadow
		value.setColor(RGBAColor.black());
		return;
	}		

	value.setColor(color);

	if (range &gt; 0.7) {
		value.setColor(RGBAColor.yellow());
	}
}
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/buttonLayout.png" class="leftImg" /><br />
This gives us a fairly predictable result, you can see where the button ends and the shadow starts and you can see which parts the inner shadow affects. We multiply the range value by 1.2 so we can shrink the circle down a bit to give us our margin around the edge for the shadow to sit in.</p>
<p>Time to add some actual colors to our template starting with the shadow. Our shadow is essentially the color black with the alpha ranging from 0.9 to 0 based on how far the point is from the edge of the button. We make the shadow fall off exponentially so the shadow becomes much lighter faster. Bear in mind that the range value for the shadow area is 1+ so we need to adjust the value into the 0..1 range.</p>
<pre class="brush: java;">
public void getColor(double u, double v, RGBAColor value) {
	double range = calculateCircleRange(u, v) * 1.2;

	if (range &gt; 1) {
		//render shadow
		value.setColor(RGBAColor.black());
		double shadow = Math.pow(2-range-0.1, 8);
		value.setAlpha( shadow);
		return;
	}		

	value.setColor(color);

	if (range &gt; 0.8) {
		value.setColor(RGBAColor.yellow());
	}
}
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/shadowButton.png" class="leftImg" /><br />
We specified a white background in our main class and the shadow has a varying alpha value so the white background show through. We can also change the background to see how it looks on different colors.</p>
<p>Now would be a good time to set up the different colors we&#8217;ll be using in our button. We have our base color passed in to the constructor, and from that, we can calculate the darker button color and the lighter highlight color. Let&#8217;s add those in to the constructor first.</p>
<pre class="brush: java;">
public class GlassButton extends AbstractTexture {

	private final RGBAColor color;
	private final RGBAColor darkerColor;
	private final RGBAColor highlightColor;

	public GlassButton(RGBAColor color) {
		this.color = new RGBAColor(color);

		//make a copy and darken it
		this.darkerColor = new RGBAColor(color);
		this.darkerColor.merge(RGBAColor.black(),0.5);

		//make a copy and lighten it
		this.highlightColor = new RGBAColor(color);
		this.highlightColor.merge(RGBAColor.white(),0.8);
	}
</pre>
<p>With our colors in place, we can start adding them to the final button image. We&#8217;ll start with the main button color which ranges from the base color to dark color as the v value increases and we evaluate pixels further down the image. This can be done with one line of code since we take the base color and merge the darker color based on the v value after we assign the base color to the value.</p>
<pre class="brush: java;">
value.setColor(color);
value.merge(darkerColor,(v-0.1)*1.2);
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/coloredButton.png" class="leftImg" /><br />
We shift and scale the v value to try and make the color transformation apply across the whole face of the button which is scaled down. This gives as a gentle transformation across the face of the button. </p>
<p>The button has an inner shadow which gives us the impression that the edges of the button are bending away from us. The code for this looks at the circle range value and if greater than 0.8 adds the inner shadow based on the distance from the edge. To calculate this, we subtract the 0.8 and scale it to get a value in the range of 0 to 0.4 which is perfect since we don&#8217;t want a strong shadow.</p>
<pre class="brush: java;">
		//add the inner shadow
		if (range &gt; 0.8) {
			double shadow = (range-0.8)*2;
			value.merge(RGBAColor.black(),shadow);
		}
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/innerShadowButton.png" class="leftImg" /><br />
The last piece we need to add is the highlight which is rendered in the shape of an smaller circle that is offset upwards slightly. This means we can reuse our <code>calculateCircleRange</code> method to determine which points are in the smaller circle. Once we have determined whether we are in the highlight circle or not we will merge the highlight color based on the <code>v</code> value so the highlight fades out as we move down the button. </p>
<p>Here is the complete <code>getColor</code> method with the code to add the highlight at the end.</p>
<pre class="brush: java;">
public void getColor(double u, double v, RGBAColor value) {
	double range = calculateCircleRange(u, v) * 1.2;

	if (range &gt; 1) {
		// render shadow
		value.setColor(RGBAColor.black());
		double shadow = Math.pow(2 - range - 0.1, 8);
		value.setAlpha(shadow);
		return;
	}

	// set the base color
	value.setColor(color);

	// add the darker color transform
	value.merge(darkerColor, (v - 0.1) * 1.2);

	// add the inner shadow
	if (range &gt; 0.8) {
		double shadow = (range - 0.8) * 2;
		value.merge(RGBAColor.black(), shadow);
	}

	// finally add the highlight
	double highlightRadius = calculateCircleRange(u, v + 0.05) * 1.5;
	if (highlightRadius &lt; 1) {
		double highlight = v * 2;
		value.merge(highlightColor, Gradient.clip(1 - highlight));
	}
}
</pre>
<p><img src="/blog/wp-content/uploads/2009/08/final.png" class="leftImg" /><br />
This is our final button image which is a round red glass button. However, because this is a procedural texture, we can scale the image in both the u and v directions without loss of quality and we can also create oval buttons. Below you can see a number of different variations. </p>
<p>I&#8217;ve also added an enhancement with regards to the inner shadow. Rather than making it a fixed black color, I have used a <code>ColorGradient</code> so the inner shadow transforms from dark to light from the top to the bottom of the button.  We did this by adding this private member </p>
<pre class="brush: java;">
private final ColorGradient shadowColor = ColorGradient.buildBlackAndWhite();
</pre>
<p>and in our inner shadow code, we use this gradient color instead of the fixed black color :</p>
<pre class="brush: java;">
	// add the inner shadow
	if (range &gt; 0.8) {
		double shadow = (range - 0.8) * 2.5;
		value.merge(shadowColor.interpolate((v+0.3)), shadow);
	}
</pre>
<p>Also, for the last image, instead of using a solid color, I used a <code>ColorGradient</code> from the highlight color to the base color and then the darker color to give it that radial gradient. I plan on writing a generalized version of this texture for inclusion in the next release that will let you set parameters and even pass <code>Texture</code> objects for the main color and the inner shadow.</p>
<p><img src="/blog/wp-content/uploads/2009/08/logoButton.png" class="leftImg" /><img src="/blog/wp-content/uploads/2009/08/oval1.png" class="leftImg" /><img src="/blog/wp-content/uploads/2009/08/lightShadow.png" class="leftImg" /><img src="/blog/wp-content/uploads/2009/08/onMarble.png" class="leftImg" /><img src="/blog/wp-content/uploads/2009/08/purpleRadial.png" class="leftImg" /><br />
<br/></p>
<div style="clear:both">
<script type="text/javascript">var dzone_url = 'http://www.andygibson.net/blog/index.php/2009/09/02/glass-button-tutorial-in-java/';</script><br />
<script type="text/javascript">var dzone_title = 'Glass Button Tutorial In Java';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.andygibson.net/blog/tutorial/glass-button-tutorial-in-java/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>JTexGen &#8211; Procedural Texture Library Released</title>
		<link>http://www.andygibson.net/blog/news/jtexgen-procedural-texture-library-released/</link>
		<comments>http://www.andygibson.net/blog/news/jtexgen-procedural-texture-library-released/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 15:02:53 +0000</pubDate>
		<dc:creator>Andy Gibson</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JTexgen]]></category>

		<guid isPermaLink="false">http://www.andygibson.net/blog/?p=225</guid>
		<description><![CDATA[Download Here I have finally found the time to finally release my library for creating procedural textures called JTexGen under the LGPL license. It is a framework for rendering and viewing procedural textures which you can then use as images, backgrounds and textures for modelling. Because the textures are procedural, you can ramp up the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://kenai.com/projects/jtexgen/downloads">Download Here</a></p>
<p>I have finally found the time to finally release my library for creating procedural textures called JTexGen under the LGPL license. It is a framework for rendering and viewing procedural textures which you can then use as images,  backgrounds and textures for modelling. Because the textures are procedural, you can ramp up the resolution of the image to get increased detail without getting blocky artifacts.</p>
<p><img src="http://www.andygibson.net/articles/jtexgen_release/demoset.gif" alt="Demo Images" /></p>
<p>The source code for the above images is included in the distribution and also discussed in the  <a href="http://www.andygibson.net/articles/jtexgen_release/html/">reference documentation</a>. These images are reproduced untouched after being generated using a few lines of Java code.</p>
<p>The distribution contains the usuals (source, a jar, javadocs can be built in maven) and also a reference manual in pdf and html format. The reference manual describes what procedural textures are and how to use the framework to create them. The library itself is thread safe, and uses multiple threads to render the images.</p>
<p>The files can be downloaded from <a href="http://kenai.com/projects/jtexgen/downloads">Project Kenai</a>, and contains nearly 100 different textures and signals you can use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andygibson.net/blog/news/jtexgen-procedural-texture-library-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Open Source is Hard</title>
		<link>http://www.andygibson.net/blog/article/open-source-is-hard/</link>
		<comments>http://www.andygibson.net/blog/article/open-source-is-hard/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 05:45:32 +0000</pubDate>
		<dc:creator>Andy Gibson</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JTexgen]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Project Kenai]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://www.andygibson.net/blog/?p=328</guid>
		<description><![CDATA[I&#8217;ve been working on getting my procedural texture library completed and released to the public which should be ready next week. I&#8217;m currently going through the difficulties that always go with getting that last bit of polish on a project to get it ready for public consumption. In particular, I&#8217;ve just switched over to maven [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on getting my procedural texture library completed and released to the public which should be ready next week. I&#8217;m currently going through the difficulties that always go with getting that last bit of polish on a project to get it ready for public consumption. In particular, I&#8217;ve just switched over to maven as a build process and moved it into Project Kenai.<br />
<span id="more-328"></span><br />
Even though it doesn&#8217;t have any dependencies, Maven does help shape project structure and lets me deploy it in a structure that includes the source and lets users easily build the jar or the documentation. I&#8217;ve also had to deal with getting it into a subversion repository on Project Kenai. Originally I had it at Sourceforge (without source storage) but I decided to move it to Kenai.</p>
<p>Granted, source control and build management  should have been done early on but this is a small project where I already make regular informal backups and the IDE handles building the single jar so I didn&#8217;t bother. Also, I wasn&#8217;t planning on making it open source originally but since I did, I wanted to get everything tidied away.</p>
<p>On top of that, I&#8217;ve had to produce documentation, more so than if I was just using the library myself, and produce wiki pages, provide uploads, test the downloads to make sure they work, check that the download will work when installed to a maven repository, and provide instruction on how to use it from a maven repository etc. It&#8217;s a lot of work all for the sake of sharing a project with the world. I wouldn&#8217;t have to do most of that if I were keeping it to myself!</p>
<p>However, it has taught me two things. One is to appreciate all the extra work that goes into managing an open source project above and beyond just putting a jar file on a web page for download. The other is a reminder to always start out with good practices (build processes and source control) from scratch if you even think the project might go public. This is a no-brainer if you know the project will be open source in advance, but probably should be followed just in case you decide to open source it later on. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.andygibson.net/blog/article/open-source-is-hard/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

