Thursday, April 26, 2007

Read data from a URL and show them on screen with Java

The following code reads data from a URL and then it shows them on screen. This is an amateur example of the use of packet. If you are an experienced user, you can use ContentHandler and ContentHandlerFactory to choose who can be a data controller.
public class ReadUrl
public static void main (String[] args)
for (int i = 0; i < url =" new" connect =" url.openConnection();" in =" connect.getInputStream();" bytes =" new" len ="">= 0)
System.out.write(bytes, 0, len);

Wednesday, April 25, 2007

Tips for debugging your web pages

After building your site, it comes the hard stuff: testing and debugging the page. There are a few rules that you can follow to make your life easier:
  1. Make sure that you spelled everything correctly (for example img is not written igm)
  2. Make sure that the Doctype of your pages matches the (x)html you are using (for example if you use deprecated tags in your code, you should choose (x)html transitional instead of (x)html strict).
  3. Follow the rules of nesting. If you open a <p> tag and then a <a> make sure that the closing </a> comes before the closing </p>
  4. Avoid the use of non-standard tags as they may lead browsers to conflicts.
  5. For elements that have content avoid the use of <p ...... /> and prefer <p> ...... </p>. Both of them are valid (x)html but the first may cause problems to some older browsers. Use the first method though for elements that are empty or contain data, for example <img src="picture.jpeg" alt="picture" />
  6. Validate your code using W3C's validator. If you see a lot of mistakes to your code do not panic, as a missing closing tag can cause the validator to show lot of mistakes. Begin from top to bottom, correct few mistakes at a time, validate again and continue correcting after with the remaining mistakes. Tip: Make sure you use a correct Doctype before you validate your markup!
  7. Download the most famous and common used browsers (Firefox, Internet Explorer, Opera) and see how your site appears by yourself.

Tuesday, April 24, 2007

Build a digital clock with Java

Here is a piece of code to build a Java clock applet that uses threads, gets the data for time automatically and is not interrupted if you click something else on the web page. There are some comments around the code to help you understand what's happening. Program build and tested with Netbeans 5.5. If I find a way, I will upload the applet here to see it running in real time!
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.text.DateFormat;
// builds the clock
class MyClock extends JApplet implements Runnable
{ private Thread myThread; // the thread
private JLabel clock; // the label
private final int interval = 1000; // time in
// milliseconds
public void init()
{ clock = new JLabel("", SwingConstants.CENTER);
clock.setFont (new Font ("Verdana", Font.BOLD,
setSize(250, 100);
public void start()
{ if (myThread == null)
{ myThread = new Thread(this, "clock");
} // start

public void run()
while (myThread == Thread.currentThread())
{ // gets data for the clock

Date time = new Date();
{ //thread sleeps for a sec
catch (InterruptedException e) {}
} // run
public void stop()
myThread = null;

Draw stars using Java

Here is a tutorial to make your way into drawing graphics with Java. This program creates a semi-circle line of stars. The stars are randomly colored. I made the program using Netbeans 5.5, and I tested the code also with Sun Java Studio Enterprise 8.1. Both of them are free. The output is what you see above (star color may differ as they are randomly colored).
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;

class psy2k extends JFrame {
public psy2k()
super( "Psy2k star draw" );

setBackground( Color.white );
setSize( 400, 400 );
public void paint( Graphics g )
int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };

Graphics2D g2d = ( Graphics2D ) g;

GeneralPath star = new GeneralPath();
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );

for ( int k = 1; k < xPoints.length; k++ ) star.lineTo( xPoints[ k ], yPoints[ k ] ); star.closePath();
g2d.translate( 200, 200 );
for ( int j = 1; j <= 20; j++ ) {
g2d.rotate( Math.PI / 20.0 );
g2d.setColor( new Color( ( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ) ) );
g2d.fill( star );
public static void main( String args[] )
psy2k app = new psy2k();
app.addWindowListener( new WindowAdapter()
public void windowClosing( WindowEvent e )
System.exit( 0 );

Monday, April 23, 2007

Download my first Wallpaper!

I made an attemp to make a wallpaper with Adobe Photoshop CS2. I used lot of gradients and layer effects. Feel free to download it by clicking the folder icon below and tell me your comments. I love minimal things: that's why my wallpaper looks like this. In the .zip file there are three versions of the wallpaper: 1600x1200, 1280x1024, 1024x768 pixels. If you use a different screen resolution feel free to mail me and I will send you a version that fits your screen.

Download Wallpaper

Read and print a serial file using C

If you have a serial .dat file that has data like these below, you can easily import and use them with C (for C++ it is almost the same).
Sample file:
If the file is named customers.dat, use the code below to read and print the data with C.
#include <stdio.h>
int main()
int number;
char name[30];
double bill;
FILE *cfPt; /*File pointer .dat */
if ((cfPtr=fopen("customers.dat", "r"))==NULL)
printf("File could not be opened\n");
else {
printf("%-10s%-13s%s\n", "Number", "Name", "Bill");
fscanf(cfPtr, "%d%s%lf", &number, name, &amp;amp;amp;amp;bill);
while (!feof(cfPtr)) {
printf("%-10d%-13s%7.2f\n", number, name, bill);
fscanf(cfPtr, "%d%s%lf", &number, name, &amp;amp;amp;amp;bill);
return 0;

Friday, April 20, 2007

Validate the Youtube embed code!

I wrote about it here and it is really usefull. As we now the embed code of youtube is a mess as it uses the embed tag which does not validate as xhtml. Using the code this tip you can embed a youtube or google video to your site or blog and keep your code validated:
<object type="application/x-shockwave-flash" data="" width="400" height="326"><param name="movie" value="" /></object>
The moviecode is a code found in each youtube video adress and is after v= until the first ampersand (&). For example in this URL, the code is xmHHjUZPyMY.