صفحه 1:
Pee DLs re) Ste Sy
Java)
er ال
صفحه 2:
]4 تراسا
Overloading
متدهاى با نام مشابه دو وجه تمايز نسبت به يكديكر دارند:
* تعداد آرگومانها
"" نوع داده اى و اشياء متفاوت به عنوان آركومان
ا ا 00
همگی عمل یکسانی را انجام دهند.
9
باز كشتى يكسان داشته باشند. در غير اين صورت برنامه جاوا
کامپایل نخواهد شد.
صفحه 3:
سك
simport ۱
۲ و ود۱» (
sint x1 = 0
sint yl = 0
sint x2 = 0
:۱ ۷2 < 0
MyRect buildRect(int x1, int y1, int x2, int
}y2)
;jthis.x1 = x1
;this.yl = y1
;jthis.x2 = x2
;jthis.y2 = y2
داطخ یلاعت وه
{
صفحه 4:
MyRect buildRect(Point topLeft, Point
( 0۵۲۲۵۳۴۲9۳
۲00۳ < 1
اكيت ا ۸۸۱-۰
22 - oats lil ey
sy2 = bottomRight.y
لليف روا تيه
1
ع buildRect(Point topLeft, int w, int
3x1 = topLeft.x
yl = topLeft.y
2 < )1 + ۷۷(
iy2 = (yl + h)
sreturn this
1
صفحه 5:
}Ovoid printRect
eee <"+x1l+%,“+
22۸
y2 + ۱ ی
5
i
} public static void main(String arguments[])
;()MyRect rect = new MyRect
ETC ToL ل ب لیا تنیز
;coordinates 25,25, 50,50:”)
srect.buildRect(25, 25, 50, 50)
;Qrect.printRect
i(**)System.out. printin
System.out.printin(“Calling buildRect with
;points (10,10), (20,20):”)
rect.buildRect(new Point(10,10), new
;Point(20,20))
صفحه 6:
("**")System.out.printin
System.out.print(“Calling buildRect
;with 1 point (10,10),”)
System.out.printin(“ width (50) and
sheight (50):”)
rect.buildRect(new Point(10,10), 50,
:50(
;Orect.printRect
("**“)System.out.printin
1
0
صفحه 7:
0
* متد سازنده متدی است. که در زمان ایجاد یک شیء از کلاس
TCS (20 BRC Cn Se) ESE IE Pes)
نمی توان مستقیماً فراخوانی کرد. متد سازنده هنگام استفاده از
عملگر 196۷4 فراخوانی می شود.
00 0
متغیرهای نمونه را خودتان مقدار دهی کنید. انجام این عمل با
فراخوانی متدهای دیگر قابل انجام است.
صفحه 8:
نكته 9 مثال
ا ا ل 1
ا ا لدان
و
ape Ceny ا ل
ا ECD) RUE ACB) ECE
"" مثال:
class Person }
;String name
;int age
Person(String n, int a) }
;name =n
صفحه 9:
jage=a
} Qvoid printPerson
System.out.print(“Hi, my name is “ +
;name)
System.out.printin(*. | am “ + age + “
years old.”)
public static void main (String
} arguments[])
;Person p
;p = new Person(“Luke’”, 50)
مهوععط]وأءوم.م():
:) (۳
صفحه 10:
)35 اهروت ات۱۰۳
;0p-printPerson
;(‘---")System.out.printin
0
{
صفحه 11:
۱۳ 1 Pea
aeey) ante Wren Sey nC re eum leper aye Sita eS CT pela
دیگر به مثال زیر توجه کنید.
مثال: ۴
}class myCircle
تایه ۱و۱
۱۷۹ tents را رأمتمطلا غخصا بأمتمطعر
}rediusLength)
نا x = xPoint
۱ تا
و radius = radiusLength
{
}myCirlce(int xPoint, int yPoint)
;jthis(xPoint, yPoint, 1)
1
صفحه 12:
مثال
jimport java.awt.Point
| تت 2۸
jint x1 2 0
nt yl =
nt x2 =
ع 2يإ غخمار 0
}MyRect2(int x1, int y1, int x2, int y2)
;this.x1 = x1
sthis.yl = y1
sthis.x2 = x2
;jthis.y2 = y2
{
صفحه 13:
} MyRect2(Point topLeft, Point bottomRight)
5x1 = topLeft.x
yl = topLeft.y
3x2 = bottomRight.x
sy2 = bottomRight.y
}MyRect2(Point topLeft, int w, int h)
5x1 = topLeft.x
sy1 = topLeft.y
x2 = (x1 + w)
iy2 = (yl + h)
if
صفحه 14:
} Qvoid printRect
System.out.print(“MyRect: <” + x1 + “, “
i+ yl)
+ ۷2 + ۳ ," + 2 ۲ * ار
4
public static void main(String
} arguments[])
sMyRect2 rect
System.out.printin(“Calling توا اس
swith coordinates 25,25 50,50:”)
srect = new MyRect2(25, 25, 50, 50(
1
("***“)System.out.printin
صفحه 15:
System.out.printin(“Calling MyRect2 with
;points (10,10), (20,20):”)
rect= new MyRect2(new Point(10,10), new
;Point(20,20))
;Qrect.printRect
((**)System.out.printin
System.out.print(*Calling MyRect2 with 1
;point (10,10)”)
System.out.printin(* width (50) and height
i(50):”)
Ds = new MyRect2(new Point(10,10), 50,
;Qrect.printRect
("**")System.out. printin
{
1
صفحه 16:
Overriding Methods
| eee Ow RCT tor EC el
Re Cpe carne ا لت EEC
فرزند است. حق تقدم دارد.
مثال: *
Super Class//
[ رد ییوس یات
jintx =0
۱ printMe
;System.out.printin("x is "+x +", yis " + y)
Seas .out.printin("I am an ۱۳۷ معط of the
class
i((this.getClass().getName
۱
صفحه 17:
Sub Class//
class PrintSubClass extends
( ۳۲۱۱۲۵۱۵5
sint z = 3
public static void main(String
} arguments[])
PrintSubClass obj = new
;()PrintSubClass
حل ز00():
۱
0
صفحه 18:
Overriding Construcror
3
Allama
} class NamedPoint extends Point
;String name
} NamedPoint(int x, int y, String name)
ssuper(x,y)
;this.name = name
0
0
17 1 اللي ل ال ا ل
;NamedPoint np = new NamedPoint(5, 5, "SmallPoint")
;System.out.printin("x is " + np.x)
;System.out.printin("y is " + np.y)
;System.out.printin("Name is " + np.name)
1
t
1
i
صفحه 19:
متد خاتمه دهنده: ۱6۲۳0۵ ۴۱۱۱۵۱۱26۲
ا ا ا ا ا
نیاز به یک شیء خاتمه می یابد فراخوانی می شود.
Protected void finalize() throws
} Throwable
;(Super.finalize
1
1
صفحه 20:
Applet: cu
PSS p Serer ا
اجرا می شوند. تفاوت ایلتها با برنامه های کاربردی
sleal es 0925 29 Igle ۱
كاربردى با درج نام برنامه در خط فرمان اجرا مى
ا ا 00
صفحه 21:
محدوديتهاى ابلتها
ا 0
20
۴ اپلتها نمی توانند با هیچ سایتی غیر از سایتی که از روی آن
بارگذازی شده اند. ارتباط برقرار کنند.
اپلتها نمی توانند هیچ برنامه ای را در سیستم سرویس
كيرنده اجرا كنند.
Beek pear ORC Re اپلتها نمی توانند هیچ *
۳ D)
صفحه 22:
لحنت
ا ا ا 0
public class Applet_Name extends
}java.applet.Applet
Applet Core Comes Here//
1
0
tefl ا الل نك
صفحه 23:
متدهای ابلتها
۱
و و
stop ».™
destroy..."
متد]۳021۳ "
صفحه 24:
متد 101
سا اس
اعمالی نظیر ایجاد اشیاء مورد نیاز اپلت. تنظیمات اولیه,
بار گذاری تصاویر و فونتها و تنظیم پارامترها را انجام می
دهد.
}Qpublic void init
Core here//
1
1
صفحه 25:
Start متد
1
el pb ab oad ilgsl,3 STOP a2. 45 زمانی هم
است.
}Qpublic void start
Core here//
1
1
صفحه 26:
Stop
اين متد اجراى ايلت را متوقف مى كند ""
}Qpublic void stop ®
Core here//
۱
صفحه 27:
د
و
NU AC ey ne a bead
01 او اشیاء از بین می روند. اگر اپلت از منابع
خاصی نظیر 0۲680]]ها استفاده نکرده باشد. نیازی به
©0611 كردن اين متد وجود ندارد.
}Qpublic void destroy
Core here//
۱
صفحه 28:
0
"ا اين متد نحوهى نمايش ايلت را كنترل مى كند. به كمكد
این متد می توان متنء خطوط. رنگ یش eres meen) از ۱
را برای اپلت تعیین کرد. این متد بعد از متد 1101 اجرا
می شود. هر گاه اپلت نیازمند به روز رسانی باشد. این متد
مجددا فراخوانی می شود.
}Qpublic void paint
Core here//
۱
صفحه 29:
منال
JAVA FILE
mport java.awt.Graphics
mport java.awt.Font
;import java.awt.Color
public class Palindrome extends
ل 1 ١
os f = new Font("TimesRoman", Font.BOLD,
ناعم
public void paint(Graphics screen) }
;screen.setFont(f)
;screen.setColor(Color.red)
screen.drawString("Go hang a salami, I'ma
jlasagna hog.", 5, 40)
{
rf
صفحه 30:
‘HTML FILE
<HTML>
<HEAD>
<TITLE>The Palindrome Page</TITLE>
<HEAD/>
<BODY>
:P>My favorite meat-related palindrome is>
<BR>
APPLET CODE="Palindrome.class" WIDTH=600>
<HEIGHT=100
!A secret if your browser does not support Java
<APPLET/>
<BODY/>
<HTML/>
صفحه 31:
APPLET .S
lezh! 029541 ol» HTML obj > APPLET 1s ® 4
صفحات وب استفاده می شود. این تگد دارای خصایصی به شرح
زير مى باشد.
۷ 22 ار 0 Pere
ere (cl) peer 1ك
"! خصيصه لاا ت)ااه: تعيين كننده محل قرار كرفتن ايلت در
صفحه 32:
خصا
ALIGN Va
ورد
RIGHT
TEXTTOP
۱۱: عامط
MIDDLE
BASELINE
BOTTOM
ABSBOTTOM
صفحه 33:
نکته
<BR CLEAR = LEFT>
SI Pape Renny Canto cnge le
<BR CLEAR = RIGHT>
۱ ىا
<BR CLEAR = ALL>
اولين فضاى خالى رادر نظر مى كيرد. !"
صفحه 34:
و
ا 02200
HSPACE كنترل فضاى افقی(سمت چپ و راست) و
0
الك a
APPLET CODE = “ShowSmiley.class’>
WIDTH = 45 HEIGHT = 42 ALIGN =
<LEFT VSPACE = 50 HSPACE = 10
Required Java
<APPLET/>
صفحه 35:
خصایص 60۳0۴ و 609۵۴8۵5۴
۴ تعیین محل قرار گرفتن فایل کلاس پایه اپلت و دیگر
A ا 1
pmo CODEBASE tured 9 oS (oo cys) 318
CN Tees 1
"" نكته: در صورتى كه از خصيصه 15 18/825 201015 به
ean pers 0( 000000
ا eee Disb eee
خواهد بود که اپلت درون آن قرار دارد
صفحه 36:
17
APPLET CODE = “Bix.class” HEIGHT =>
<“40” WIDTH = “400’> </APPLET
APPLET CODE = “Bix.class” CODEBASE>
= “Toshire” HEIGHT = “40” WIDTH =
<"400’> </APPLET
APPLET CODE = “Bix.class” CODEBASE>
“http://www. Toshire.com/javaclasses”
HEIGHT = “40” WIDTH = “400’>
<</APPLET
صفحه 37:
Object S
را OMEN
حالی که تگ. ۸۳۳۱۲ تنها توانایی بار گذاری اپلتها را
ال
OBJECT CLASSID =>
“java.bix.class” CODEBASE =
1ن 2 Cl ال ا لل
<’WIDTH = “400
<OBJECT/>
صفحه 38:
کلاس 6۲۵0۱۱65
و
ی ]2۷3.8۷0[ قرار دارد.
* مثال:
;import java.awt.Graphics
;import java.awt.Polygon
public class Map extends java.applet.Applet
0
} public void paint(Graphics screen)
;screen.drawString("Florida", 185, 75)
1
1
صفحه 39:
تر سیم اشکال هندسی
صفحه 40:
ترسيم خطوط
* براى ترسيم خطوط از متد 0۲3۷۷1۳6 استفاده می
شود. این متد خطی بین دو نقطهى (1ل/إ,0»1) و
(2لا,2“)ترسيم مى كند.
;drawLine(x1,y1,x2,y2)
صفحه 41:
چهار ضلعی
pep) eee pe ۱9
eee ا See
cent Tea oa a
;drawRect(x, y, width, height)
٠١ ) مستطيل با كوشه هاى كرد
drawRoundRect(x, y, width, height,
;roundWidth, RoundHeight)
Fea ا peer ros)
Bye MPO CL ie LCC MO C=C 4
صفحه 42:
PES CeO UC iad) hii: ol eee ne nee a wens Delis
ByC Pee mL xe hCe (oli eeeas Olin CRMs ht)
ح و 410, 20, 30, 40, 50(
jint y[] = {15, 25, 35, 45, 55}
;int points = x.Length
;Polygon poly = new Polygon(x, y, points)
* نکته: پس از ایجاد چند ضلعی برای افزودن یک نقطه ی جدید به آن از متد
(۷ ,*)]2010۳01۳0ز استفاده می شود.
* نکته: برای پر كردن فضای داخل چند ضلعی از متد ۴۱۱۱۵۵۱۷901
ا 0
صفحه 43:
بیضی و دایره
ees ا الل
براى ير كردن فضاى داخل آن از متد 1/1 611!0()
استفاده می شود.
;drawOval(x, y, width, height)
* نکته: مقدار ۷۷۱۵۲۱۱ و ۱610۱۴ برای ترسیم دایره
برابر است.
صفحه 44:
کمان
0 ا لف
PSO Ug eco rel IN CSNY Crepe
شود.
drawArcs(x, y, width, height,
;start_angle, traveled_degree)
صفحه 45:
مثال
;import java.awt.Graphics
simport java.awt.Polygon
ublic class Map extends java.applet.Applet
} public void paint(Graphics screen)
;screen.drawString("Florida", 185, 75)
sscreen.drawLine(185,80,222,80)
sscreen.drawRect(2, 2, 345, 345)
screen.drawRoundRect(182,61,43,24,10,8)
int x[] = { 10, 234, 253, 261, 344, 336,
,295, 259, 205, 211
ل 10 ):
int y[] = { 12, 15, 25, 71, 209, 278, 310,
صفحه 46:
jint pts = x.length
;Polygon poly = new Polygon(x, y, pts)
;screen.drawPolygon(poly)
sscreen. fillOval(235,140,15,15)
sscreen. fillOval(225,130,15,15)
sscreen. fillOval(245,130,15,15)
for (int ax = 50; ax < 150; ax += 10)
for (int ay = 120; ay < 320; ay += 10)
screen.drawArc(ax, ay, 10, 10, 0, -
:180(
if
1
صفحه 47:
ols
ار eee
ل ا ل Byer
copyArea(x, y, width, height,
shorizontal_distance, vertical_distance)
;screen.CopyArea(0, 0, 100, 100, 50, 25)
;clearRect(x, y, width, height)
3 00 (=t- اا RON CLIT Pe SHIN CUE DC MOT MTS De Tosi
ا 0ت BRST)
screen.clearRect(0, 0, size().width,
;size(). height)
صفحه 48:
me) ere |
* برای ایجاد شیء 018 باید سه آر گومان به متد سازنده ی کلاس
ترا 0 Pl eee)
LJ نام فونت
cols ا ا ل (Ube 1 i Comm
"" سایز فونت
* مثال:
Font f = new Font(“Dialog’, Font.BOLD +
;Font.ITALIC, 24)
ee else nT pec ela ل و Pee
شود.
jscreen. setFont(f)
Le WWE) a Lie eos] ieee e ITS Se wees Be lag ل ا
مس
صفحه 49:
مثال
jimport java.awt.Font
jimport java.awt.Graphics
jimport java.awt.FontMetrics
} public class SoLong extends java.applet.Applet
} public void paint(Graphics screen)
;Font f = new Font("Courier", Font.BOLD, 18)
;FontMetrics fm = getFontMetrics(f)
;screen.setFont(f)
String s = "So long, and thanks for all the
ارت ریت
int x = (size().width - fm.stringWidth(s)) / 2
jint y = size().height / 2
;screen.drawString(s, x, y)
i
t
صفحه 50:
رنک
"ا با استفاده از كلاسهاى 0101© و ©2366 601015 مى
PELE SRM CH epee ol BST ORCS Tel)
yaya)
صفحه 51:
ادامه دارد...
* برای اطلاع از قسمت های بعدی به سایت مراجعه کنید.