Wednesday, November 26, 2008
Sunday, November 16, 2008
Flash AS3 Custom Component - parameters
If the parameters are modified during authoring time,
they are not ready til after the constructor is called.
package{
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class Testing extends MovieClip{
[Inspectable]
public var id:Number = 0;
function Testing(){
this.addEventListener(MouseEvent.CLICK, mouseclick);
}
function mouseclick(e:MouseEvent):void{
trace("id" + id);
}
}
}
they are not ready til after the constructor is called.
package{
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class Testing extends MovieClip{
[Inspectable]
public var id:Number = 0;
function Testing(){
this.addEventListener(MouseEvent.CLICK, mouseclick);
}
function mouseclick(e:MouseEvent):void{
trace("id" + id);
}
}
}
Tuesday, August 12, 2008
Elastic Flash Demo
Key in a number from 2-20 in the textbox and click the button to generate an elastic object. Use the mouse to drag the dots.
Tuesday, July 08, 2008
Microsoft.Win32.OpenFileDialog affect FileIO operation
Just found out that if I use the following code to retrieve a filename, the FileStream will be affected as the current directory will be set to where the filename was taken from, eg.: "My Documents", instead of the Application Path.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All files (*.*)|*.*";
if ((bool)dialog.ShowDialog())
{
// store filename
}
// and later in the code
FileStream stream = new FileStream(
"mydata.bin", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
// write some data
stream.Close();
// "mydata.bin" will be saved in the current directory of OpenFileDialog
One solution will be to use the application path and extract the directory:
String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
another will be to use RestoreDirectory property of the OpenFileDialog. set it to true.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All files (*.*)|*.*";
if ((bool)dialog.ShowDialog())
{
// store filename
}
// and later in the code
FileStream stream = new FileStream(
"mydata.bin", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
// write some data
stream.Close();
// "mydata.bin" will be saved in the current directory of OpenFileDialog
One solution will be to use the application path and extract the directory:
String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
another will be to use RestoreDirectory property of the OpenFileDialog. set it to true.
Thursday, June 19, 2008
Flash 10 3D Demo - partial cube
NOT working. Flash 10 player has been updated to official release.

Requires Flash 10 (currently beta)
http://labs.adobe.com/downloads/flashplayer10.html
Demo of rotating cube(partial to see culling effect):
http://psalmhundred.net/experiment/flash3d/Flash10App.html

Requires Flash 10 (currently beta)
http://labs.adobe.com/downloads/flashplayer10.html
Demo of rotating cube(partial to see culling effect):
http://psalmhundred.net/experiment/flash3d/Flash10App.html
Flash 10 3D API - Utils3D.projectVectors(...)
Utils3D.projectVectors(m:Matrix3D, verts:Vector, projectedVerts:Vector, uvts:Vector) causes Flash player 10 beta to crash currently.
so just a workaround, a custom method to do similar thing:
public function projectVectors(projMatrix:Matrix3D, vert:Vector.<Number>,
v2D:Vector.<Number>, uvt:Vector.<Number>):void{
var temp:Vector.<Number> = new Vector.<Number>(vert.length);
projMatrix.transformVectors(vert, temp);
var z:Number = 0;
for(var i:int = 0; i<temp.length/3; i++){
z = temp[i*3+2];
z = focalLength / (focalLength+z);
// x' = f*x/(f+z), y' = f*y/(f+z)
v2D[i*2] = temp[i*3]*z;
v2D[i*2+1] = temp[i*3+1]*z;
// t = f/(f+z)
uvt[i*3+2] = z;
}
}
so just a workaround, a custom method to do similar thing:
public function projectVectors(projMatrix:Matrix3D, vert:Vector.<Number>,
v2D:Vector.<Number>, uvt:Vector.<Number>):void{
var temp:Vector.<Number> = new Vector.<Number>(vert.length);
projMatrix.transformVectors(vert, temp);
var z:Number = 0;
for(var i:int = 0; i<temp.length/3; i++){
z = temp[i*3+2];
z = focalLength / (focalLength+z);
// x' = f*x/(f+z), y' = f*y/(f+z)
v2D[i*2] = temp[i*3]*z;
v2D[i*2+1] = temp[i*3+1]*z;
// t = f/(f+z)
uvt[i*3+2] = z;
}
}
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();
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
}
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
}
<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();
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);
}
}
}
}
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();
}
}
}
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);
}
}
}
}
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();
}
}
}
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);
}
}
}
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; }
}
}
}
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. : )
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();
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:
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;
}
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();
}
//-- status now
boolean isFrustrated = true;
while(isFrustrated){
thinkSomeMoreIdeas();
}
Subscribe to:
Posts (Atom)