Graphic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Graphic Programming

Description:

I.e bounces back. class GFrame2 :GFrame1 ... so the ball initially diagonally & bounces. off each wall ... the ball initially diagonally & bounces ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 82
Provided by: dra77
Category:

less

Transcript and Presenter's Notes

Title: Graphic Programming


1
Section 9
Graphic Programming
2
(No Transcript)
3
using System using System.Windows.Forms using
System.Drawing class GFrame1Form
protected int x10, y20 protected
Label l1 protected Font fnew
Font("Times New
Roman",20,FontStyle.Bold)
4
public GFrame1() l1new Label()
l1.Fontf 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
(No Transcript)
6
class GFrame2 GFrame1 protected Label
l2new Label() public GFrame2()base()
l2.Text"Home" l2.SetBounds(x,y40
,160,43) l2.Font f
Controls.Add(l2) public
class Test92 public static void Main(string
args) Application.Run(new GFrame2())
7
(No Transcript)
8
class GFrame2 GFrame1 protected Button
b1new 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
(No Transcript)
10
class GFrame2 GFrame1 protected Button
b1new 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
Using Paint Feature
12
class GFrame2 GFrame1 protected Button
b1new 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)
starttrue this.Refresh()
13
protected override void OnPaint(PaintEventArgs
pe) base.OnPaint(pe)
Graphics g pe.Graphics
SolidBrush brushnew SolidBrush(Color.Black)
if (starttrue)
g.DrawString("Everyone",f,brush,30,60)
public class Test92 public static
void Main(string args) Application.Run(new
GFrame2())
14
(No Transcript)
15
First a new Version of the Base class
16
using System using System.Windows.Forms using
System.Drawing using System.Threading class
GFrame1Form protected int x10, y20
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
brushnew SolidBrush(Color.Black)
g.DrawString("Welcome",f,brush,x,y)

17
class GFrame2 GFrame1 protected Button
b1new 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)
starttrue this.Refresh()

18
protected override void OnPaint(PaintEventArgs
pe) base.OnPaint(pe)
Graphics g pe.Graphics
SolidBrush brushnew SolidBrush(Color.Black)
if (starttrue)
x15 Thread.Sleep(1000) this.Refresh()

public class Test92 public
static void Main(string args) Application.Run
(new GFrame2())
19
I.e restarted from left of screen
20
class GFrame2 GFrame1
protected override void
OnPaint(PaintEventArgs pe)
base.OnPaint(pe) Graphics g
pe.Graphics SolidBrush brushnew
SolidBrush(Color.Black) if
(starttrue) x15
Thread.Sleep(1000) if (xgt280)x10
// reset this.Refresh()
21
I.e bounces back
22
class GFrame2 GFrame1
private int step15
protected override void OnPaint(PaintEventArgs
pe) base.OnPaint(pe)
Graphics g pe.Graphics
SolidBrush brushnew SolidBrush(Color.Black)
if (starttrue)
xstep Thread.Sleep(1000) if
((xgt180)xlt10)stepstep-1 this.Refresh()

23
Ex91. Rewrite Test91 so text moves vertically
up down screen. Note Repositioning of
Button
24
(No Transcript)
25
using System using System.Windows.Forms using
System.Drawing using System.Threading class
GFrame3Form protected int x10, y10
protected SolidBrush brush public
GFrame3() brush
new SolidBrush(Color.Blue)

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
(No Transcript)
28
class GFrame4 GFrame3 protected Button
b1new 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.ColorColor.Yellow
this.Refresh() public
class Test92 public static void Main(string
args) Application.Run(new GFrame4())
29
(No Transcript)
30
class GFrame4 GFrame3 protected Button
b1new Button() private bool start false
private int step8 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)
starttrue this.Refresh()

31
protected override void
OnPaint(PaintEventArgs pe)
base.OnPaint(pe) if
(starttrue) ystep
Thread.Sleep(500) if
((ygt219)ylt10)stepstep-1
this.Refresh()
public class Test92 public static void
Main(string args) Application.Run(new
GFrame4())
32
Ex92. Rewrite Test99 so the ball moves around
the Frame
33
Ex93. Rewrite Test99 so the ball moves
vertically up down the Frame but
not go as high next time eventually stops
34
Ex94. Rewrite Test99 so the ball initially
diagonally bounces off each wall .
35
Ex95. Rewrite Ex94 so the ball initially
diagonally bounces off each wall
accelerating as it reflects off wall
36
class GFrame3 extends JFrame protected
Container content protected int x10,
y30 protected Color cColor.blue
public GFrame3() contentgetContentPane()
content.setLayout(null)
setSize(300,300)
setVisible(true) public void
paint(Graphics g) super.paint(g)
g.drawLine(x, y40, x30,y) public
class Test910 public static void main(String
args) GFrame3 e new
GFrame3() Ex96. This Application produces the
following result
37
Now complete the application (using inheritence)
so it draws a triangle which moves up down
screen after button clicked
38
(No Transcript)
39
Ex97. Now rewrite the application so the triangle
moves around screen
40
GUI Front End
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
class MyCounterCounter public MyCounter(int
v, String n)base(v,n)

public void decrement() this.value--
public void add(int amt)
this.valueamt
43
class GFrameForm private Label
lmainnew Label() private Label l1new
Label() private Button b_incrnew
Button() private Button b_decrnew
Button() private Button b_addnew
Button() private TextBox t1new TextBox()
private TextBox t2new TextBox()
private Font fnew Font("Times New
Roman",15,FontStyle.Bold) private
MyCounter c new MyCounter(1,"Score")
44
public GFrame()base()
lmain.Text"Counter Application"
lmain.Fontf
l1.Text"Value" l1.Fontf
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

b_incr.Click new EventHandler(this.button1_Clic
k) 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
private void
button1_Click(object sender, EventArgs e)
c.increment() t1.Text"
"c.read_value() private void
button3_Click(object sender, EventArgs e)
int amtint.Parse(t2.Tex
t) c.add(amt) t1.Text"
"c.read_value()
47
public class Test92 public static void
Main(string args) Application.Run(new
GFrame())
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.balancebal
this.accNoNo public int getAccNo()return
accNo public int getBalance() return
balance public void credit(int amount)
balanceamount public boolean debit(int
amount) if (amount gt
balance) return false else balance
balance - amount return
true
49
(No Transcript)
50
Ex99. Put this GUI front -end with the following
Time class
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 (sec60) sec0 min
if (min60)min0 hour public
void decrement_sec() if (secgt0) sec--
else
sec59 if(mingt0) min--
else min59 hour-- public
String read_time()
return ""hour""min""sec
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 gt current
bid
Online Auction
53
Generics
54
class ArrayTest private int
values public ArrayTest(int v)
values v
public int first()
if (values.Length 0) throw new
Exception() return
values0 public void printAll()
Console.WriteLine()
Console.Write("") foreach (int el
in values) Console.Write("
" el) Console.WriteLine("")

Version 1
55
class GenericsEx public static
void Main() int a 2,
6, 3, 5 ArrayTest atnew
ArrayTest(a) int res at.first()
Console.WriteLine(First" res)
at.printAll()
56
class ArrayTest private double
values public ArrayTest(double v)
values v
public double first()
if (values.Length 0) throw
new Exception() return
values0 public void printAll()
Console.WriteLine()
Console.Write("") foreach (double
el in values)
Console.Write(" " el)
Console.WriteLine("")
Version 2
57
class GenericsEx public static void
Main() double a 2.25,
6.5, 3.25, 5.5 ArrayTest atnew
ArrayTest(a) double res
at.first() Console.WriteLine("Sum"
res) at.printAll()

58
class ArrayTestltTgt private T
values public ArrayTest(T v)
values
v public T first()
if (values.Length
0) throw new Exception()
return values0 public
void printAll()
Console.WriteLine() Console.Write("")
foreach (T el in values)

Console.Write(" " el)
Console.WriteLine("")
Version 3
59
class GenericsEx public static
void Main() double a
2.25, 6.5, 3.25, 5.5
ArrayTestltdoublegt atnew ArrayTestltdoublegt(a)
double res at.first()
Console.WriteLine("First" res)
at.printAll()
60
interface First int Add()
class SecondltTgt 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
class MyT First private int v1,v2
public MyT(int a, int b)v1a v2b
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) SecondltMyTgt sec new
SecondltMyTgt(mt1, mt2)
Console.WriteLine("Sum" sec.sum())
62
//Error public class Test public static
void Main() MyT mt1 new MyT(1,
2) MyT mt2 new MyT(3, 4)
Secondltintgt sec new Secondltintgt(mt1,
mt2) Console.WriteLine("Sum"
sec.sum())
63
class ArrayTestltTgt where TIComparable
private T values public ArrayTest(T v)
values v public T
largest() if (values.Length 0)
throw new Exception() T res
values0 foreach (T el in values)
if (res.CompareTo(el)lt0) resel
return res
64
class GenericsEx public static void Main()
double a 2.25, 6.5, 3.25, 5.5
ArrayTestltdoublegt at new
ArrayTestltdoublegt(a) double res
at.largest() Console.WriteLine("Largest"
res) at.printAll()
65
  • IEnumerator Interface
  • has following members
  • Property Current
  • Method MoveNext
  • Method Reset
  • "Supports a simple iteration over a
    collection.", as well as

66
  • IEnumerable Interface
  • 1 method called
  • GetEnumerator.
  • "Exposes the enumerator
  • support the ForEach semantics

67
foreach loop Iterates through a
composition
i.e IEnumerator type public class
Array3 static int add(int b) int
total0 foreach (int element in b)
total total element return
total
b-Must be an IEnumerable type Implied call to
b.GetEnumator()
68
Yield Only used in an Iterator Block //
yield-example.cs using System using
System.Collections public class List
69
public static IEnumerable Power(int number, int
exponent) int counter 0 int result 1
while (counter lt exponent)
result result number yield
return result
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
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
class MyClass  IEnumerableltstringgt   IEnumerat
orltstringgt Letter         get               
yield return "A"         yield return "B"     
    yield return "C"            public IEnu
meratorltstringgt GetEnumerator()         return 
Letter   
73
class MainClass   static void Main()        
 MyClass mc1  new MyClass()      foreach (strin
g s in mc1)         Console.Write("0 ", s)   

Mc IEnumerable type Implied call to
getEnumerator - Foreach works on IEnumerator type
74
Generics public class ArrayListltTgt      pri
vate T itemArray           public void Add(T
newItem)      ...           public T thisint
i      ...
75
ArrayListltstringgt strArrList new
ArrayListltstringgt() strArrList.Add(fred) str
ing str strArrList0
76
public class ObjectListltItemTypegt
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 thisint index
get return (ItemType)listindex
set listindex value
77
ObjectListltstringgt list new ObjectListltstringgt()
// Add two strings. list.Add("blue")
list.Add("green")
78
public class GenericListltTgt 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
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
public T RemoveFirst() if (head
null) throw new Exception() else
T res head.Data head
head.Next return res
public IEnumeratorltTgt GetEnumerator()
Node current head
while (current ! null)
yield return current.Data
current current.Next
81
class TestGenericList static void Main()
GenericListltchargt list new
GenericListltchargt() for (int x 0 x lt
10 x) list.AddHead((char)
(x65)) foreach (char i in
list) System.Console.Write(i
" ")
Write a Comment
User Comments (0)
About PowerShow.com