Sunum yükleniyor. Lütfen bekleyiniz

Sunum yükleniyor. Lütfen bekleyiniz

Bölüm 3 - Java Appletlerine Giriş

Benzer bir sunumlar


... konulu sunumlar: "Bölüm 3 - Java Appletlerine Giriş"— Sunum transkripti:

1 Bölüm 3 - Java Appletlerine Giriş
İçerik Giriş 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak 3.4 Çizgi Çizmek ve String Yazmak 3.5 İki Sayıyı Eklemek 3.6 Java Applet Internet ve World Wide Web Kaynakları

2 3.1 Giriş Applet Program aşağıdaki programların birisiyle çalışır.
appletviewer (appletler için test aracı) Web browser (IE, Communicator) Applet içeren HTML (Hypertext Markup Language) dökümanlarını çalıştırır.

3 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği
Örnek Appletler Java 2 Software Development Kit (J2SDK) içinde gelen örnekelere bakacağız. Kaynak kodlar (.java dosyası) Yeni fikirlerin akla gelmesi açısından önemli J2SDK demo kalsörü içinde birçok örnek görülebilir. J2SDK aşağıdaki adresten indirilebilr. java.sun.com/j2se/1.4.1/

4 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği
Appletlerin çalışması Komut isteminden appletinizin bulunduğu yere gelin. c:\j2sdk1.4.1\demo\applets cd appletDirectoryName Appleti çalıştıracak bir HTML dasyası olacak. Yazın “ appletviewer example1.html” appletviewer html dosyayı yükler. HTML dosyasından hangi appletin çalışacağına karar verilir. Applet çalışır, Reload ve Quit komutları Applet menusünün altında bulunur.

5 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği
player "X“ olarak oyuna başlarsınız. Fig. 3.2 Sample execution of applet TicTacToe.

6 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği
Fig. 3.4 Sample execution of applet DrawTest. Mouse hareketleri ile şekillerin çizildiği alan. Component tıkalanır Lines veya Points seçilir. Bu GUI component combo box, choice veya drop-down list olarak bilinir. Tıkalanılarak istediğiniz renk seçilir. Bu GUI componentleri radio buttons olarak bilinir.

7 3.2 Java 2 Software Development Kit’den Birkaç Applet Örneği
Demonstrates 2D drawing capabilities built into Java2 Try changing the options to see their effect on the demonstration. Click a tab to select a two-dimensional graphics demo.

8 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Şimdi, kendi appletimizi oluşturacağız. Demodakiler gibi örnek yapmak için biraz zamanımız var. Birçok farklı teknikleri bilmemiz gerekir. Programımız Yeni bir applet oluşturacağız ve bu applet aşağıdakini yazacak; "Welcome to Java Programming!" Applet ve HTML dosyaları gösterilecek, sonra satır satır inceleme yapacağız.

9 Java applet Program Output
import allows us to use predefined classes (allowing us to use applets and graphics, in this case). // Fig. 3.6: WelcomeApplet.java // A first applet in Java. 3 // Java packages import java.awt.Graphics; // import class Graphics import javax.swing.JApplet; // import class JApplet 7 public class WelcomeApplet extends JApplet { 9 // draw text on applet’s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); 15 // draw a String at x-coordinate 25 and y-coordinate 25 g.drawString( "Welcome to Java Programming!", 25, 25 ); 18 } // end method paint 20 21 } // end class WelcomeApplet Java applet Program Output extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above.

10 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
// Fig. 3.6: WelcomeApplet.java // A first applet in Java. Yorumlar Kaynak kodun ismi ve appletin tanıtımı Önceden tanımlı sınıfları programımıza dahil etme Bir applet oluşturacağın zaman, JApplet sınıfını programına dahil etmelisin. (package javax.swing) Grafik çizimi yapabilmek için Graphics sınıfını (package java.awt) programına dahil etmelisin Çizgi, dörtgen, oval çizip yazı yazabilmek için import java.awt.Graphics; // import class Graphics import javax.swing.JApplet; // import class JApplet

11 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Appletde de en az bir tane sınıf tanımlanmalı (uygulama örneğinde olduğu gibi) class WelcomeApplet için class tanımlaması Anahtar kelime class dan sonra class ismi class isminden sonra extends Yeni classı türeteceğin ana class (JApplet) JApplet : superclass (ana class) WelcomeApplet : altclass (türemiş class) WelcomeApplet classı JApplet in method ve datalarına sahip public class WelcomeApplet extends JApplet {

12 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Sınıf JApplet bizim yerimize tanımlanmış Birisi kalkmış “Bir şeyin applet olması için neler lazım“ tanımlamış Applets 200 fazla metodda sahip! extends JApplet Miras yolu ile meodalarını yeniden yazmaya gerek olmadan programıma dahil ediyorum. JApplet sınıfının tüm detayını bilmeme gerek yok. public class WelcomeApplet extends JApplet {

13 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Class WelcomeApplet is a tasarım appletviewer veya browser WelcomeApplet sınıfının objesini oluşturur. Anahtar kelime public gerekli. Bir dosyada sadece bir tane public class olur. public class ismi dosya ismi ile aynı olamlıdır. public class WelcomeApplet extends JApplet {

14 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Sınıfımız JApplet sınıfından paint metodunu miras alır. paint metodunun default olarak gövde kodu yok. Bizim sınıfımızdaki paint metodu yeniden tanımlanur.(override) Methods paint, init, ve start Her appletin bu üç metoda ihtiyacı olamayabilir. İhtiyacı olduklarını yeniden tanımlar. public void paint( Graphics g )

15 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Method paint Satır paint metodununa aittir. Ekrana grafik çizer. void metodun işi bittiğinde hiçbirşey geri döndermeyeceği anlamındadır. Parentezlar parametre listesini tanımlamak içindir. Normalde, programcı metoda verilerini parametre yolu ile geçirir JOptionPane.showMessageDialog olduğu gibi paint parametrelerini otomatik olarak alır. Graphics objesi paint tarafından kullanılır. public void paint( Graphics g )

16 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
super.paint( g ); superclass JApplet tarafından paint metodu çağrılır. Her applet’in paint metodununu ilk satırında olmalıdır. Paint in gövde kodları Method drawString (Graphics sınıfının ) Graphics türünnde yaratılmış g objesi Method ismi, parametreleri için parantez Birinci parametre: yazılacak String İkincisi: x koordinatı (in pixels) Üçüncüsü: y koordinatı (in pixels) Java koordinat sistemi Pixel olarak ölçeklenir. Üst sol (0,0) g.drawString( "Welcome to Java Programming!", 25, 25 );

17 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Appleti çalıştırma Derleme javac WelcomeApplet.java Hata yoksa, bytecode lar WelcomeApplet.class içinde saklanır. HTML dosyası yüklemek Dosyayı appletviewer veya browsera yüklemek .htm or .html bitebilir. HTML dosyasında hangi applet varsa o applet çalışır.

18 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Basit HTML dosyası (WelcomeApplet.html) Genellikle .class dosyası ile aynı kalsörde bulunur. Hatırlayın, .class dosyası derlemeden sonra oluşur. HTML kodları (tagler) Genellikle çiftler halinde yazılır. < ile başlar . > ile biter. Satır 1 ve 4 – başlangıç bitiş HTML tagleri Satır 2 - <applet> tagine başlayış Applet için özel bir tag Appleti görüntülemek için width ve height parametreleri ister. Satır 3 - </applet> taginin bitişi <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html>

19 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
appletviewer sadece <applet> taglerinden anlar. Gerisini iptal eder. Küçük tarayıcı Appleti çalıştırma appletviewer WelcomeApplet.html .class dosyası çalışır. <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html>

20 3.3 Basit Java Appleti: Bir String ifadeyi Applete Yazmak
Web browser da appletin çalışması

21 3.4 Çizgi Çizmek ve String Yazmak
Daha fazla applet Birinci örnek İki satırlık text yazdırmak drawString kullanarak iki satırlık text yazdırmak. İkinci örnek Method g.drawLine(x1, y1, x2, y2 ) (x1, y1)’den (x2, y2)’ye kadar çizgi çizmek Hatırla (0, 0) appletin sol üst kısmını işaret eder. drawLine tkullanarak text in altını ve üstünü çerçeveleyelim.

22 1 // Fig. 3.9: WelcomeApplet2.java
// Displaying multiple strings in an applet. 3 // Java packages import java.awt.Graphics; // import class Graphics import javax.swing.JApplet; // import class JApplet 7 public class WelcomeApplet2 extends JApplet { 9 // draw text on applet’s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); 15 // draw two Strings at different locations g.drawString( "Welcome to", 25, 25 ); g.drawString( "Java Programming!", 25, 40 ); 19 } // end method paint 21 22 } // end class WelcomeApplet2 WelcomeApplet2.java 1. import 2. Class WelcomeApplet2 (extends JApplet) 3. paint 3.1 drawString 3.2 drawString on same x coordinate, but 15 pixels down The two drawString statements simulate a newline. In fact, the concept of lines of text does not exist when drawing strings.

23 HTML file Program Output
<applet code = "WelcomeApplet2.class" width = "300" height = "60"> </applet> </html> HTML file Program Output

24 1 // Fig. 3.11: WelcomeLines.java
// Displaying text and lines 3 // Java packages import java.awt.Graphics; // import class Graphics import javax.swing.JApplet; // import class JApplet 7 public class WelcomeLines extends JApplet { 9 // draw lines and a string on applet’s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); 15 // draw horizontal line from (15, 10) to (210, 10) g.drawLine( 15, 10, 210, 10 ); 18 // draw horizontal line from (15, 30) to (210, 30) g.drawLine( 15, 30, 210, 30 ); 21 // draw String between lines at location (25, 25) g.drawString( "Welcome to Java Programming!", 25, 25 ); 24 } // end method paint 26 27 } // end class WelcomeLines WelcomeLines.java 2. Class WelcomeLines (extends JApplet) 3. paint 3.1 drawLine 3.2 drawLine 3.3 drawString Program Output Draw horizontal lines with drawLine (endpoints have same y coordinate).

25 HTML file 1 <html>
<applet code = "WelcomeLines.class" width = "300" height = "40"> </applet> </html> HTML file

26 3.4 Çizgi Çizmek ve String Yazmak
Graphics sınıfının drawLine metodu Başlangıç x ve y koordinatı Bitiş x ve y koordinatı

27 3.5 İki Sayıyı Eklemek Bir sonraki applet
Bu sefereki küçük uygulamamız; iki sayının toplanması Sayıları ondalıklı kullanacağız. İlkel tipler double float Programı görelim, sonra satır satır inceleyelim...

28 * allows any class in the package to be used.
// Fig. 3.13: AdditionApplet.java // Adding two floating-point numbers. 3 // Java packages import java.awt.Graphics; // import class Graphics import javax.swing.*; // import package javax.swing 7 public class AdditionApplet extends JApplet { double sum; // sum of values entered by user 10 // initialize applet by obtaining values from user public void init() { String firstNumber; // first string entered by user String secondNumber; // second string entered by user 16 double number1; // first number to add double number2; // second number to add 19 // obtain first number from user firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); 23 // obtain second number from user secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" ); 27 // convert numbers from type String to type double number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber ); 31 2 // Adding two floating-point numbers 3 import java.awt.Graphics; // import class Graphics AdditionApplet.java 1. import 2. Class AdditionApplet (extends JApplet) 3. Fields 4. init 4.1 Declare variables 4.2 showInputDialog 4.3 parseDouble 5 6 public class AdditionApplet extends JApplet { * allows any class in the package to be used. 7 double sum; // sum of the values entered by the user 8 9 public void init() Field sum may be used anywhere in the class, even in other methods. Type double can store floating point numbers. 10 { String firstNumber, // first string entered by user secondNumber; // second string entered by user double number1, // first number to add number2; // second number to add 15 // read in first number from user firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); 20 // read in second number from user secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" ); 25

29 // add numbers sum = number1 + number2; 34 } // end method init 36 // draw results in a rectangle on applet’s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); 42 // draw rectangle starting from (15, 10) that is 270 // pixels wide and 20 pixels tall g.drawRect( 15, 10, 270, 20 ); 46 // draw results as a String at (25, 25) g.drawString( "The sum is " + sum, 25, 25 ); 49 } // end method paint 51 52 } // end class AdditionApplet // add the numbers sum = number1 + number2; 33 } 5. Draw applet contents 5.1 Draw a rectangle 5.2 Draw the results HTML file 34 35 public void paint( Graphics g ) 36 { // draw the results with g.drawString g.drawRect( 15, 10, 270, 20 ); g.drawString( "The sum is " + sum, 25, 25 ); 40 } 41 } 1 <html> 2 <applet code="AdditionApplet.class" width=300 height=50> 3 </applet> 4 </html> drawRect takes the upper left coordinate, width, and height of the rectangle to draw. <html> <applet code = "AdditionApplet.class" width = "300" height = "65"> </applet> </html>

30 Program Output

31 3.5 İki Sayıyı Eklemek Satır 1-2: Yorum
Satır 5: imports class Graphics Eğer kodlarda paket ve sınıf ismi tam kullanılırsa import ifadesine gerek yoktur. public void paint ( java.awt.Graphics g ) Satır 8: javax.swing paketinin özel kullanımı * javax.swing içindeki tüm sınıflar erişilebilir. JApplet ve JOptionPane kullanılmış * bütün sınıfları yüklemez. Derleyici sadece programda kullanılan sınıfları yükler. import java.awt.Graphics; // import class Graphics import javax.swing.*; // import package javax.swing

32 3.5 İki Sayıyı Eklemek Sınıf tanıtımı Sınıf üye değişkeni tanımlama
javax.swing paketindeki JApplet ten miras alınmış. Sınıf üye değişkeni tanımlama Sınıf gövdesinde tanıtılmalı, metod içinde tanıtılmamalı Metodda tanıtılan değişkenler yerel (lokal) değişkenlerdir. Metodda tanıtılan değişken sadece metod içinde kullanılabilir. Sınıf üyeleri sınıfın istenilen yerinde kullanılırlar. Varsayılan değeri (0.0 in this case) public class AdditionApplet extends JApplet { double sum; // sum of values entered by user

33 3.5 İki Sayıyı Eklemek İlkel tip double Method init
Appletteki sınıfının ve üyelerinin ilk değer aldığı metod. Applet çaışırken ilk çağrılan metod. İlk satır herzaman yukardaki gibi gözükür. Geirye birşey döndürmez (void) ve herhangi bir argümanda almaz init metoduna başlangıç double sum; // sum of values entered by user public void init() {

34 3.5 İki Sayıyı Eklemek Değişken deklare etme İki tür değişken vardır
Referans değişkenler (referanslar) Objeyi referans eder. (bellekte konum içerir) Objeler sınıf tanımlamasında tanımlanırlar. Birçok data ve metodlar içerirler. paint metodu Graphics objesinden g isminde referans alır. Bu referans Graphics objesinin metodlarını çağırır. ilkel tipler (değişkenler) Sadce bir tane veriyi tutarlar. String firstNumber; // first string entered by user String secondNumber; // second string entered by user 16 double number1; // first number to add double number2; // second number to add

35 3.5 İki Sayıyı Eklemek Referans ve değişkenler arasındaki farklılıklar
String firstNumber; // first string entered by user String secondNumber; // second string entered by user 16 double number1; // first number to add double number2; // second number to add Referans ve değişkenler arasındaki farklılıklar Eğer veri tipi sınıf ismi ise, bu referanstır. String bir sınıftır. firstNumber, secondNumber Eğer veri tipi ilkel tip ise, bu değişkendir. double bir ilkel tiptir. number1, number2

36 Method JOptionPane.showInputDialog
3.5 İki Sayıyı Eklemek firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); Method JOptionPane.showInputDialog Kullanıcıdan string bir girdi dönderir. Text alan yazılır ardından OK basılır. Eğer yalnış tip girdi ise hata verir. Kullanıcı girişini string olarak geri dönderir. Satır 25-26: Yukardaki aynı işi secondNumber için yapar.

37 3.5 İki Sayıyı Eklemek static method Double.parseDouble
number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber ); static method Double.parseDouble String ifadeyi double çevirir. Static method sentaksını hatırlayın. Classİsmi.methodİsmi( arguments )

38 3.5 İki Sayıyı Eklemek Init metodunun bitişi
appletviewer (veya browser) start metodunu çağırır. start ugenellikle çoklu kanallama (multithreading) de kullanılır. Bölüm 16 dan bakılabilir. Biz bu programımızda onu deklare etmediğimizden kullanılmıyor. Sonra, method paint çağrılır. Method drawRect( x1, y1, width, height ) Sol üst koordinatı (x1, y1) olan ve genişliği (width) ve yüksekliği (height) Satır 45 (15, 10) noktasından başlayıp genişliği 270 pixel , yüksekliği 20 pixel olan bir dikdörtgen çizer. } // end method init g.drawRect( 15, 10, 270, 20 );

39 3.5 İki Sayıyı Eklemek g referansını kullanarak Graphics objesinin drawString metoduna "The sum is" + sum yollanıyor. sum stringe çevrildi. sum paint metodunun içinde tanımlanmadığı halde kullanıldı. Local değişken değil g.drawString( "The sum is " + sum, 25, 25 );

40 Örnek Programlar for while do while

41 WhileCounter.java Line 14 Line 16 Line 18
// Fig. 5.1: WhileCounter.java // Counter-controlled repetition. import java.awt.Graphics; 4 import javax.swing.JApplet; 6 public class WhileCounter extends JApplet { 8 // draw lines on applet’s background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet 13 int counter = 1; // initialization 15 while ( counter <= 10 ) { // repetition condition g.drawLine( 10, 10, 250, counter * 10 ); counter; // increment 19 } // end while 21 } // end method paint 23 24 } // end class WhileCounter WhileCounter.java Line 14 Line 16 Line 18 Control-variable name is counter Control-variable initial value is 1 Condition tests for counter’s final value Increment for counter

42 Condition tests for counter’s final value
// Fig. 5.2: ForCounter.java // Counter-controlled repetition with the for statement. import java.awt.Graphics; 4 import javax.swing.JApplet; 6 public class ForCounter extends JApplet { 8 // draw lines on applet’s background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet 13 // for statement header includes initialization, // repetition condition and increment for ( int counter = 1; counter <= 10; counter++ ) g.drawLine( 10, 10, 250, counter * 10 ); 18 } // end method paint 20 21 } // end class ForCounter ForCounter.java Line 16 int counter = 1; Line 16 counter <= 10; Line 16 counter++; Condition tests for counter’s final value Control-variable name is counter Control-variable initial value is 1 Increment for counter

43 DoWhileTest.java Lines 16-20
// Fig. 5.7: DoWhileTest.java // Using the do...while statement. import java.awt.Graphics; 4 import javax.swing.JApplet; 6 public class DoWhileTest extends JApplet { 8 // draw lines on applet public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet 13 int counter = 1; // initialize counter 15 do { g.drawOval( counter * 10, counter * 10, counter * 20, counter * 20 ); counter; } while ( counter <= 10 ); // end do...while 21 } // end method paint 23 24 } // end class DoWhileTest DoWhileTest.java Lines 16-20 Oval is drawn before testing counter’s final value

44 SwitchTest.java Lines 16-21: Getting user’s input
// Fig. 5.9: SwitchTest.java // Drawing lines, rectangles or ovals based on user input. import java.awt.Graphics; 4 import javax.swing.*; 6 public class SwitchTest extends JApplet { int choice; // user's choice of which shape to draw 9 // initialize applet by obtaining user's choice public void init() { String input; // user's input 14 // obtain user's choice input = JOptionPane.showInputDialog( "Enter 1 to draw lines\n" + "Enter 2 to draw rectangles\n" + "Enter 3 to draw ovals\n" ); 20 choice = Integer.parseInt( input ); // convert input to int 22 } // end method init 24 // draw shapes on applet's background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from JApplet 29 for ( int i = 0; i < 10; i++ ) { // loop 10 times (0-9) 31 SwitchTest.java Lines 16-21: Getting user’s input Get user’s input in JApplet

45 user input (choice) is controlling expression
switch ( choice ) { // determine shape to draw 33 case 1: // draw a line g.drawLine( 10, 10, 250, 10 + i * 10 ); break; // done processing case 37 case 2: // draw a rectangle g.drawRect( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); break; // done processing case 42 case 3: // draw an oval g.drawOval( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); break; // done processing case 47 default: // draw string indicating invalid value entered g.drawString( "Invalid value entered", , 20 + i * 15 ); 51 } // end switch 53 } // end for 55 } // end method paint 57 58 } // end class SwitchTest user input (choice) is controlling expression switch statement determines which case label to execute, depending on controlling expression SwitchTest.java Line 32: controlling expression Line 32: switch statement Line 48 default case for invalid entries

46 SwitchTest.java

47 SwitchTest.java

48 3.6 Java Applet Internet ve World Wide Web Kaynakları
Birçok Java appletlerine ulaşılabilir java.sun.com/applets/ Birçok kaynak kod ve free appletler indirilebilir. Sun site developer.java.sun.com/developer Tartışma oturumları, eğitici slaytlar, tutoriallar, makaleler..vb. Kayıt gerekli Appletler oylanıyor., top 1, 5 ve yüzde 25 Web deki en iyi appletleri bu sayfadan göreblirsiniz.


"Bölüm 3 - Java Appletlerine Giriş" indir ppt

Benzer bir sunumlar


Google Reklamları