|
| 1 | +/* |
| 2 | + * BirIslem Java ile yazilmis ve Genetik Algoritma kullanimini ornekleme |
| 3 | + * amaci guden bir ozgur yazilimdir. |
| 4 | + * Copyright (C) 2007 T. E. KALAYCI (http://kodveus.blogspot.com) |
| 5 | + * |
| 6 | + * BirIslem is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | +package net.tekrei.birislem; |
| 21 | + |
| 22 | +import net.tekrei.birislem.araclar.SayiArac; |
| 23 | + |
| 24 | +import javax.swing.*; |
| 25 | +import javax.swing.text.MaskFormatter; |
| 26 | +import java.awt.*; |
| 27 | + |
| 28 | +/** |
| 29 | + * Bu sinifin temel goreve uygulamayi baslatmak ve ilgili grafiksel arayuz |
| 30 | + * bilesenlerini hazirlayip yuklemek ayrica kullanici etkilesimini saglayarak |
| 31 | + * genetik algoritmanin tetiklenmesi gorevini de gerceklestirmektedir. |
| 32 | + * |
| 33 | + * @author emre |
| 34 | + */ |
| 35 | +public class BirIslemApp extends JFrame { |
| 36 | + |
| 37 | + // Hesaplamada kullanilacak olan sayilar |
| 38 | + private JFormattedTextField txtSayi1; |
| 39 | + private JFormattedTextField txtSayi2; |
| 40 | + private JFormattedTextField txtSayi3; |
| 41 | + private JFormattedTextField txtSayi4; |
| 42 | + private JFormattedTextField txtSayi5; |
| 43 | + private JFormattedTextField txtSayi6; |
| 44 | + // Ulasilmasi gereken hedef sayi |
| 45 | + private JFormattedTextField txtHedefSayi; |
| 46 | + // Cozumun yazilacagi etiket |
| 47 | + private JLabel lblCozum; |
| 48 | + |
| 49 | + /** |
| 50 | + * yapici metot |
| 51 | + */ |
| 52 | + private BirIslemApp() { |
| 53 | + super(); |
| 54 | + initialize(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Uygulamayi baslatan ana metot |
| 59 | + */ |
| 60 | + public static void main(String[] args) { |
| 61 | + new BirIslemApp().setVisible(true); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Grafiksel arayuz bilesenlerini olusturup pencereye ekleme isini yapan |
| 66 | + * metot |
| 67 | + */ |
| 68 | + private void initialize() { |
| 69 | + this.setSize(395, 110); |
| 70 | + this.setTitle("Bir İşlem"); |
| 71 | + this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 72 | + |
| 73 | + this.getContentPane().setLayout(null); |
| 74 | + |
| 75 | + // Sayilar arayuze ekleniyor |
| 76 | + this.getContentPane().add( |
| 77 | + createLabel("SAYILAR:", new Rectangle(0, 0, 100, 25))); |
| 78 | + try { |
| 79 | + // Sadece iki basamak kabul eden bicimlendirici |
| 80 | + MaskFormatter ikiBasamak = new MaskFormatter("##"); |
| 81 | + // Sadece uc basamak kabul eden bicimlendirici |
| 82 | + MaskFormatter ucBasamak = new MaskFormatter("###"); |
| 83 | + |
| 84 | + // Metin alanlari bicimli metin alani olarak yaratiliyor |
| 85 | + // veri giris hatalarini onlemeye calisiyoruz |
| 86 | + txtSayi1 = new JFormattedTextField(ikiBasamak); |
| 87 | + txtSayi1.setBounds(new Rectangle(100, 0, 30, 25)); |
| 88 | + this.getContentPane().add(txtSayi1); |
| 89 | + txtSayi2 = new JFormattedTextField(ikiBasamak); |
| 90 | + txtSayi2.setBounds(new Rectangle(130, 0, 30, 25)); |
| 91 | + this.getContentPane().add(txtSayi2); |
| 92 | + txtSayi3 = new JFormattedTextField(ikiBasamak); |
| 93 | + txtSayi3.setBounds(new Rectangle(160, 0, 30, 25)); |
| 94 | + this.getContentPane().add(txtSayi3); |
| 95 | + txtSayi4 = new JFormattedTextField(ikiBasamak); |
| 96 | + txtSayi4.setBounds(new Rectangle(190, 0, 30, 25)); |
| 97 | + this.getContentPane().add(txtSayi4); |
| 98 | + txtSayi5 = new JFormattedTextField(ikiBasamak); |
| 99 | + txtSayi5.setBounds(new Rectangle(220, 0, 30, 25)); |
| 100 | + this.getContentPane().add(txtSayi5); |
| 101 | + txtSayi6 = new JFormattedTextField(ucBasamak); |
| 102 | + txtSayi6.setBounds(new Rectangle(250, 0, 30, 25)); |
| 103 | + this.getContentPane().add(txtSayi6); |
| 104 | + // Hedef Sayi arayuze ekleniyor |
| 105 | + this.getContentPane().add( |
| 106 | + createLabel("HEDEF SAYI:", new Rectangle(0, 25, 100, 25))); |
| 107 | + txtHedefSayi = new JFormattedTextField(ucBasamak); |
| 108 | + txtHedefSayi.setBounds(100, 25, 30, 25); |
| 109 | + this.getContentPane().add(txtHedefSayi); |
| 110 | + // Dugmeler ekleniyor |
| 111 | + this.getContentPane().add(getBtnRandomSayi()); |
| 112 | + this.getContentPane().add(getBtnRandomHedefSayi()); |
| 113 | + this.getContentPane().add(getBtnHesapla()); |
| 114 | + // Sonuc ekleniyor |
| 115 | + lblCozum = new JLabel(); |
| 116 | + lblCozum.setBounds(0, 50, 300, 25); |
| 117 | + this.getContentPane().add(lblCozum); |
| 118 | + } catch (Exception e) { |
| 119 | + throw new RuntimeException(e); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Bu metodun amaci hedef sayiyi 100-999 arasinda rastgele uretip ilgili |
| 126 | + * metin alanina yazma islemini tetikleyen dugme nesnesini olusturmak |
| 127 | + */ |
| 128 | + private JButton getBtnRandomHedefSayi() { |
| 129 | + JButton btn = new JButton("RASTGELE"); |
| 130 | + // alt+r'de dugmeyi tetikleyecek |
| 131 | + btn.setMnemonic('r'); |
| 132 | + btn.setBounds(130, 25, 125, 25); |
| 133 | + btn.addActionListener(e -> { |
| 134 | + // 100 ile 999 arasinda sayi uretip hedef sayi metin alanina |
| 135 | + // yaziyoruz |
| 136 | + txtHedefSayi.setText("" |
| 137 | + + (SayiArac.getInstance().nextInt(1000) + 100)); |
| 138 | + }); |
| 139 | + return btn; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Bu metodun amaci hesaplamada kullanilacak 6 sayiyi rastgele uretip ilgili |
| 144 | + * metin alanina yazma islemini tetikleyen dugme nesnesini olusturmak |
| 145 | + */ |
| 146 | + private JButton getBtnRandomSayi() { |
| 147 | + JButton btn = new JButton("RASTGELE"); |
| 148 | + // alt+s'de dugmeyi tetikliyor |
| 149 | + btn.setMnemonic('s'); |
| 150 | + btn.setBounds(280, 0, 100, 25); |
| 151 | + btn.addActionListener(e -> rastgeleSayilariUret()); |
| 152 | + return btn; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Bu metot tum veriler girildikten sonra verilere dayanarak genetik |
| 157 | + * hesaplama islemini baslatacak olan dugmeyi yaratip donduren metottur. |
| 158 | + */ |
| 159 | + private JButton getBtnHesapla() { |
| 160 | + JButton btn = new JButton("HESAPLA"); |
| 161 | + // alt+h'de dugmeyi tetikliyor |
| 162 | + btn.setMnemonic('h'); |
| 163 | + btn.setBounds(255, 25, 125, 25); |
| 164 | + btn.addActionListener(e -> hesapla()); |
| 165 | + return btn; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Metin alanindaki sayilari okuyarak genetik hesaplama sinifindaki ilgili |
| 170 | + * metodu cagiran ve hesaplama sonucunu sonuc etiketine yazan metot |
| 171 | + */ |
| 172 | + private void hesapla() { |
| 173 | + // Hesaplama sayilarini okuyalim |
| 174 | + int sayi1 = Integer.parseInt(txtSayi1.getText()); |
| 175 | + int sayi2 = Integer.parseInt(txtSayi2.getText()); |
| 176 | + int sayi3 = Integer.parseInt(txtSayi3.getText()); |
| 177 | + int sayi4 = Integer.parseInt(txtSayi4.getText()); |
| 178 | + int sayi5 = Integer.parseInt(txtSayi5.getText()); |
| 179 | + int sayi6 = Integer.parseInt(txtSayi6.getText()); |
| 180 | + // Hedef sayiyi okuyalim |
| 181 | + int hedefSayi = Integer.parseInt(txtHedefSayi.getText()); |
| 182 | + // Bosu bosuna hesaplama yapmamak icin asagidaki kontrolu yapiyoruz |
| 183 | + if ((sayi1 * sayi2 * sayi3 * sayi4 * sayi5 * sayi6) < hedefSayi) { |
| 184 | + JOptionPane.showMessageDialog(this, |
| 185 | + "Bu sayılarla hedef sayı üretilemez!", "ÜRETİLEMEZ", |
| 186 | + JOptionPane.ERROR_MESSAGE); |
| 187 | + } else { |
| 188 | + // GenetikHesaplama sinifinda elimizdeki sayilara gore |
| 189 | + // hesaplama yapip sonucu cozum etiketine yazalim |
| 190 | + lblCozum.setText("" |
| 191 | + + GenetikHesaplama.getInstance().hesapla( |
| 192 | + new int[]{sayi1, sayi2, sayi3, sayi4, sayi5, |
| 193 | + sayi6}, hedefSayi)); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Hesaplama sayilarini rastgele uretip ilgili metin alanlarina yazan metot |
| 199 | + */ |
| 200 | + private void rastgeleSayilariUret() { |
| 201 | + // Ilk 5 sayi 1 ile 9 arasinda (1 ve 9 dahil) herhangi bir sayi |
| 202 | + // olabilir |
| 203 | + txtSayi1.setText("0" + (SayiArac.getInstance().nextInt(9) + 1)); |
| 204 | + txtSayi2.setText("0" + (SayiArac.getInstance().nextInt(9) + 1)); |
| 205 | + txtSayi3.setText("0" + (SayiArac.getInstance().nextInt(9) + 1)); |
| 206 | + txtSayi4.setText("0" + (SayiArac.getInstance().nextInt(9) + 1)); |
| 207 | + txtSayi5.setText("0" + (SayiArac.getInstance().nextInt(9) + 1)); |
| 208 | + // 6. sayi 10-25-50-75 ve 100 olabilir |
| 209 | + int rnd = SayiArac.getInstance().nextInt(5); |
| 210 | + if (rnd == 0) { |
| 211 | + txtSayi6.setText("010"); |
| 212 | + } else { |
| 213 | + txtSayi6.setText("" + 25 * rnd); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Etiket yaratma islemini kolaylastiran metot |
| 219 | + */ |
| 220 | + private JLabel createLabel(String label, Rectangle bounds) { |
| 221 | + JLabel jlabel = new JLabel(label); |
| 222 | + jlabel.setBounds(bounds); |
| 223 | + return jlabel; |
| 224 | + } |
| 225 | +} |
0 commit comments