Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming.

Similar presentations


Presentation on theme: "Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming."— Presentation transcript:

1 Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming

2 Oct 2007 SDP-MSc Slide 2

3 3 using System; using System.Windows.Forms; using System.Drawing; class GFrame1:Form { protected int x=10, y=20; protected Label l1; protected Font f=new Font("Times New Roman",20,FontStyle.Bold); :

4 Oct 2007 SDP-MSc Slide 4 : public GFrame1(){ l1=new Label(); l1.Font=f; l1.Text="Welcome"; l1.SetBounds(x,y,160,43); Controls.Add(l1); }} public class Test91{ public static void Main(string[] args){ Application.Run(new GFrame1());} }

5 Oct 2007 SDP-MSc Slide 5

6 class GFrame2 :GFrame1 { protected Label l2=new Label(); public GFrame2():base(){ l2.Text="Home"; l2.SetBounds(x,y+40,160,43); l2.Font = f; Controls.Add(l2); } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame2());} }

7 Oct 2007 SDP-MSc Slide 7

8 class GFrame2 :GFrame1 { protected Button b1=new Button(); public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); Controls.Add(b1); } public class Test93{ public static void Main(string[] args){ Application.Run(new GFrame2());} }

9 Oct 2007 SDP-MSc Slide 9

10 10 class GFrame2 :GFrame1 { protected Button b1=new Button(); public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1);} private void button1_Click(object sender, EventArgs e){ l1.Text="Goodbye";} } public class Test94{ public static void Main(string[] args){ Application.Run(new GFrame2());} }

11 Oct 2007 SDP-MSc Slide 11 Using Paint Feature

12 Oct 2007 SDP-MSc Slide 12 class GFrame2 :GFrame1 { protected Button b1=new Button(); protected bool start = false; public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1); } private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); }

13 Oct 2007 SDP-MSc Slide 13 protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true) g.DrawString("Everyone",f,brush,30,60); } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame2());} }

14 Oct 2007 SDP-MSc Slide 14

15 Oct 2007 SDP-MSc Slide 15 First a new Version of the Base class

16 using System; using System.Windows.Forms; using System.Drawing; using System.Threading; class GFrame1:Form { protected int x=10, y=20; protected Font f= new Font("Times New Roman",20,FontStyle.Bold); public GFrame1(){ } protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); g.DrawString("Welcome",f,brush,x,y);} } :

17 class GFrame2 :GFrame1 { protected Button b1=new Button(); protected bool start = false; public GFrame2():base(){ b1.Text= "Move"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1); } private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); } :

18 protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){ x+=15; Thread.Sleep(1000); this.Refresh();} } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame2());} }

19 Oct 2007 SDP-MSc Slide 19 I.e restarted from left of screen

20 Oct 2007 SDP-MSc Slide 20 class GFrame2 :GFrame1 { : protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){ x+=15; Thread.Sleep(1000); if (x>280)x=10; // reset this.Refresh();} }

21 Oct 2007 SDP-MSc Slide 21 I.e bounces back

22 class GFrame2 :GFrame1 { : private int step=15; : protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; SolidBrush brush=new SolidBrush(Color.Black); if (start==true){ x+=step; Thread.Sleep(1000); if ((x>180)||x<10)step=step*-1; this.Refresh();} }

23 Oct 2007 SDP-MSc Slide 23 Ex91. Rewrite ‘Test91’ so text moves vertically up & down screen. Note: Repositioning of Button

24 Oct 2007 SDP-MSc Slide 24

25 Oct 2007 SDP-MSc Slide 25 using System; using System.Windows.Forms; using System.Drawing; using System.Threading; class GFrame3:Form { protected int x=10, y=10; protected SolidBrush brush; public GFrame3(){ brush =new SolidBrush(Color.Blue); } :

26 Oct 2007 SDP-MSc Slide 26 protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); Graphics g = pe.Graphics; g.FillEllipse(brush,x,y,50,50); } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame3());} }

27 Oct 2007 SDP-MSc Slide 27

28 Oct 2007 SDP-MSc Slide 28 class GFrame4 :GFrame3 { protected Button b1=new Button(); public GFrame4():base(){ b1.Text= "Change Colour"; b1.SetBounds(100,180,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1); } private void button1_Click(object sender, EventArgs e){ brush.Color=Color.Yellow; this.Refresh();} } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame4());}} }

29 Oct 2007 SDP-MSc Slide 29

30 Oct 2007 SDP-MSc Slide 30 class GFrame4 :GFrame3 { protected Button b1=new Button(); private bool start = false; private int step=8; public GFrame4():base(){ b1.Text= "Change Colour"; b1.SetBounds(180,100,90,23); b1.Click += new EventHandler(this.button1_Click); Controls.Add(b1); } private void button1_Click(object sender, EventArgs e){ start=true; this.Refresh(); } :

31 Oct 2007 SDP-MSc Slide 31 : protected override void OnPaint(PaintEventArgs pe){ base.OnPaint(pe); if (start==true){ y+=step; Thread.Sleep(500); if ((y>219)||y<10)step=step*-1; this.Refresh();} } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame4());} }

32 Oct 2007 SDP-MSc Slide 32 Ex92. Rewrite ‘Test99’ so the ball moves around the Frame

33 Oct 2007 SDP-MSc Slide 33 Ex93. Rewrite ‘Test99’ so the ball moves vertically up & down the Frame but not go as high next time & eventually stops

34 Oct 2007 SDP-MSc Slide 34 Ex94. Rewrite ‘Test99’ so the ball initially diagonally & bounces off each wall ….

35 Oct 2007 SDP-MSc Slide 35 Ex95. Rewrite ‘Ex94’ so the ball initially diagonally & bounces off each wall accelerating as it reflects off wall

36 Oct 2007 SDP-MSc Slide 36 class GFrame3 extends JFrame { protected Container content; protected int x=10, y=30; protected Color c=Color.blue; public GFrame3(){content=getContentPane(); content.setLayout(null); setSize(300,300); setVisible(true);} public void paint(Graphics g){ super.paint(g); g.drawLine(x, y+40, x+30,y);} } public class Test910{ public static void main(String[] args){ GFrame3 e = new GFrame3();}} Ex96. This Application produces the following result

37 Oct 2007 SDP-MSc Slide 37 Now complete the application (using inheritence) so it draws a triangle which moves up & down screen after button clicked

38 Oct 2007 SDP-MSc Slide 38

39 Oct 2007 SDP-MSc Slide 39 Ex97. Now rewrite the application so the triangle moves around screen

40 Oct 2007 SDP-MSc Slide 40 GUI Front End

41 Oct 2007 SDP-MSc Slide 41 using System; using System.Windows.Forms; using System.Drawing; using System.Threading; class Counter{ protected int value; private String name; public Counter(int v, String n){ this.value = v; this.name = n;} public void increment(){this.value++;} public int read_value(){ return this.value;}}

42 Oct 2007 SDP-MSc Slide 42 class MyCounter:Counter{ public MyCounter(int v, String n):base(v,n) { } public void decrement() {this.value--;} public void add(int amt){ this.value+=amt;} }

43 Oct 2007 SDP-MSc Slide 43 class GFrame:Form { private Label lmain=new Label(); private Label l1=new Label(); private Button b_incr=new Button(); private Button b_decr=new Button(); private Button b_add=new Button(); private TextBox t1=new TextBox(); private TextBox t2=new TextBox(); private Font f=new Font("Times New Roman",15,FontStyle.Bold); private MyCounter c =new MyCounter(1,"Score");

44 Oct 2007 SDP-MSc Slide 44 : public GFrame():base(){ lmain.Text="Counter Application"; lmain.Font=f; l1.Text="Value"; l1.Font=f; b_incr.Text= "Incr"; b_decr.Text= "Decr"; b_add.Text= "Add Amt"; t1.Text= "1"; lmain.SetBounds(10,10,220,43); l1.SetBounds(22,60,70,23); t1.SetBounds(102,60,90,23); t1.Text= " "+c.read_value(); b_incr.SetBounds(12,90,90,23); b_decr.SetBounds(102,90,90,23); b_add.SetBounds(12,120,90,23); t2.SetBounds(102,120,90,23); :

45 Oct 2007 SDP-MSc Slide 45 : b_incr.Click += new EventHandler(this.button1_Click); b_add.Click += new EventHandler(this.button3_Click); Controls.Add(lmain); Controls.Add(l1); Controls.Add(t1); Controls.Add(b_incr); Controls.Add(b_decr); Controls.Add(b_add); Controls.Add(t2); }

46 Oct 2007 SDP-MSc Slide 46 private void button1_Click(object sender, EventArgs e){ c.increment(); t1.Text=" "+c.read_value();} private void button3_Click(object sender, EventArgs e){ int amt=int.Parse(t2.Text); c.add(amt); t1.Text=" "+c.read_value();} }

47 Oct 2007 SDP-MSc Slide 47 public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame());} }

48 Oct 2007 SDP-MSc Slide 48 Ex98. Put a GUI Front End on the following Account class class Account{ private int accNo; private int balance; public Account(int No, int bal){ this.balance=bal; this.accNo=No;} public int getAccNo(){return accNo;} public int getBalance() {return balance;} public void credit(int amount){ balance+=amount;} public boolean debit(int amount){ if (amount > balance) return false; else { balance = balance - amount; return true;}} }

49 Oct 2007 SDP-MSc Slide 49

50 Oct 2007 SDP-MSc Slide 50 Ex99. Put this GUI front -end with the following Time class

51 Oct 2007 SDP-MSc Slide 51 class Time{ private int sec; private int min; private int hour; public Time(int h,int m, int s){ this.hour = h; this.min = m; this.sec = s;} public void increment_sec(){this.sec++; if (sec==60){ sec=0; min++;} if (min==60){min=0; hour++;} } public void decrement_sec(){if (sec>0) sec--; else { sec=59; if(min>0) min--; else {min=59; hour--;}}} public String read_time(){ return ""+hour+":"+min+":"+sec;}

52 Oct 2007 SDP-MSc Slide 52 Ex910. Change Q72 from DOS menu to GUI class Item{ private int lotno; private String bidder; private int bid; public Item(int l,String bn, int b){... public void nextbid(String b_name, int amt){... public void print_details(){ } public class Q72{ public static void main(... Note: next bid only accepted if > current bid Online Auction

53 Oct 2007 SDP-MSc Slide 53 Generics

54 Oct 2007 SDP-MSc Slide 54 class ArrayTest { private int[] values; public ArrayTest(int[] v) { values = v;} public int first() { if (values.Length == 0) throw new Exception(); return values[0];} public void printAll() { Console.WriteLine(); Console.Write("["); foreach (int el in values) Console.Write(" " + el); Console.WriteLine("]"); }} Version 1

55 Oct 2007 SDP-MSc Slide 55 class GenericsEx { public static void Main() { int[] a = { 2, 6, 3, 5 }; ArrayTest at=new ArrayTest(a); int res = at.first(); Console.WriteLine(“First=" + res); at.printAll(); }

56 Oct 2007 SDP-MSc Slide 56 class ArrayTest { private double[] values; public ArrayTest(double[] v) { values = v;} public double first() { if (values.Length == 0) throw new Exception(); return values[0];} public void printAll() { Console.WriteLine(); Console.Write("["); foreach (double el in values) Console.Write(" " + el); Console.WriteLine("]"); }} Version 2

57 Oct 2007 SDP-MSc Slide 57 class GenericsEx { public static void Main() { double[] a = { 2.25, 6.5, 3.25, 5.5 }; ArrayTest at=new ArrayTest(a); double res = at.first(); Console.WriteLine("Sum=" + res); at.printAll(); }

58 Oct 2007 SDP-MSc Slide 58 class ArrayTest { private T [] values; public ArrayTest(T[] v) { values = v;} public T first() { if (values.Length == 0) throw new Exception(); return values[0];} public void printAll() { Console.WriteLine(); Console.Write("["); foreach (T el in values) Console.Write(" " + el); Console.WriteLine("]"); }} Version 3

59 Oct 2007 SDP-MSc Slide 59 class GenericsEx { public static void Main() { double[] a = { 2.25, 6.5, 3.25, 5.5 }; ArrayTest at=new ArrayTest (a); double res = at.first(); Console.WriteLine("First=" + res); at.printAll(); }

60 Oct 2007 SDP-MSc Slide 60 interface First { int Add(); } class Second where T : First { T val1,val2; public Second(T v1, T v2) { val1 = v1; val2 = v2; } public int sum() { return val1.Add()+val2.Add(); }}

61 Oct 2007 SDP-MSc Slide 61 class MyT : First { private int v1,v2; public MyT(int a, int b){v1=a; v2=b;} public int Add() { return v1 + v2; } public class Test {public static void Main() { MyT mt1 = new MyT(1, 2); MyT mt2 = new MyT(3, 4); Second sec = new Second (mt1, mt2); Console.WriteLine("Sum=" + sec.sum()); }}

62 Oct 2007 SDP-MSc Slide 62 //Error public class Test {public static void Main() { MyT mt1 = new MyT(1, 2); MyT mt2 = new MyT(3, 4); Second sec = new Second (mt1, mt2); Console.WriteLine("Sum=" + sec.sum()); }}

63 Oct 2007 SDP-MSc Slide 63 class ArrayTest where T:IComparable { private T[] values; public ArrayTest(T[] v) { values = v;} public T largest() { if (values.Length == 0) throw new Exception(); T res = values[0]; foreach (T el in values) if (res.CompareTo(el)<0) res=el; return res; } :

64 Oct 2007 SDP-MSc Slide 64 class GenericsEx { public static void Main() { double[] a = { 2.25, 6.5, 3.25, 5.5 }; ArrayTest at = new ArrayTest (a); double res = at.largest(); Console.WriteLine("Largest=" + res); at.printAll(); }

65 Oct 2007 SDP-MSc Slide 65 IEnumerator Interface has following members: Property: Current Method: MoveNext Method: Reset "Supports a simple iteration over a collection.", as well as: :

66 Oct 2007 SDP-MSc Slide 66 IEnumerable Interface 1 method called GetEnumerator. "Exposes the enumerator support the ForEach semantics

67 Oct 2007 SDP-MSc Slide 67 foreach loop Iterates through a composition i.e IEnumerator type public class Array3{ static int add(int []b) { int total=0; foreach (int element in b) total = total+ element; return total;} : b-Must be an IEnumerable type Implied call to b.GetEnumator()

68 Oct 2007 SDP-MSc Slide 68 Yield Only used in an Iterator Block // yield-example.cs using System; using System.Collections; public class List { ;

69 Oct 2007 SDP-MSc Slide 69 public static IEnumerable Power(int number, int exponent) { int counter = 0; int result = 1; while (counter++ < exponent) { result = result * number; yield return result; } }

70 Oct 2007 SDP-MSc Slide 70 static void Main() { // Display powers of 2 up to the exponent 8: foreach (int i in Power(2, 8)) { Console.Write("{0} ", i); } } } 2 4 8 16 32 64 128 256

71 Oct 2007 SDP-MSc Slide 71 IEnumerator Interface If you want to Iterate through a collection in C#, the collection must Implement this interface Supports functions like: - Void Reset(), - bool MoveNext(), - object Current

72 Oct 2007 SDP-MSc Slide 72 class MyClass : IEnumerable { IEnumerator Letter { get { yield return "A"; yield return "B"; yield return "C"; } } public IEnumerator GetEnumerator() { return Letter; } }

73 Oct 2007 SDP-MSc Slide 73 class MainClass { static void Main() { MyClass mc1 = new MyClass(); foreach (string s in mc1) Console.Write("{0} ", s); } } Mc – IEnumerable type Implied call to getEnumerator - Foreach works on IEnumerator type

74 Oct 2007 SDP-MSc Slide 74 Generics: public class ArrayList { private T[] itemArray; public void Add(T newItem) {...} public T this[int i] {...} }

75 Oct 2007 SDP-MSc Slide 75 ArrayList strArrList = new ArrayList (); strArrList.Add(“fred”); string str = strArrList[0];

76 Oct 2007 SDP-MSc Slide 76 public class ObjectList : CollectionBase { private ArrayList list = new ArrayList(); public int Add(ItemType value) { return list.Add(value); } public void Remove(ItemType value) { list.Remove(value); } public ItemType this[int index] { get { return (ItemType)list[index]; } set { list[index] = value; } } }

77 Oct 2007 SDP-MSc Slide 77 ObjectList list = new ObjectList (); // Add two strings. list.Add("blue"); list.Add("green"); :

78 Oct 2007 SDP-MSc Slide 78 public class GenericList { private class Node { public Node(T t) { next = null; data = t; } private Node next; public Node Next { get { return next; } set { next = value; }} private T data; public T Data { get { return data; } set { data = value; } } } Inner Class Node

79 Oct 2007 SDP-MSc Slide 79 private Node head; // constructor public GenericList() { head = null; } // T as method parameter type: public void AddHead(T t) { Node n = new Node(t); n.Next = head; head = n; }

80 Oct 2007 SDP-MSc Slide 80 public T RemoveFirst() { if (head == null) throw new Exception(); else { T res = head.Data; head = head.Next; return res; } public IEnumerator GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } }}

81 Oct 2007 SDP-MSc Slide 81 class TestGenericList { static void Main() { GenericList list = new GenericList (); for (int x = 0; x < 10; x++) { list.AddHead((char)(x+65)); } foreach (char i in list) { System.Console.Write(i + " "); }


Download ppt "Oct 2007 SDP-MSc Slide 1 Section 9 Graphic Programming."

Similar presentations


Ads by Google