24
BSM201 Nesneye Dayalı Programlama 14. Hafta – Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ MÜHENDİSLİK FAKÜLTESİ BİLGİSAYAR MÜHENDİSLİĞİ BÖLÜMÜ

BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

BSM201 Nesneye Dayalı Programlama

14. Hafta – Appletler

Dr. Öğr. Üyesi Nesibe YALÇIN

BARTIN ÜNİVERSİTESİ

MÜHENDİSLİK FAKÜLTESİ

BİLGİSAYAR MÜHENDİSLİĞİ BÖLÜMÜ

Page 2: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

2

Appletler

• HTML etiketleri kullanarak web sayfasına eklenebilen Java programıdır.

• <applet> </applet> • <object> </object>• <embed> </embed>

• Aşağıdaki programların birisiyle çalışır. appletviewer (appletler için test aracı) Web sunucu

• Applet içeren HTML (Hypertext Markup Language) dökümanlarını çalıştırır.

Page 3: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

3

Appletler

• Bir applet oluşturmak için JApplet(swing) veya Applet (Awt) sınıfından extend etmek (türetmek) gerekir.

• import java.applet.Applet;

ya da

• import javax.swing.JApplet;

Page 4: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

4

Applet Oluşturma

import java.applet.Applet;public class sinif_adi extends Applet {

//işlemler

}

import javax.swing.JApplet;public class sinif_adi extends JApplet {

//işlemler

}

Page 5: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

5

Applet Örnekleri

Page 6: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

6

Applet Örnekleri

Page 7: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

7

Applet Metotları

• public void init(): Applet çalışırken ilk çağrılan metottur. Nesneleri oluşturma, ilk değer atamaları için kullanılır.

• public void start(): Her sayfa yüklendiğinde ve yeniden başlatıldığında çağrılır.

• public void stop(): Tarayıcı sayfasından ayrılırken çağrılır.

• public void destroy(): Sistem kaynaklarını(thread) serbest bırakmak için kullanılır.

• public void paint (Graphics): Ekrana herhangi bir şey bastırmak için kullanılır. import java.awt.Graphics;

Page 8: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

8

Applet Metotları

Page 9: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

9

Applet Metotları

• public void repaint(): Bir şeyler değiştirdiğinizde veya ekrandaki değişimleri göstermek istediğinizde çağrılır.

• public void showStatus(String): Uygulama penceresi durumu çubuğunda bilgi görüntülemek için kullanılır.

• public String getParameter(String): HTML dosyasından herhangi bir bilgiyi parametre olarak almak için kullanılır.

• public void actionPerformed(ActionEvent): Applette her olay olduğunda otomatik olarak çağrılır.

Page 10: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

10

Applet Uygulama

import java.awt.Graphics;import java.applet.Applet;

public class uygulama extends Applet {

public void paint(Graphics g) {g.drawString("Günaydın Bartın!", 30, 30);

}}

Java koordinat sistemi• Pixel olarak ölçeklenir.• Üst sol(0,0)

Page 11: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

11

Applet Çalıştırma

Derleme javac uygulama.javaHata yoksa, bytecodelar uygulama.class içinde saklanır.

HTML dosyası yükleme• Dosyayı appletviewer veya browsera yükleme• .htm ya da .html ile bitebilir.

Appleti çalıştırma appletviewer uygulama.html• HTML dosyasında hangi applet varsa o applet çalışır.• .class dosyası çalışır.

Page 12: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

12

HTML Applet Etiketi

<html> <applet code="uygulama.class" width=“250" height=“250"></applet>

</html>

http://ecomputernotes.com/java/awt-and-applets/html-applet-tag

uygulama.html

Page 13: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

13

Applet Çalıştırma

Page 14: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

14

Applet Uygulama2

import java.awt.*;import javax.swing.JApplet;

public class uygulama2 extends JApplet {

public void paint(Graphics g) {g.drawLine(10, 10, 150, 10);g.drawLine(10, 40, 150, 40); g.drawString("Günaydın Bartın!", 40, 25);}

}

g.drawLine(x1, y1, x2, y2 ); (x1, y1)’den (x2, y2)’ye kadar çizgi çizer.

Page 15: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

15

Applet Uygulama2

Page 16: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

16

Applet Uygulama3

import java.applet.Applet; import java.awt.Graphics;

public class Javaapp extends Applet { public void paint(Graphics g) {

g.drawString("Günaydın Bartın!", 40, 25); showStatus(“Bartın Üniversitesi");

} }

Page 17: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

17

Applet Uygulama4

import java.awt.*;import java.applet.Applet;

public class uygulama4 extends Applet {Font f1=new Font("Times New Roman",Font.BOLD,20);Font f2=new Font("Comic Sans",Font.BOLD+Font.ITALIC,24);

public void paint(Graphics g) { g.setFont(f1); //yazı tipig.setColor(Color.red); //metin-çizim rengig.drawString("Günaydın!", 40, 20);g.setFont(f2); g.setColor(Color.blue); //metin-çizim rengig.drawString("Bartın Üniversitesi",20,60);

}}

Page 18: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

18

Applet – Grafik Uygulama

import java.awt.*;import java.applet.Applet;

public class grafik extends Applet {public void paint(Graphics g) {

g.drawLine(10,210,250,210);g.drawLine(20,20,20,220);g.drawString("Ankara", 50, 220); g.drawString("Bartın", 150, 220); g.drawRect(50, 45, 35, 165); g.setColor(Color.green);g.fillRect(150, 110, 35, 100);

}}

Page 19: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

19

Applet - Form Uygulaması

Label

TextField

TextArea

CheckBox

Button

CheckboxGroup

Page 20: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

20

Applet - FormUygulaması

import java.applet.Applet;import java.awt.*;

public class form extends Applet {public void init() {

setBackground(Color.gray); //arkaplan rengisetLayout(new GridLayout(13,2)); //hizalama

Label lbAdSoyad = new Label("Ad Soyad ");Label lbAdres = new Label("Adres ");Label lbCinsiyet = new Label("Cinsiyet ");Label lbHobi = new Label("Hobi ");

Page 21: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

21

Applet - Form Uygulaması

TextField txtAdSoyad = new TextField();

TextArea txtAdres = new TextArea(2, 5);

CheckboxGroup cbCinsiyet = new CheckboxGroup();

Checkbox cbInternet = new Checkbox("İnternet");Checkbox cbMuzik = new Checkbox("Müzik");Checkbox cbSinema = new Checkbox("Sinema");Checkbox cbTiyatro = new Checkbox("Tiyatro");

Button btnYaz = new Button("Yaz");

Page 22: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

22

Applet - Form Uygulamasıadd(lbAdSoyad); add(txtAdSoyad); add(lbAdres); add(txtAdres);add(lbCinsiyet);add(new Checkbox("Erkek", cbCinsiyet, true));add(new Checkbox("Kadin", cbCinsiyet, false));add(lbHobi);add(cbInternet);add(cbMuzik);add(cbSinema);add(cbTiyatro);add(btnYaz);

}}

Page 23: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

23

Applet - Form Uygulamasıimport java.awt.event.*; public class form extends Applet implements ActionListener{

Button btnYaz;public void init() {

……btnYaz = new Button("Yaz");btnYaz.addActionListener(this);……

}public void actionPerformed(ActionEvent Olay) {

if (Olay.getSource() == btnYaz) {………

} }

}

Page 24: BSM201 Nesneye Dayalı Programlama - Nesibe YALÇIN · 2019. 12. 26. · BSM201 Nesneye Dayalı Programlama 14. Hafta –Appletler Dr. Öğr. Üyesi Nesibe YALÇIN BARTIN ÜNİVERSİTESİ

24

Applet - Form Uygulamasıpublic class form extends Applet implements KeyListener{

TextField txtAdSoyad; public void init() {

……txtAdSoyad = new TextField(); txtAdSoyad.addKeyListener(this);…… }

public void keyPressed(KeyEvent ke){if(ke.getSource() == txtAdSoyad){ …… } }

public void keyReleased(KeyEvent ke) {if(ke.getSource() == txtAdSoyad){…… } }

public void keyTyped(KeyEvent ke){if(ke.getSource() == txtAdSoyad){…… } }

}