Rich Theory

My Ads

GD Library Sample

I just created an uninspiring graphic to use on a web site. I will put the code below. This purpose of this post is to vent about poorly written documentation.

Venting about Java

I feel like a dolt. I want to add graphics to the Rich Theory project. I lack a decent paint program; so, I decided to try doing the graphics with applets.

I spent a week going through all the different tutorials for Java Applets I could find on the net. After working for a week, I found I couldn't determine if I should use Java Graphics, Java Graphics2D, Swing or something else. I want graphics that are 640x480. Several sites warned against using double buffering for applets over 500px wide. Several of the sample applets I found on the net froze my browser.

Trying to figure out which commands are deprecated on the poorly organized java.sun.com site is a chore in itself.

Anyway, after a frustrating week. I sat down with the PHP GD Library and had a sample graphics written 15 minutes later.

Altough Java is a stronger language, I will probably end up doing the whole project in the GD Library for the very simple reason that Java fails to let new users know current best practices.

The program below isn't great. The point of this post is about time.

Code Sample

I was able to pound out the following code in 15 minutes because the PHP documentation was organized.

// This PHP code makes a PNG image.
// Coded by kd on 6/9/2008
header ("Content-type: image/png");
$im = @imagecreatetruecolor(400, 240)
      or die('Cannot Initialize new GD image stream');

// define the colors for the pallette.      
$sky_color = imagecolorallocate($im, 200, 200, 255); 
$grass_color = imagecolorallocate($im, 120, 255, 120);   
$pole_color = imagecolorallocate($im, 200, 28, 90); 
$text_color = imagecolorallocate($im, 14, 80, 50); 
$line_color = imagecolorallocate($im, 14, 14, 233);
$box_color = imagecolorallocate($im, 233, 233, 200);

// Set background colors.
imagefilledrectangle($im, 0, 0, 399, 100, $sky_color);
imagefilledrectangle($im, 0, 102, 399, 239, $grass_color);
// The box showing relations has a border.
imagerectangle($im, 299, 109, 391, 231, $text_color);
imagefilledrectangle($im, 300, 110, 390, 230, $box_color);

// X marks the viewer's spot
imagechar($im, 3, 16, 96,  'X', $text_color);

// draw the telephone poles
for ($i=0; $i < 6; $i++) {
  $polePos = 80 + (60 * $i);
  $relTop = 120 + 100 - floor( 100 / ($i + 1)); 
  $relPos = 310 + (12 * $i);
  imagefilledrectangle ($im ,  $polePos , 10 ,  $polePos+5 , 100 , $pole_color );
  imagefilledrectangle ($im ,  $relPos , $relTop ,  $relPos+5 , 220 , $pole_color );
  imageline($im, 20, 100, $polePos, 10, $line_color);
}
// Define then write a paragraph of text.

$textArray = array(
'You are at position X looking at a string',
'of evenly spaced telephone poles.',
'The point where the blue line crosses the',
'first pole determines the relative size ',
'of the pole as seen from your point of',
'view. See box to the right.');

$textTop = 125;
foreach ($textArray as $text) {
  imagestring($im, 3, 10, $textTop, $text , $text_color);
  $textTop += 15;
}

imagepng($im);
imagedestroy($im);

Previous ~ ~ Start ~ ~ Resources ~ ~ Next
575 page views

index - y-intercept - Salt Lake - sponsors