Wednesday, September 19, 2007

Java packages you should know and use

Here is a list of the main and most commonly used java packages. Packages are imported using import <package name>;, for example import java.net.*;

  • java.applet: includes class Applet and other methods that allow applet creation. javax.swing.JApplet is used when an applet enhances GUI swing components
  • java.io: includes classes for data input and output
  • java.net: includes classes for network programming like socket and datagram programming.
  • java.rmi: incudes classes and methods for creating applications for distributed computing.
  • java.security: includes classes for authorisation and data encryption.
  • javax.swing: incudes classes for GUI.
  • java.sql: incudes classes for connection with SQL databases.

Monday, July 30, 2007

Create a Colour Chooser Window with Java

Here a simple piece of code on how you can easily create a Colour Chooser window. The program uses JColorChooser method which is part of the java.swing packet. The code is:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ColorChooser extends JFrame {
private JButton changeColor;
private Color color = Color.lightGray;
private Container c;

public ColorChooser()
{
super( "Using JColorChooser" );

c = getContentPane();
c.setLayout( new FlowLayout() );

changeColor = new JButton( "Change Color" );
changeColor.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( ColorChooser.this,
"Choose a colour", color );

if ( color == null )
color = Color.blue;

c.setBackground( color );
c.repaint();
}
}
);
c.add( changeColor );

setSize( 400, 130 );
show();
}

public static void main( String args[] )
{
ColorChooser app = new ColorChooser();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}

User is able to choose colors using RGB, HSB or Swatch modes.

Wednesday, May 16, 2007

The bad co-worker: recognise him!

Everybody of us has experienced the phenomenon "bad co-worker", but let me explain what I learned from my experience (web-developing, coding, graphics):
  1. He (the bad co-worker) designs a layout and then he sends it to me for slicing. BUT when I open the layout in Photoshop, I realise that it consists of 200 layers, ungrouped and named with names "layer 1, layer 2, layer 3, ......., layer 200". Believe me pal it is much easier for me to deal with grouped layers in categories like Header, Footer etc and names like Title, sidebar background etc than guessing or right clicking its layout's component to find the appropriate layer.
  2. If I am out of luck at all the layout file is merged down and in .jpg format with the sample text (which should be deleted) on it.
  3. When he writed code, he does not use "ENTER" button for changing line. The code is a mess, without comments and looks like simple text.
  4. When his pc has a problem, he asks the others first and then tries to fix it on his own.
  5. He prefer asking the others for finding information (like a website's URL) than using Google.
  6. His computer is a mess. The desktop is full of icons and files are totally unorganised so he (and everybody else) cannot find them when it is needed.
  7. When a serious problem occurs in his pc, it means data-loss as he does not know the very simple word "BACKUP".
  8. He leaves on time even if he is a click away from completing a serious task.

Thursday, May 10, 2007

Create a drop-down menu for image selection using java

The following code uses JCombo class for creating a drop down menu that provides the user the ability to choose and see the selected image. Note that the images you want to inlcude in your app should be saved in the main directory of your program (if you use netbeans for example open the project folder and save the images there).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ComboBoxTest extends JFrame {
private JComboBox images;
private JLabel label;
private String names[] =
{ "cutlass_32.png", "pet-monkey_32.png",
"pirate-captain_32.png", "first-mate_32.png" };
private Icon icons[] =
{ new ImageIcon( names[ 0 ] ),
new ImageIcon( names[ 1 ] ),
new ImageIcon( names[ 2 ] ),
new ImageIcon( names[ 3 ] ) };

public ComboBoxTest()
{
super( "Psy2k-Photo Selection" );

Container c = getContentPane();
c.setLayout( new FlowLayout() );

images = new JComboBox( names );
images.setMaximumRowCount( 3 );

images.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent e )
{
label.setIcon(
icons[ images.getSelectedIndex() ] );
}
}
);

c.add( images );

label = new JLabel( icons[ 0 ] );
c.add( label );

setSize( 350, 100 );
show();
}

public static void main( String args[] )
{
ComboBoxTest app = new ComboBoxTest();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
The icons I used in this app are from Iconbuffet's Amsterdam High Seas collection (pirate icon yeah). Iconbuffet is a free social community for trading small icons with others. If you would like to join the madness feel free. It is very addicting!

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 java.net packet. If you are an experienced user, you can use ContentHandler and ContentHandlerFactory to choose who can be a data controller.
import java.io.*;
import java.net.*;
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 =" in.read(bytes))">= 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,
28));
clock.setBackground(Color.BLUE());
clock.setForeground(Color.WHITE);
clock.setOpaque(true);
getContentPane().add(clock);
setSize(250, 100);
}
public void start()
{ if (myThread == null)
{ myThread = new Thread(this, "clock");
myThread.start();
}
} // start

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

Date time = new Date();
clock.setText(DateFormat.getTimeInstance
(DateFormat.MEDIUM).format(time));
try
{ //thread sleeps for a sec
myThread.sleep(interval);
}
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 );
show();
}
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);
}
fclose(cfPtr);
}
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="http://www.youtube.com/v/moviecode" width="400" height="326"><param name="movie" value="http://www.youtube.com/v/moviecode" /></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 http://www.youtube.com/watch?v=xmHHjUZPyMY, the code is xmHHjUZPyMY.

Sunday, February 25, 2007

Microsoft Word became crazy?

Searching around on the Internet I found this trick: Open an empty Word file (I have not tested it in Word 2007), type = rand (200,99) and press enter. Wait for 3 seconds and then a lot of pages will be filled with a repeating sentense (I use the Greek version of Word 2003 and the sentense that appears is part from a poem). As I released after, this is not a bug, it is an easy way to transfer text and Microsoft knows that it happens. If with a single line like this, Word could write down pages from my exercises, it would be for sure my favorite program to use.

Tuesday, February 13, 2007

Blogger navbar and Internet Explorer 7

As you may have noticed, if you use Blogger with a blogger template, Internet explorer 7 displays your blog with a problem on the top (Blogger nav bar is hiding part of the header). The solution for this problem is to put some html code into your template and hide the navbar (very few people use the nav bar, so it is better to hide it and have your site being displayed correctly). Between <head> and </head> tags paste the following code under the */layout*/ line. (everything between /*....*/ is comments and it does not affect your site, so be carefull to paste the code under the comment line).
#navbar-iframe {
height: 0px;
visibility: hidden;
display: none;
}
Save your template, view your blog and enjoy how Internet Explorer 7 displays it!

Monday, January 22, 2007

How to find your MAC adress

It is very easy to discover your MAC adress (MAC adress is needed sometimes to make operations on your network or router). So follow these steps and you will discover your MAC adress:
  1. Click Start, Run and type cmd in the text box
  2. When the command promt window opens, type ipconfig/all
The 12-digit Physical adress is the same as your MAC adress.
You will have multiple MAC adresses if you have both wired and wireless connection on your system.

Monday, January 15, 2007

Another way to track your blog traffic

Searching through the Internet, I found 103bees. 103bees is a service, with which you can track the visitors of your blog or site. I use it alongside with Google Analytics, because 103bees offers the option to see what search terms did users entered and reached your pages through search engines (like google, yahoo and live) and what was your page ranking in the results. It is very easy to track your visitors with 103bees, you just have to register an account (it is completely free), add some code to every page you want to track (the codes loads up very quickly and validates for w3c standards) and you are ready. Although 103bees is very usefull, it cannot replace completely Google Analytics, as Analytics offer the geo map overlay, marketing information about your site and various other statistics.

Friday, December 22, 2006

Create a shortcut to shut down your PC

Did you know that you can log off, shut down or restart your pc with a single click? Here is the way: Right click on the desktop and select create, new shorcut. Write (or browse the path) C:\Windows\System32\Shutdown.exe (where C is the letter of the drive on which Windows are installed to) and click next. Name your shortcut as you want and click finish. Then single click the shortcut you created and select properties. In target field add to the command line -1 if you want log off function for your shortcut, -s if you want shut down or -r if you want restart function. After adding this, you can add also -t x (where x is seconds) if you want a countdown until your system shuts down or restarts. Also you can add -c "text" (where text write anything you want) if you want a text message to appear when you click your shorcut.

Saturday, December 16, 2006

10 tips for a successfull blog!

Here are ten tips that in my opinion are the element for a successfull blog:
  1. Write quality content. Quality content means to write on topics that other would like to read and content that you know well because to attract readers you have to write something that is not mentioned by everyone.
  2. Categorize your blog when you create it. Blogs that covers a lot of irrelevant subjects at the same time are not successfull most of the times.
  3. Write for readers and not for Google and other search engines. First of all you have to think about what would attract more readers to your blog and then how to make search engines index it.
  4. Start promoting your blog to blog catalogs after you have written some posts, so readers can read many posts. A blog with a single post does not attract anybody. This will help you also with the search engines.
  5. Put a foto on your profile to blog catalogs like technorati. People remember photos and are attracted by them.
  6. Read other blogs and try to come to contact with other bloggers by posting comments on their blog or by sending them an email. This will help you build relationship with other bloggers and make them come to your blog.
  7. Try to gain inbound links. The best way to achieve it is by writing quality contect, so others would like to link to your posts or to your blog. Links from pages with better pagerank than yours is a plus for your blog's indexing by google.
  8. Have quality outbound links. Outbound links will help you a little with search engines and when you link to a blog or post you liked, you will probably get an inbound link by the blogger to whose post or blog you linked.
  9. Have an easy-to-read blog, which means add labels and tags if you are able to, write with fonts that can be read easily and avoid black backgrounds. It is better if you include some pictures relevant to your posts, because posts with text only are not so attractive. Be carefull if you use photos from the web to your blog, as some of them are copyrighted.
  10. Do not advertise your blog (spam) to other blogs by leaving comments that include your url. Anyone that would like to read your posts can find your blog's url in your profile.
Bonus tips: Be patient as indexing by search engines and increasing the traffic of your blog can take time. Update your blog regularly so you can keep your readers, as a blog that is not updated for a long time seems as abandoned to others. If you will not post for a while (for example when you are on vacation) write a post to inform your readers about it.

Wednesday, December 06, 2006

Web 2.0 Logo made with Photoshop!

I designed a web 2.0 logo using Adobe Photoshop! Thanks a lot to Alleba for his great video guide! Visit this link and create your own web logo. It is much more easier than you can imagine! Below you can see Alleba's video guide.

Saturday, December 02, 2006

How to speed up your computer's boot

As you may have noticed, when Windows boot, a lot of programs start running in the background (you can see these programs alongside with windows's processes in the task manager -processes tab- by pressing ctr+alt+del. Most of them put also an icon in system tray). These programs of course take time to load, which means that Windows take more time to boot! Most of the times, programs that run in the background are not needed, so you can eliminate your system's boot time by disabling them. Lets start:
  • Press start and then select run
  • Type msconfig and press enter
  • In the window that appears choose the Startup tab
  • Here is the list of programs that load when you boot your system and continue running in the background. Disable any program that you want, click "apply" and then "ok". Restart your system when you are promt to and enjoy how faster your pc boots!
Notice that it is safe to disable all these applications! They have nothing to do with the services that run also in background. You can leave in the startup list any program you want to load when you boot up your system (for example msn messenger or skype).

Sunday, November 26, 2006

Modify the RSS icon for your blog or site!

The RSS feed icon has an orange colour by default. According to the RSS icon's license you are free to change its colour so the icon fits well to your site. There are two ways to change the colour: one using Adobe Illustrator (which means change the colour manually) and one using Adobe Photoshop. Illustrator gives a little bit better results but this way is more difficult if you are not an Illustrator user. Anyway most of the times we use small versions of RSS icon, so its hard to notice a difference. I will show you the "easy" way using Photoshop. Lets start! First of all download the feed icons from here (there are PSD, EPS, JPEG and PNG versions of the icon in the zip file). Unzip the file and:
  • Open the greyscale folder and choose the .psd icon you want (for example the 128x128 which is the biggest dimensions offered).
  • After you open the file with Photoshop, check if the layer pallete is visible. If not press F7 to make it visible.
  • Right click on the "Feed icon" layer in layers pallete and select "Blending Options"
  • Select "Color Overlay" on the left. This will make the entire icon solid red!
  • Change the "Blend mode" to color as shown in the photo.
  • Click on the color swatch and a select the color you want.
  • Click ok and then go to File, save for web and then save the file in png 24 format and you are ready! I made the icon green and that is how it looks like!

Thursday, November 23, 2006

How to add a favicon to your site!

Favicon is a small icon displayed beside the url of a site and beside the site name on the tab (if you use a browser that supports tab browsing). You can see a favicon in the foto below.

Lets see how you can add your own favicon to your web site or to your blog. A favicon is a 16x16 or 32x32 image saved in .ico or .gif or .png format. You can transform your image to these dimension using a photo editing program like Adobe Photoshop. If you do not want to do it by yourself or don't have a photo editing program you can generate your favicon here.
  • Upload the photo you want, click generate and
  • Download the Zip File that contains the favicons.
Now upload the favicon to your server (where you host your site). If you use Blogger, you cannot upload files. There are two ways to use favicon with Blogger. You can upload the favicon to another server that you have access to and link through blogger template there or if you have not access to a server you can create a new post called "Favicon", upload the favicon image (you are allowed to upload images in posts in Blogger), publish the post, click on the image and copy the url. When you have finally upload one way or another the favicon you have to modify the HTML template of your site or blog:
  • Find the </head> tag in your template.
  • Exactly before the </head> tag, paste the following code:
<link rel="icon" href="[URL]"
type="image/x-icon"/>
<link rel="shortcut icon" href="[URL]"
type="image/x-icon"/>


Where [URL] you should enter the url of the favicon (the one that you copied before).
  • Save your html file, or template if you are a Blogger user, publish and enjoy your favicon.
If you generated your favicon from the site I gave above and you would like to use the animated favicon, you should upload the animated favicon to your server (or upload it via a post for blogger users) and you should enter the following code to your template in order to see the favicon's animation:

<link rel="shortcut icon" href="[URL]"/>
<link rel="icon" href="[URL]" type="image/gif"/>

If you use the code for the static favicon to the animated one there is no problem, but you want be able to see the animation in some browsers. You can validate your favicon here.

Sunday, November 19, 2006

Google Webmaster's tool to your blog!

Google analytics lets you track your visitors and be informed about how they see your blog. Time to be informed on how Google and its crawler (Googlebot) see your blog. Go the Google Webmasters page, sign in using your Google account and click "Webmasters tools (including sitemaps)". Type your blog or site url (the procedure is tha same) and click "add". Then you will be asked to verify your url by adding a META tag or by uploading an HTML file. If you use blogger select verify via META tag. Select the META tag, right click, copy. Then sign in to blogger, open the edit template window and find the < /head > tag. Paste the META tag exactly before the < /head > tag. Click save template changes. After you placed the META tag go to Google Webmasters again and click "Verify". You will be promted that your blog was verified successfully and now you can get full information on how your blog is crawled by Google. Information appears after 5-6 days (some information like page rank or crawl/index stats appear later). You can see when Googlebot last accessed your url, if your site is included in Google index, and if it is included, what page rank it has and various other query, index and crawl stats. If you had modified robots.txt and you do not allow Google crawlers to access a specific url in your site, you can see it in Google Webmasters too. Information on how to modify your template for not allowing access to search engine crawlers in a few days.

Thursday, November 16, 2006

Google Analytics to your Blog!

Google Analytics is a powerfull online free tool by Google, which helps you track your web site's visitors. It provides many information, like the reffering source for where the visitors came to your site, or where do they live, what browser or operating system do they use, how many of them visited back your web site and more. Now to the point:
  1. Visit Analytics site and sign up using your Google account (if you do not have one, you will be promted to create one).
  2. After that, sign in and click the "add website profile" button.
  3. Enter the URL of your blog and click next.
  4. Google gives you a tracking javascript code to place to your blog.
  5. Sign in to blogger and open the edit template window.
  6. Go to the end of the template code and find the < /body > tag.
  7. Place the code exactly before the < /body > tag (this is recommended for the tracking to work properly).
  8. After you place the code, click save settings and republish your blog for the changes to take effect.
  9. Now go to Google analytics again and click done. After clicking done the main page of your analytics account loads, where you can see that the tracking is installed, and that analytics is waiting for data from your site. Usually it takes up to 24 hours for the first data to appear in anlytics. After that data are updated every 8-10 hours. That means that you can not see with Google Analytics how many users are online in your site at a specific moment.
That's all. When you want to see information about your site visitors, sign in to Google Analytics, and click on your site's URL.

Monday, November 13, 2006

How to add a Youtube video in your blog!

First of all find the video that you want to embed in your blog in YouTube. On the right of the video there is a column below tags and above related videos list which is named "Embed"! Select the code that is written in the column, right click and then copy. Go to your blog, write the post in which you want to include the video. Then click the "edit HTML" tab (for blogger users, it is next to compose on the up right side of "create/edit post" window) and paste the code you copied from Embed column in Youtube. Be sure you paste the whole code (check that the code you pasted starts with tag and ends with tag). Click publish, view your blog and you are ready to watch the video you embeded!

Sunday, November 12, 2006

Migrating!

My blogs migrated to New Blogger (it should not be called beta anymore I think...). The migration was very easy (I owned a google account) except the fact that I had to customize my sidebar again because all of the HTML code I have added to my template (buttons, links, hit counter etc) was gone... Although the customisation of the template in New Blogger is very easy, you click the "Add HTML/Javascript" button, you enter the code in the popup window and that's it. You do not have to manually align buttons, counters and links or looking through the entire template to decide where to enter the HTML code so it appears exactly where you want the item to appear. The WYSIWYG (What you see is what you get) editing environment is much more usefull. If you decide to switch to new blogger, do not forget to enter again yout META tags in head section.