Monday 6 November 2006

Digital life and Evolution

A 4th year assignment I had where I had to make a 2D application

The title I was given Digital life and Evolution


Notes from my report:
  • Environment's design considerations
In this project, I had to consider the new design issues of working in 2D and keeping track of the items that would be drawn to the screen, also, where on the screen they should be placed and in relation to other items.
  • Methodology
The methodology for this work was primary one of experimentation. Over the weeks of this project it slowly evolved into its current stage.
  • Implementation
To implement this project, I first drew up a layout plan for how it should look and how the user would interact with the programme i.e. where hills would be drawn, how the courser would move, types of random patterns …etc.
When creating the growing plants, I used random number to influence the size and direction of them but I also needed to found where the next part of the plant should grow from. I got this by finding the midpoint of a line on the side it would grow from.
The stars that can be dropped in the sky are created by placing a dot where the courser is and randomly setting the brightness and speed of this rotation around the top centre of the screen. The change in brightness helps to create a feeling of depth.

The implementation for this work contained some key features:

I used separate files for each of the classes in other to make code navigation easier.
I created management class to better control the multiple instances of the class.
Array-list was added to allow for dynamic growth of the group of objects.
The key words “Height” and “Width” were used throughout the code to allow the graphics to adapt to changes in the setup size.
I added a screen help view (that can be selected by pressing the “h” button on the keyboard) to make navigate the environment more easily.

I also replaced the courser with a small floating rectangle which glows in a cycle and change size based on the speed of the mouse movement. This adds to help it amerce the user into the environment.
  • Screen shots
A view of the programme, when it first starts.
When the help screen is displayed.
The view after several star and plants, have been dropped.


... continue reading!

Monday 31 July 2006

Animated Java JLabel

today I'm going to share with you a little class I made that extends the JAVA Swing jlabel element.
With this class you'll be able to fade text in/out and cycle text to be displayed.

To better help you understand. I've attached two examples.

setTxt( "code me" ) & setInterFadeTxt( "a sandwich" )
This first 1 shows the fading between two text strings


setTxtAniam(...) Here I am using the items in the array
["s","sa","san","sand","sandw","sandwi","sandwic","sandwich"]


Ok on to the good stuff!

here I'm just extending JLabel and defining some variables that we need later
import javax.swing.JLabel;
import java.awt.Color;

public class AnimeJLabel extends JLabel
{
 private static final long serialVersionUID = 1L;
 private Color startColour;

 private Thread t;
 private boolean firstTime;
 private long waitTime;
 
 private boolean stopTxtAniamBool;
 
 private int StartR,StartG,StartB;
 private int MidR,MidG,MidB;
 private int EndR,EndG,EndB;

In the constructor you can
  1. set the colour of your text
  2. the background color of the label and
  3. *optional: the number of seconds the fade should take to complete.

 public AnimeJLabel(Color startColour,Color endColour)
 {
  this(startColour,endColour,5);
 }

 public AnimeJLabel(Color startColour,Color endColour,int sec)
 {
  this.setForeground(startColour);
  this.startColour = startColour;
  
  StartR = startColour.getRed();
  StartG = startColour.getGreen();
  StartB = startColour.getBlue();
  
  EndR = endColour.getRed();
  EndG = endColour.getGreen();
  EndB = endColour.getBlue();

  firstTime = true;
  waitTime = sec * 1000;
 }

So after you create the label you can set text at anytime using this method.
you should note that this will stop the fade/animation. Then fade in the new text
 public void setTxt(String txt)
 { 
  MidR = StartR;
  MidG = StartG;
  MidB = StartB;
  
  stopT();

  this.setForeground(startColour);
  this.setText(" "+txt);
  if( ! txt.equals(""))//if theres No text then there no need to blend
  {
   try
   {
          t = new Thread(new Runnable()
               {
                   public void run()
                   {
                 try
                 {
                  Thread.sleep(waitTime);
                  
                  while(true == start2end_colour()){
                   Thread.sleep(10);
                  }
                  
                 }
                 catch(Exception ex){
                  //System.out.println("Error:Sleep");
                  //System.out.println(ex);
                 }
                   }
               });
               t.start();
   }
   catch(Exception ex){
    System.out.println("Error:Nova_JLabel");
   }
  }
 }

with the stopTxtAniam method you can stop the Label at any time.
An example; is if you are using the label to display a waiting message and then your done so you need to get rid of it.
 public void stopTxtAniam(){
  stopTxtAniamBool = true;
 }

Here are some utility methods that are frequency used throughout class
  
 private void stopT()
 {
  if(false == firstTime){
   t.interrupt();
  }
  else{
   firstTime = false;
  }
 }
 
 private void setPlaneTxt(String txt)
 {
  this.setText(" "+txt);
 }

Unlike "setText" this will allow the current text to fade out.. and then bring the new text in
 public void setInterFadeTxt(final String txt)
 {
  stopT();
  
  try
  {
         t = new Thread(new Runnable()
              {
                  public void run(){
                try
                {
                 while(true == start2end_colour()){
                  Thread.sleep(2);
                 }
                 setPlaneTxt(txt);
                 
                 while(true == end2start_colour()){
                  Thread.sleep(2);
                 }
                }
                catch(Exception ex){          
       }
                  }
              });
              t.start();
  }
  catch(Exception ex)
  {
   System.out.println("Error:CountDown");
  }
 }

this is a very handy method that allows the label to act like a countdown timer.
this method is pretty much self explanatory. Just to clarify the "displayMessage" parameter will prefix timer
 
 public void CountDown(int Sec){
  CountDown(Sec,"","Finished");
 }
 
 public void CountDown(final int Sec,final String displayMessage,final String endMessage)
 { 
  stopT();
  
  try
  {
         t = new Thread(new Runnable()
              {
                  public void run()
                  {
                try
                {
                 for(int i = Sec-1; 0<i; i--)
                 {
                  for(int ii = 100; 0<ii; ii-=2)//go up in 2
                  {
                  setPlaneTxt(displayMessage +" "+i+"."+ii);
                  Thread.sleep(20);
                  }
                 }
                 setPlaneTxt(endMessage);
                 
                }
                catch(Exception ex){
                }
                  }
              });
              t.start();
  }
  catch(Exception ex){
   System.out.println("Error:CountDown");
  }
 }

and here's where we get our lovely typewriter effect
you should note that the time is in milliseconds and represents the time between each array element.
 public void setTxtAniam(final String [] txtArray, final long timeInMill)
 {
  stopTxtAniamBool = false;
  stopT();
  
  try
  {
         t = new Thread(new Runnable()
              {
                  public void run()
                  {
                try
                {
                 while(false == stopTxtAniamBool)
                 {
                  for(int i = 0; i<txtArray.length; i++)
                  {
                   if(false == stopTxtAniamBool)//will help to stop the aniam faster
                   {
                    setPlaneTxt(txtArray[i]);
                    Thread.sleep(timeInMill);
                   }
                  }
                 }
                 while(true == start2end_colour()){
                  Thread.sleep(10);
                 }
                 
                }
                catch(Exception ex){
                }
                  }
              });
              t.start();
  }
  catch(Exception ex){
   System.out.println("Error:setTxtAniam");
  }
 }

here are just a methods that handles the colour transform
 private boolean start2end_colour()
 {
  
  int intR = 0;
  int intG = 0;
  int intB = 0;
  
  this.setForeground(new Color(MidR,MidG,MidB));

  if(MidR == EndR)
  {  intR = 1;  }
  else if(EndR>MidR)
  {  MidR++;  }
  else// if(EndR<MidR)
  {  MidR--;  }
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  if(MidG == EndG)
  {  intG = 1;  }
  else if(EndG>MidG)
  {  MidG++;  }
  else// if(EndG<MidG)
  {  MidG--;  }
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  if(MidB == EndB)
  {  intB = 1;  }
  else if(EndB>MidB)
  {  MidB++;  }
  else// if(EndB<MidB)
  {  MidB--;  }
  
  if(1 == intR && 1 == intB && 1 == intG)
  {  return false;  }
  else
  {  return true;  }
 }

 private boolean end2start_colour()
  { 
   int intR = 0;
   int intG = 0;
   int intB = 0;
  
   this.setForeground(new Color(MidR,MidG,MidB));

   if(MidR == StartR)
   { intR = 1;  }
   else if(StartR>MidR)
   { MidR++;  }
   else// if(EndR>MidR)
   { MidR--;  }
   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   if(MidG == StartG)
   { intG = 1;  }
   else if(StartG>MidG)
   { MidG++;  }
   else// if(EndG>MidG)
   { MidG--;  }
   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   if(MidB == StartB)
   { intB = 1;  }
   else if(StartB>MidB)
   { MidB++;  }
   else// if(EndB>MidB)
   { MidB--;  }
  
   if(1 == intR && 1 == intB && 1 == intG)
   { return false;  }
   else
   { return true; }
  }
}

If you end up using this in any projects/work. I would love to hear. ^_^
... continue reading!

Friday 14 July 2006

FinShare - 3rd year project


I have upload a copy of my 3rd year CS project.

FinShare is a peer-2-peer file sharing desktop application for local area networks written in Java
(Try saying that 10 times fast)

So what makes this special:
  1. Auto discover other users
  2. Search for files by name across all users on the network
  3. Browse remote users shared files in a file tree
  4. Chat + encrypted with character substitution
  5. Format chat with tags
  6. Log file for errors
  7. Fluid layout
  8. Compiled HTML Help
  9. Transparent splash screen




... continue reading!