<?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>CoderDan &#187; Programming</title>
	<atom:link href="http://www.coderdan.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coderdan.com</link>
	<description>My thoughts on programming, technology and business</description>
	<lastBuildDate>Tue, 13 Dec 2011 23:34:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP foreach and references</title>
		<link>http://www.coderdan.com/2011/09/php-foreach-and-references/</link>
		<comments>http://www.coderdan.com/2011/09/php-foreach-and-references/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 14:52:43 +0000</pubDate>
		<dc:creator>Daniel Negrea</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.coderdan.com/?p=93</guid>
		<description><![CDATA[The foreach construct is an usual way to iterate over an array. Since PHP 5, you can also modify the array values using references. Here is one interesting side effect: &#60;?php $a = range&#40;1,5&#41;; foreach&#40;$a as &#38;$x&#41; &#123; $x++; &#125; &#160; foreach&#40;$a as $x&#41; &#123; echo $x; &#125; The output is 23455. This happens because [...]]]></description>
			<content:encoded><![CDATA[<p>The <code>foreach</code> construct is an usual way to iterate over an array. Since PHP 5, you can also modify the array values using references. Here is one interesting side effect:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The output is <code>23455</code>. This happens because the <code>$x</code> reference remains even after the <code>foreach</code> loop ends. The reference is destroyed if you unset <code>$x</code> after the loop.</p>
<p>This is not very well known. If you have the misfortune to maintain some scripts with a coding style involving little structure, lots of spaghetti code and nested includes than you might be very puzzled by the behavior of a regular foreach loop just because the iterating value was used previously as a reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderdan.com/2011/09/php-foreach-and-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google AI Challenge</title>
		<link>http://www.coderdan.com/2011/06/google-ai-challenge/</link>
		<comments>http://www.coderdan.com/2011/06/google-ai-challenge/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 00:25:46 +0000</pubDate>
		<dc:creator>Daniel Negrea</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AI]]></category>

		<guid isPermaLink="false">http://www.coderdan.com/?p=88</guid>
		<description><![CDATA[Artificial inteligence(AI) is a complex branch of computer science involving a lot of research by private and academic entities. The Google AI Challenge brings bot writers around a simple and fun idea. The idea is to write a bot to play a strategy game against other bots. In the next challenge the bots will play [...]]]></description>
			<content:encoded><![CDATA[<p>Artificial inteligence(AI) is a complex branch of computer science involving a lot of research by private and academic entities.  The Google AI Challenge brings bot writers around a simple and fun idea. The idea is to write a bot to play a strategy game against other bots. </p>
<p>In the next challenge the bots will play an Ants game where they control an ant colony fighting for resources against another colonies. The system is functional but in beta version. There is a set of APIs for most programming languages, a couple of tutorials and the source code for the system itself is available. </p>
<p>The University of Waterloo Computer Science Club is organizing the contest with Google as a sponsor. There are no listed prizes but for people with some spare time it will provide quite enough entertainment value. I hope to have a least a bit of spare time for this and play a bit with it.</p>
<p>Most information is on the <a href="http://ai-contest.com/forum/index.php">forum</a> and there is also a <a href="http://aichallengebeta.hypertriangle.com/">beta site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderdan.com/2011/06/google-ai-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intuitive Zoom with JScrollPane</title>
		<link>http://www.coderdan.com/2010/09/intuitive-zoom-with-jscrollpane/</link>
		<comments>http://www.coderdan.com/2010/09/intuitive-zoom-with-jscrollpane/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 02:01:33 +0000</pubDate>
		<dc:creator>Daniel Negrea</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JScrollPane]]></category>
		<category><![CDATA[zoom]]></category>

		<guid isPermaLink="false">http://www.coderdan.com/?p=68</guid>
		<description><![CDATA[The problem: Display an image inside a panel and handle different zoom levels. The best approach is to use an already made component. If a component is not available or you just have to write it using only the basic Swing components than might help you save some time. One approach is to hold a [...]]]></description>
			<content:encoded><![CDATA[<p>
The problem: Display an image inside a panel and handle different zoom levels. The best approach is to use an already made component. If a component is not available or you just have to write it  using only the basic Swing components than might help you save some time.</p>
<p>
One approach is to hold a BufferedImage and the zoom level(integer, initial 100) and draw the image with appropriate settings in a JPanel. Overwrite the paintComponent function of a JPanel. Set the size of the panel to match the size of the zoomed image. Add this panel inside a JScrollPane and you have an initial version. Enable double buffering for the resulting panel to have a smooth appearance.</p>
<p>
I prefer this approach instead of using a zoomed copy of the image as it saves a lot of memory.</p>
<p>
The positions on the drawing panel match the positions on the zoomed image. The coordinates in the original image are calculated using a simple formula:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> imageX <span style="color: #339933;">=</span> panelX <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">/</span>zoom<span style="color: #339933;">;</span></pre></div></div>

<p>
If the zoomed image is smaller than the container than the image will be in the top right corner. Most users will want it in the center of the container. The solution is to wrap the drawing panel inside a centering panel. There a couple of solutions using a custom layout manager, a combination of Box layouts, SpringLayout, etc. A simple solution is the GridBagLayout:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">JPanel</span> centeringPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JPanel</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GridBagLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
centeringPanel.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>drawingPanel, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GridBagConstraints</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will center the drawing panel vertically and horizontally.</p>
<p>
When changing the zoom, change the size of the drawing panel and call invalidate() and repaint() on the scroll pane view port. Now you have zoom in and zoom out.</p>
<p>
You will notice a small problem. The zoom is not very intuitive, it will move the image to the top left corner. A more usable panel would zoom in/out preserving the view on the same area of the image. You need to calculate the new view position. There a couple of ways to do this I will present one of them:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Save the previous coordinates</span>
<span style="color: #000066; font-weight: bold;">int</span> oldZoom <span style="color: #339933;">=</span> zoom<span style="color: #339933;">;</span>
<span style="color: #003399;">Rectangle</span> oldView <span style="color: #339933;">=</span> scrollPane.<span style="color: #006633;">getViewport</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getViewRect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// resize the panel for the new zoom</span>
....
<span style="color: #666666; font-style: italic;">// calculate the new view position</span>
<span style="color: #003399;">Point</span> newViewPos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
newViewPos.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #009900;">&#40;</span>oldView.<span style="color: #006633;">x</span> <span style="color: #339933;">+</span> oldView.<span style="color: #006633;">width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> newZoom <span style="color: #339933;">/</span> oldZoom <span style="color: #339933;">-</span> oldView.<span style="color: #006633;">width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
newViewPos.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #009900;">&#40;</span>oldView.<span style="color: #006633;">y</span> <span style="color: #339933;">+</span> oldView.<span style="color: #006633;">height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> newZoom <span style="color: #339933;">/</span> oldZoom <span style="color: #339933;">-</span> oldView.<span style="color: #006633;">height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
scrollPane.<span style="color: #006633;">getViewport</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setViewPosition</span><span style="color: #009900;">&#40;</span>newViewPos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
You can add many usability improvements and optimizations but what I described here is an easy to implement and usable component that can be used in any application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderdan.com/2010/09/intuitive-zoom-with-jscrollpane/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Create a Pdf in Php Using Tcpdf</title>
		<link>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/</link>
		<comments>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 16:40:30 +0000</pubDate>
		<dc:creator>Daniel Negrea</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tcpdf]]></category>

		<guid isPermaLink="false">http://www.coderdan.com/?p=28</guid>
		<description><![CDATA[There are several ways to generate PDF files in PHP. Each has its strengths and weaknesses. TCPDF strikes a good balance although it is far from ideal.]]></description>
			<content:encoded><![CDATA[<p>There are several ways to generate PDF files in PHP. Each has its strengths and weaknesses. TCPDF strikes a good balance although it is far from ideal.</p>
<p>The best way to transform some web page to php is to let the user print it. With some free software like <a href="http://www.pdfforge.org/">PDFCreator</a>, you can get far better results than anything you can achieve using the available php libraries. Sometimes a client really wants this option or you need to generate some kind of report on the fly. This is when <a href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf">TCPDF</a> is a good choice.</p>
<p>There is no special handling to use the library. You can download it, unpack it and include it in your project. For some advanced features, you might need to allow writing in the <em>cache</em> and <em>images</em> folders. To use it, you include the <strong>tcpdf.php</strong> file and some configuration file. You will need to make a custom configuration for more advanced features but the default one will work in most cases.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you use the library from a cronjob or command line you might get this error: <strong>PHP Notice:  Undefined variable: k_path_url in tcpdf\config\tcpdf_config.php on line 75</strong></p>
<p>This is where a custom configuration file is useful but the fastest way to fix it is to just initialize that variable ahead of the config file. It will work for most of the tasks.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$k_path_url</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The functionality is available using the <strong>TCPDF</strong> class. I think the OOP approach stops here because there are no other classes used (with a minor exception) and I assume the author took this path to compensate for the lack of name spaces in PHP.</p>
<p>The flow is simple: create a TCPDF class, call its methods to generate pages, text and drawings and then save the content. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create new PDF document</span>
<span style="color: #000088;">$pdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TCPDF<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;">// set font</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'times'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// add a page</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddPage</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;">// print a line</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cell</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Some text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print html formated text</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeHtml</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html text:&lt;br /&gt;&lt;b&gt;Bold&lt;/b&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// draw a circle</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Circle</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Close and output PDF document</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Output</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'out.pdf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The library includes a set of examples to demonstrate what it can do.</p>
<p>A TCPDF object maintains internal coordinates used to write text and html with Cell and writeHtml. These methods (and some other related) use these coordinates to maintain the flow of text on the page and add new pages if needed.  They also take into consideration the page margins and headers. The drawing functions are relative to the origin of the page and do not use the Cell coordinates system. This will complicate the job for reports requiring a good layout and a lot of drawing.</p>
<p>The html support is limited. The html needs to be well formatted and only a limited set of tags is supported. There is no error handling for unsupported html so in the best case you will only get some strange php warnings like division by zero and others. You will have to specify the width of the table cells to maintain a minimal control of the layout because the tables are not rendered/positioned as in an usual browser.  I advise against using tables to control the layout of the pdf pages. I assume you read something similar as a web developer.</p>
<p>As a conclusion, I avoid generating complex PDF files in php. TCPDF is the best for the task but it has limited functionality, a poor API and little documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

