Thursday, April 12, 2007

WPF: Media Player

MediaPlayer player;
Uri uri = new Uri("pack://siteoforigin:,,,/music.mp3");
player = new MediaPlayer();
player.Open(uri);
player.Play();

Wednesday, April 11, 2007

WPF: setting cursor

eg:
void Page1_MouseMove(object sender, MouseEventArgs e)
{
e.MouseDevice.SetCursor(Cursors.Hand); //-- hand cursor
e.MouseDevice.SetCursor(Cursors.None); //-- none, invisible
}

WPF: Image Drawing

XAML:
<Image Name="image1" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" />

C#:
DrawingGroup group = new DrawingGroup();
DrawingImage di = new DrawingImage(group);
//-- create clipping bound for drawing group
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = new Rect(0,0,250,250);
group.ClipGeometry = myRectangleGeometry;
image1.Source = di;

//-- load image source (refer to prev post)
ImageSource img2 = ...

//-- to draw something
using (DrawingContext dc = group.Open())
{
dc.DrawImage(img2, new Rect(x, y, w, h));
//-- and other stuff
}

Friday, March 30, 2007

C#: using the prev LinkList

LinkList<Dog> list = new LinkList<Dog>();
list.addLast(new Dog("Rocky", 16));
list.addLast(new Dog("Waggie", 6));
list.remove(0);
list.get(0).bark();

C#: reading text files

using System;
using System.IO;

namespace FileIOText
{
class ReadTextFile
{
public ReadTextFile()
{
try
{
//-- "using" also closes the file
using (StreamReader sr = new StreamReader("data.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch(Exception e){
Console.WriteLine("File could not be read");
Console.WriteLine(e.Message);
}
}
}
}

C#: reading binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace FileIOText
{
class ReadBinaryFile
{
const String filename = "binfile.dat";
public ReadBinaryFile()
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
/*
//create reader
BinaryReader br = new BinaryReader(fs);

Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadDouble());
Console.WriteLine(br.ReadBoolean());
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadChar());

br.Close();
*/

BinaryFormatter formatter = new BinaryFormatter();
Dog d = (Dog)formatter.Deserialize(fs);
Console.WriteLine(d.Name);
Console.WriteLine(d.Age);
fs.Close();
}
}
}

C#: writing to text file

using System;
using System.IO;

namespace FileIOText
{
class WriteTextFile
{
public WriteTextFile()
{
try
{
//-- "using" also closes the file
using (StreamWriter sw = new StreamWriter("data.txt"))
{
sw.Write("Hello...");
sw.WriteLine("This is the start of the writing...");
sw.WriteLine("------------------");
//-- write arbitrary objects
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
catch (Exception e)
{
Console.WriteLine("File could not be written");
Console.WriteLine(e.Message);
}
}
}
}

C#: Writing binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace FileIOText
{
class WriteBinaryFile
{
const String filename = "binfile.dat";
public WriteBinaryFile()
{
if(File.Exists(filename)){
Console.WriteLine("{0} already exists!", filename);
return;
}
FileStream fs = new FileStream(filename, FileMode.CreateNew);
/*
//create writer
BinaryWriter bw = new BinaryWriter(fs);

//create test data to write to file
int i = 299;
double d = 2.5999;
bool b = false;
String s = "Haha";
char c = 'A';

bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
bw.Write(c);
*/

Dog d = new Dog("Rocky", 16);
Dog e = new Dog("Waggie", 10);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, d);
formatter.Serialize(fs, e);
//bw.Close();
fs.Close();
}
}
}

C#: LinkList

using System;
using System.Collections.Generic;
using System.Text;

namespace List
{
[Serializable]
class LinkList<T>
{
int numOfElements = 0;
ListNode<T> headNode = null;
public LinkList() { }
public LinkList(T data) {
headNode = new ListNode<T>(data);
}
public bool isEmpty()
{
return (numOfElements == 0);
}
public int getNumOfElements()
{
return numOfElements;
}
public ListNode<T> find(int index)
{
ListNode<T> node = headNode;
if(index < 0 || index >= numOfElements){
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
for (int i = 0; i < index; i++ )
{
node = node.Next;
}
return node;
}
public T get(int index)
{
/*
if (index >= 0 && index < numOfElements)
{
return find(index).Data;
}*/
return find(index).Data;
}
public void add(int index, T data)
{
if (index == 0)
{
ListNode<T> newNode = new ListNode<T>(data, headNode);
headNode = newNode;
}
else if (index > 0 && index <= numOfElements)
{
ListNode<T> prevNode = find(index - 1);
ListNode<T> newNode = new ListNode<T>(data, prevNode.Next);
prevNode.Next = newNode;
}else{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements++;
}
public void addLast(T data)
{
add(getNumOfElements(), data);
}
public void remove(int index)
{
if (index == 0)
{
headNode = headNode.Next;
}
else if (index > 0 && index < numOfElements)
{
ListNode<T> prev = find(index - 1);
ListNode<T> current = prev.Next;
prev.Next = current.Next;
}
else
{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements--;
}
public void removeLast()
{
remove(numOfElements - 1);
}
}
}

C#: ListNode

using System;
using System.Collections.Generic;
using System.Text;

namespace List
{
[Serializable]
class ListNode<T>
{
ListNode<T> next = null; //-- self referencing node
T data; //-- data segment
public ListNode(T t)
{
data = t;
}
public ListNode(T t, ListNode<T> n):this(t) //-- call the prev constructor 1st
{
//-- then proceed to following line
next = n;
}
public T Data{
get { return data; }
set { data = value; }
}
public ListNode<T> Next
{
get { return next; }
set { next = value; }
}
}
}

Thursday, March 29, 2007

NTU Assignment Demos

Inverse Kinematics using Flash
Fluid Simulation using Flash




http://psalmhundred.net/NTU/

My favourite: the fluid simulation one. : )

Saturday, February 10, 2007

WPF: loading images, audio, video

hmmm. strange that loading images more ma2 fan2 than loading video/audio
must set width/height. tried without and no image : (

loading images:
Image LoadImage(String imageFileName)
{
// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageFileName);

// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather then just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.EndInit();
Image i = new Image();
i.Source = myBitmapImage;
i.Width = myBitmapImage.Width;
i.Height = myBitmapImage.Height;
return i;
}

loading audio/video:
xaml:
<MediaElement Grid.Row="2" Name="myMedia" LoadedBehavior="Manual"/>

cs:
myMedia.Source = new Uri("mymediafile.wmv", UriKind.Relative);
myMedia.Play();

Friday, February 09, 2007

What am i busy with?

Hmm...

switch(task){
case 1:
prepareFor1stBorn(); //-- baby coming
break;
case 2:
doASsignments(); //-- NTU modules
break;
case 3:
doResearch(); //-- NTU dissertation
break;
case 4:
mark(); //-- mark exam papers
break;
}

How to teach programming???

am waiting for wifey at her office. still cracking my brain how to teach oopm more effectively... :(

//-- status now
boolean isFrustrated = true;
while(isFrustrated){
thinkSomeMoreIdeas();
}

Thursday, February 01, 2007

AS3: SoundMixer.computeSpectrum()

public static function computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void

Takes a snapshot of the current sound wave and places it into the specified ByteArray object. The values are formatted as normalized floating-point values, in the range -1.0 to 1.0. The ByteArray object passed to the outputArray parameter is overwritten with the new values. The size of the ByteArray object created is fixed to 512 floating-point values, where the first 256 values represent the left channel, and the second 256 values represent the right channel.

Flash: BitmapData.draw

Draws a source image or movie clip onto a destination image, using the Flash Player vector renderer

draw(source:Object, [matrix:Matrix], [colorTransform:ColorTransform], [blendMode:Object], [clipRect:Rectangle], [smooth:Boolean])