interactive java note - java applets
TRANSCRIPT
-
8/3/2019 Interactive Java Note - Java Applets
1/28
Programming Language (JAVA)
Unit 6.11 Applets
Presentation 1
-
8/3/2019 Interactive Java Note - Java Applets
2/28
Pengaturcaraan Applet
Applets adalah program Java yang dilaksanakandengan Web browser .
Appletviewer adalah utiliti Java yangmelaksanakan applet.
Applet adalah fail kelas yang khas ditulis untukmemaparkan grafik dalam Web browser .
-
8/3/2019 Interactive Java Note - Java Applets
3/28
Applets
Applets dimuatkan dalam laman Webmenggunakan tag HTML . Apabila laman Web yang mengandungidilarikan, ianya dimuat turunkan ke memorisecara automatik dan dilaksanakan oleh Web browser .
Applet dipaparkan dalam ruang yang tertentudalam laman Web page dipanggil AppletWindow.
-
8/3/2019 Interactive Java Note - Java Applets
4/28
Applets
Applet boleh mengandungi objek-objek yangdimuatkan seperti :
Animated graphics
Video games
Advanced text displays and images
Audio
-
8/3/2019 Interactive Java Note - Java Applets
5/28
Jenis Applet
Local Applets
Applets yang disimpan di dalam local machine
Remote AppletsApplets yang disimpan dalam remote computer .
Ini boleh dimuat turunkan dan diletakkan dalam lamanWeb dari Internet.
-
8/3/2019 Interactive Java Note - Java Applets
6/28
Local Applets
-
8/3/2019 Interactive Java Note - Java Applets
7/28
Remote Applets
-
8/3/2019 Interactive Java Note - Java Applets
8/28
Metod asas Applet
Ada 5 metod asas :
1. init()
2. start()
3. stop()
4. destroy() 5. paint(Graphics g)
-
8/3/2019 Interactive Java Note - Java Applets
9/28
Life Cycle of Applets
-
8/3/2019 Interactive Java Note - Java Applets
10/28
init()
Syntax
public void init( )
{
;
}
-
8/3/2019 Interactive Java Note - Java Applets
11/28
paint()
Syntax
public void paint(Graphics g)
{ ;
}
-
8/3/2019 Interactive Java Note - Java Applets
12/28
Kelas Applet
Semua applet yang dicipta perlu menjadi subkelas kepada kelas Applet .
Program Applet perlu import 2 pakej iaitu, awtdan applet atau swing.
-
8/3/2019 Interactive Java Note - Java Applets
13/28
Class Inheritance of Applet Class
-
8/3/2019 Interactive Java Note - Java Applets
14/28
Execution of Applet
-
8/3/2019 Interactive Java Note - Java Applets
15/28
tag
Syntax
/*
-
8/3/2019 Interactive Java Note - Java Applets
16/28
Sections of Web page
Comment Section Mengandungi komenmengenai laman web.
Head Section mengandungi tajuk laman Web.
Body Section mengandungi teks dan arahanyang menentukan bagaimana teks, imej,animasi dan sebagainya patut wujud dalamlaman Web.
Tag Applet dimasukkan dalam body section .
-
8/3/2019 Interactive Java Note - Java Applets
17/28
Sample Applet
-
8/3/2019 Interactive Java Note - Java Applets
18/28
Kelas Grafik
Ciri yang paling penting bagi Java adalahmenyokong grafik.
Anda boleh menulis applet untuk melukisgarisan, pelbagai bentuk , imej dan teksdalam pelbagai jenis font dan gaya.
Saiz tetingkap applet didefinisikan olehattribut tag .
-
8/3/2019 Interactive Java Note - Java Applets
19/28
Metod Kelas Grafik
Method Description
clearRect( ) Erases a rectangular area of thecanvas.
copyArea( ) Copies a rectangular area of the canvasto another area.
drawArc( ) Draws a hollow arc.
drawLine( ) Draws a straight line.
drawOval() Draws a hollow oval.
-
8/3/2019 Interactive Java Note - Java Applets
20/28
Metod Kelas Grafik
Method Description
drawPolygon( ) Draws a hollow polygon.
drawRect( ) Draws a hollow rectangle.
drawroundRect( ) Draws a hollow rectangle withrounded corners.
drawstring( ) Displays a text string.
fillArc( ) Draws a filled arc.
-
8/3/2019 Interactive Java Note - Java Applets
21/28
Metod Kelas Grafik
Method Description
fillOval( ) Draws a filled oval.
fillPolygon( ) Draws a filled polygon.
fillRect( ) Draws a filled rectangle.
fillRoundedRect( ) Draws a filled rectangle withrounded corners.
-
8/3/2019 Interactive Java Note - Java Applets
22/28
Metod Kelas Grafik
Method Description
getColor( ) Retrieves the current drawingcolour.
getFont( ) Retrieves the currently usedfont.
setColor( ) Sets the drawing colour.
setFont( ) Sets the font.
-
8/3/2019 Interactive Java Note - Java Applets
23/28
Melukis garisan
Semasa melukis garisan dalam tetingkap appletwindow menggunakan metod drawLine( ) , andaperlu sepasang koordinat.
Contoh:
g.drawLine(20,20, 60,60);
-
8/3/2019 Interactive Java Note - Java Applets
24/28
Drawing a Circle and an Ellipse The drawOval() method is used to draw acircle or an ellipse.
Example:
g.drawOval(10,20,50,30);
fillOval() is similar to drawOval() but fills theoval with a colour.
-
8/3/2019 Interactive Java Note - Java Applets
25/28
Drawing a Rectangle
drawRect( ) method is used to draw arectangle. This method takes four arguments.
Example:
g.drawRect(20,70, 30,40);
-
8/3/2019 Interactive Java Note - Java Applets
26/28
Draw Rectangle
The fillRect() method is similar to drawRect() method but the fillRect() method fills the
rectangle with a colour.The drawRoundRect() method is similar todrawRect() method but drawRoundRect() method draws a rectangle with rounded corners.
The fillRoundRect() method is similar todrawRoundRect() method butfillRoundRect() method fills the rectangle witha colour.
-
8/3/2019 Interactive Java Note - Java Applets
27/28
setColor()
The setColor() method is used to set thecurrent foreground colour.
The following code creates an object of Colorclass and sets the foreground colour to red.
Color col=new Color(255,0,0);
g.setColor(col);
The foreground colour can also be set using g.setColor(Color.Red);
-
8/3/2019 Interactive Java Note - Java Applets
28/28
getColor()
The getColor() method is used to get thecurrent foreground colour of the applet.
The following is the code to get the currentforeground colour.
g.getColor();