Saturday, June 30, 2007

Google Developer Day 2007

What's a day, I wish I could be there :'(

Firstly check this video http://youtube.com/watch?v=the0KZLEacs snapshots of Google Developer Day.

Then check the sessions listed in this search
http://youtube.com/profile_videos?user=GoogleDeveloperDay&p=r

Day page
http://code.google.com/events/developerday/

Wednesday, June 20, 2007

ITL, Masha'a Allah

The Islamic Tools and Libraries (ITL) is a project to provide a plethora of useful Islamic tools and applications as well as a comprehensive feature-full Islam-centric library. The ITL project currently includes Hijri date, Muslim prayer times, and Qibla.

There is also Minbar, a Gnome GUI with tray icon, athan call and notifications at prayer times.

http://www.arabeyes.org/project.php?proj=ITL

Tuesday, June 12, 2007

JUnit - The ever easier

Really how amazing and powerful is that framework. JUnit is in role one of the most well-known unit testing frameworks for Java.

It's the issue of testing component by component, integrate, test again and fix and so ... . Unit testing applies test driven development aspects. If you're familiar with Extreme programming you'll feel it the most suitable for you.

Let start with a simple example describing how to test a simple calculator using JUnit 3.8 and Eclipse IDE 3.2.2.1.

1. Create a new Java Project, name it "JUnit Calculator Test"



2. Create a new class name it "SimpleCalculator" under package "calculator"



3. Write down those two sample functions
package calculator;

public class SimpleCalculator
{

public static int add( int a , int b )
{
return a + b;
}

public static int subtract( int a , int b )
{
return a - b;
}

}
4. Now we're about to test

5. Get to your project properties, right click your project and choose properties.



6. Now you should have something like that

7. Choose "Java Build Path" tab



8. Click "Add library" and Choose "JUnit" from the library list



9. By default you'll find "JUnit 3.8.1" selected leave and proceed till finish.

10. You should have a class path like that. Click OK and be ready to write down your first test case.



11. Right click you project select new -> "JUnit Test Case"



12. Name it SimpleCalculatorTestCase
13. Now write down this code into the source file
package calculator.test;
import calculator.SimpleCalculator;
import junit.framework.TestCase;

public class SimpleCalculatorTestCase extends TestCase
{
public void testAdd()
{
int added = SimpleCalculator.add( 10 , 12 );
assertEquals( 12 , added );
}

public void testSubtract()
{
int subtract = SimpleCalculator.subtract( 12 , 10 );
assertEquals( 2 , subtract );
}

}

14. Now we have a test case for both add and subtract methods of our simple calculator.
15. Let's run it. Choose run menu -> Run as -> JUnit Test
16. You should have result like that.



17. When your run a JUnit test case, JUnit automatically parses your test class and find all methods starting with "test" clause, so they are the points to check. So we have two methods testAdd and testSubtract.

18. As you can see testAdd failed, testSubtract succeed. This is because if you check the assertEquals parameters you'll find that the expected parameter is not the really the expected one. But I intended to put it that false to get the idea. The function assertEquals took two parameters.
  • The expected value of an expression
  • The expression to be evaluated
If the two are equals so it means the the test is passed.

I think you should now take your hands changing that 12 to 22 and check the results. Waiting for you comments.

Download the full eclipse project here

References
http://en.wikipedia.org/wiki/Junit

Saturday, June 02, 2007

JPanel with gradient background

Thanks Sun for your great Swing framework. You've to know that Swing is the most powerful GUI framework I've seen in my life. Well architecture and extraordinary think for being very usable. The well MVC model, the ease of creating custom GUI components all of this should reflect the power of Swing. Check my blog I'll soon write down tips and lessons for having nice GUI using swing.

Let's start



First you've to know some about Swing painting mechanism. Each Swing component - JComponent - has a paint method which has a role of calling three main method to draw the component right

  1. paintComponent(Graphics g) this function is responsible for drawing the status of the component. Think of the JButton's paintComponent, it's responsible for drawing the JButton in three ways: normal, mouse over, clicked. The button is aware of it's status so it could paint itself right.
  2. paintBorder(Graphics g) this is the one responsible for drawing the border of your component, in case it have a border.
  3. paintChildren(Graphics g) this one is called to draw children components of the current component. For example, a panel which has two buttons. This function would call the paint of each component attached to this component.
For you to implement your custom drawing. You've to override "paintComponent", you've not to touch "paint", "paintBorder" nor "paintChildren".

Read this if you've difficulties understanding http://java.sun.com/products/jfc/tsc/articles/painting/

Secondly you've to know about Opacity. What is opacity? An opaque thing is difficult to see through - your room walls -. In Java each JComponent defines it's opacity. If it's opaque so you can't see through. The else is you can see through.

For example we've two panels. p1 and p2. p2 is a child of p1. p1's background is red, p2's background is green.

If the two panels are opaque you should see a green color; as p2 is opaque and you can't see p1 through it.

If p1 is opaque and p2 is not. You'll see a red color. whatever p2's background is you'll see a red color. As p2 is not opaque you can see through.

Read this if you didn't get it
http://mindprod.com/jgloss/opaque.html
http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#isOpaque()

Thirdly we're now to start coding.

Open your favorite IDE which is Eclipse for me.
And follow the next steps

1- Create a new Java project and name it "Gradient Panel"
2- Create a new package - to organize your work - and name it "gradient"
3- Create a new class name it "GradientPanel".
4- You should have something like :



5- Now open you editor and type in the following code

package gradient;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

/**
* GradientPanel is a class with a gradient background, put your non-opaque objects over it and enjoy.
* @author Mohammad Abou-Basha
*
*/
public class GradientPanel extends JPanel
{

private Color startColor;

private Color endColor;

public GradientPanel()
{
this( Color.GRAY , Color.WHITE );
}

public GradientPanel( Color startColor , Color endColor )
{
super();
this.startColor = startColor;
this.endColor = endColor;
}

@Override
protected void paintComponent( Graphics g )
{
super.paintComponent( g );
int panelHeight = getHeight();
int panelWidth = getWidth();
GradientPaint gradientPaint = new GradientPaint( 0 , 0 , startColor , panelWidth , panelHeight , endColor );
if( g instanceof Graphics2D )
{
Graphics2D graphics2D = (Graphics2D)g;
graphics2D.setPaint( gradientPaint );
graphics2D.fillRect( 0 , 0 , panelWidth , panelHeight );
}
}
}


Now we've a gradient panel. We have to create a demo and try playing.

6- Create a new Frame, name it "GradientPanelDemo". For the ease you can use the visual editor.
7- Drag a new bean and select the GradientPanel. The code should be something like that

package gradient;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class GradientPanelDemo extends JFrame
{

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private GradientPanel gradientPanel = null;

/**
* This is the default constructor
*/
public GradientPanelDemo()
{
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize( 300 , 200 );
this.setContentPane( getJContentPane() );
this.setTitle( "JFrame" );
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if( jContentPane == null )
{
jContentPane = new JPanel();
jContentPane.setLayout( new BorderLayout() );
jContentPane.add(getGradientPanel(), BorderLayout.CENTER);
}
return jContentPane;
}

/**
* This method initializes gradientPanel
*
* @return gradient.GradientPanel
*/
private GradientPanel getGradientPanel()
{
if( gradientPanel == null )
{
gradientPanel = new GradientPanel();
}
return gradientPanel;
}

}

8- You should see that in your visual editor



9-Now let add some buttons and text stuff and check the result


Here's the component hierarchy


Preview 2

Ooh, why is the last panel "jPanel" gray!!
Don't you remember? I told you about opacity. Simply make this panel not opaque and check the result
jPanel.setOpaque( false );


Preview after setting the panel opacity to false

Use this constructor to define your own colors
gradientPanel = new GradientPanel( Color.RED , Color.YELLOW );




This was the first tip having great Swing GUI.
Click here to get the full project.

Comments are welcomed, so feel free.



References
http://java.sun.com/products/jfc/tsc/articles/painting/
http://java.sun.com/javase/6/docs/api/java/awt/GradientPaint.html

Friday, June 01, 2007

Share you calendar events from your mobile

Google has just announced the new web interface for Google Calendar, now enjoy the synchronization between your mobile phone and PC. Point to calendar.google.com and start.

http://www.google.com/mobile/calendar/index.html