Wednesday, December 12, 2018

Evaluasi Akhir Semester - PWeb C

Nama : Haikal Almaz Said
NRP   : 05111740000068
Kelas  : Pemrograman Web C


Pada postingan saya kali ini, saya akan membagikan hasil EAS mata kuliah Pemrograman Web saya yaitu membuat system PPDB online. Adapun untuk link web dapat dilihat di http://ppdb.zamlaa.com/.


1. Buat rancangan databasenya.

Disini, saya membuat 2 database yaitu:
  • User sebagai database yang berisi data akun pada website



  • Siswa sebagai database yang berisi data siswa pada website 




2. Buat rancangan interface aplikasi

  • Login

  • Menu Utama

  • Daftar



3. Implementasi dan hostingkan

  • Source Code
    • config.php

1:  <?php   
2:   $server = "ppdb.zamlaa.com";   
3:   $user = "u5480750_admin";   
4:   $password = "djsghs1122";   
5:   $nama_database = "u5480750_siswa";   
6:   $db = mysqli_connect($server, $user, $password, $nama_database);   
7:   if( !$db ){   
8:    die("Gagal terhubung dengan database: " . mysqli_connect_error());   
9:   }   
10:   ?>        

    • index.php

1:   <?php    
2:   session_start();    
3:   if (!isset($_SESSION['username'])) {    
4:     $_SESSION['msg'] = "You must log in first";    
5:     header('location: login.php');    
6:   }    
7:   if (isset($_GET['logout'])) {    
8:     session_destroy();    
9:     unset($_SESSION['username']);    
10:     header("location: login.php");    
11:   }    
12:   ?>   
13:   <!DOCTYPE HTML>   
14:   <html>   
15:   <head>   
16:     <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>   
17:     <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
18:     <script type="text/javascript" src="js/jquery.js"></script>   
19:     <script type="text/javascript" src="js/bootstrap.js"></script>   
20:      <style type="text/css">    
21:    ul{    
22:     display:table; margin:0 auto;    
23:    }    
24:    body {   
25:     text-align: center;   
26:        background: darkred;        
27:    }     
28:   </style>   
29:   </head>   
30:   <body class="p-3 mb-2 text-white">   
31:     <header>   
32:        <h3 align="center">Penerimaan Peserta Didik Baru</h3>   
33:        <h1 align="center">Kota Bekasi</h1>   
34:     </header>   
35:        <br/>  
36:     <div class="p-3 mb-2 bg-secondary text-white">  
37:              <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>   
38:        <h4 align="center">Menu</h4>   
39:        <div class="p-3 mb-2 bg-light text-dark">   
40:          <ul class="list-group">   
41:             <li class="list-group-item"><a href="form-daftar.php"><button class="btn btn-light" type="button">Daftar Baru</button></a></li>   
42:             <li class="list-group-item"><a href="list-siswa.php"><button class="btn btn-light" type="button">Pendaftar</button></a></li>   
43:                            <li class="list-group-item"><a href="logout.php"><button class="btn btn-light" type="button">Logout</button></a></li>   
44:          </ul>   
45:        </div>   
46:     </div>   
47:   </body>   
48:   </html>   

    • login.php

1:   <?php include('server.php') ?>    
2:   <!DOCTYPE html>    
3:   <html>    
4:   <head>    
5:   <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>    
6:   <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
7:   <script type="text/javascript" src="js/jquery.js"></script>   
8:   <script type="text/javascript" src="js/bootstrap.js"></script>   
9:    <style type="text/css">    
10:    ul{    
11:     display:table; margin:0 auto;    
12:    }    
13:    body {   
14:     text-align: center;  
15:        background: darkred;       
16:    }    
17:   </style>   
18:   </head>    
19:   <body class="p-3 mb-2 text-white">   
20:   <header>   
21:    <h3 align="center">Penerimaan Peserta Didik Baru</h3>   
22:    <h1 align="center">Kota Bekasi</h1>   
23:   </header>   
24:   <br/>  
25:   <form method="post" action="login.php">    
26:   <div class="p-3 mb-2 bg-secondary text-white">   
27:    <h4 align="center">Login</h4>   
28:    <div class="p-3 mb-2 bg-light text-dark">   
29:    <div class="form-group">   
30:     <li class="list-group-item">   
31:     <label for="nama">Username</label>   
32:     <input type="text" name="username" class="form-control" id="username" placeholder="Username" />   
33:     </li>   
34:    </div>   
35:     <div class="form-group">     
36:     <li class="list-group-item">   
37:     <label for="password">Password </label>   
38:     <input type="password" name="password" class="form-control" id="password" placeholder="Password"></input>   
39:     </li>   
40:     <br/>   
41:     <input class="btn btn-primary" type="submit" name="login_user" value="Login"/></input> &emsp;   
42:     <p>Belum memiliki akun?    <a href="register.php">Sign up</a></input></p>   
43:     </div>   
44:    </div>   
45:    </div>   
46:   </form>    
47:   </body>    
48:   </html>    

    • register.php

1:   <?php include('server.php') ?>    
2:   <!DOCTYPE html>    
3:   <html>    
4:   <head>    
5:   <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>    
6:   <link rel="stylesheet" type="text/css" href="style.css">  
7:   <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
8:   <script type="text/javascript" src="js/jquery.js"></script>   
9:   <script type="text/javascript" src="js/bootstrap.js"></script>   
10:    <style type="text/css">    
11:    ul{    
12:     display:table; margin:0 auto;    
13:    }    
14:    body {   
15:     text-align: center;    
16:        background-color: darkred;  
17:    }    
18:   </style>   
19:   </head>    
20:   <body class="p-3 mb-2 text-white">    
21:   <form method="post" action="register.php">    
22:     <?php include('errors.php'); ?>    
23:     <header>   
24:    <h3 align="center">Penerimaan Peserta Didik Baru</h3>   
25:    <h1 align="center">Kota Bekasi</h1>   
26:   </header>   
27:   <br/>  
28:   <form method="post" action="login.php">    
29:   <div class="p-3 mb-2 bg-secondary text-white">   
30:    <div class="p-3 mb-2 bg-light text-dark">   
31:    <div class="form-group">   
32:     <li class="list-group-item">   
33:     <label for="username">Username </label>   
34:     <input type="text" name="username" value="<?php echo $username; ?>" class="form-control" id="username" placeholder="Username" />   
35:     </li>   
36:    </div>   
37:     <div class="form-group">     
38:     <li class="list-group-item">   
39:     <label for="email">Email </label>   
40:     <input type="email" name="email" class="form-control" id="email" placeholder="Email" value="<?php echo $email; ?>"></input>   
41:     </li>   
42:     </div>   
43:    <div class="form-group">   
44:     <li class="list-group-item">   
45:     <label for="password1">Password </label>   
46:     <input type="password" name="password_1" placeholder="Please enter your password" class="form-control"/>   
47:     </li>   
48:    </div>   
49:    <div class="form-group">   
50:     <li class="list-group-item">   
51:     <label for="password2">Confirm Password </label>   
52:     <input type="password" name="password_2" placeholder="Please re-enter your password" class="form-control"/>   
53:     </li>   
54:    </div>   
55:    <div class="form-group">    
56:     <button type="submit" class="btn" name="reg_user">Register</button>&emsp;   
57:     <a href="login.php"><input class="btn btn-primary" type="button" value="Sign In"/></a></input>    
58:     </div>    
59:    </div>   
60:    </div>   
61:     <!-- -->   
62:     <!-- <div class="input-group">    
63:     <label>Username</label>    
64:     <input type="text" name="username" value="<?php echo $username; ?>">    
65:     </div>    
66:     <div class="input-group">    
67:     <label>Email</label>    
68:     <input type="email" name="email" value="<?php echo $email; ?>">    
69:     </div>    
70:     <div class="input-group">    
71:     <label>Password</label>    
72:     <input type="password" name="password_1">    
73:     </div>    
74:     <div class="input-group">    
75:     <label>Confirm password</label>    
76:     <input type="password" name="password_2">    
77:     </div>    
78:     <div class="input-group">    
79:     <button type="submit" class="btn" name="reg_user">Register</button>    
80:     </div>    
81:     <p>    
82:      Already a member? <a href="login.php">Sign in</a>    
83:     </p> -->   
84:     <!-- -->   
85:   </form>    
86:   </body>    
87:   </html>    

    • form-daftar.php

1:   <!DOCTYPE HTML>   
2:   <html>   
3:   <head>   
4:     <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>   
5:     <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
6:     <script type="text/javascript" src="js/jquery.js"></script>   
7:     <script type="text/javascript" src="js/bootstrap.js"></script>   
8:     <style type="text/css">   
9:      body {    
10:             text-align: center;  
11:             background: darkred;  
12:            }    
13:     </style>   
14:   </head>   
15:   <body class="p-3 mb-2 text-white">   
16:     <script type="text/javascript">   
17:        function insertData(){   
18:          var name = document.getElementById("nama").value;   
19:          var address = document.getElementById("alamat").value;   
20:          var gender = document.getElementById("jenis_kelamin").value;   
21:          var religion = document.getElementById("agama").value;   
22:          var school1 = document.getElementById("sekolah_asal").value;   
23:          var school2 = document.getElementById("sekolah_pilihan").value;   
24:          if(window.XMLHttpRequest)   
25:          {   
26:             xmlhttp = new XMLHttpRequest();   
27:          }   
28:          else   
29:          {   
30:             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
31:          }   
32:          xmlhttp.onreadystatechange=function(){   
33:             if(this.readyState==4 && this.status==200)   
34:             {   
35:               alert(this.responseText);   
36:               if(this.responseText == "sukses")    
37:               {   
38:                  alert('Data berhasil disimpan');    
39:               }   
40:             }   
41:          }   
42:          xmlhttp.open("GET","proses-pendaftaran.php?name=" + name + "&address=" + address + "&gender=" + gender + "&religion=" + religion + "&school1=" + school1 + "&school2=" + school2, true);   
43:          xmlhttp.send();   
44:        }   
45:     </script>   
46:     <header>   
47:        <h3 align="center">Penerimaan Peserta Didik Baru | Formulir Pendaftaran</h3>   
48:     </header>   
49:            <br/>  
50:     <div class="p-3 mb-2 bg-dark text-white">   
51:     <form action="proses-pendaftaran.php">   
52:     <ul class="list-group">   
53:        <div class="p-3 mb-2 bg-light text-dark">   
54:        <div class="form-group">   
55:          <li class="list-group-item">   
56:             <label for="nama">Nama </label>   
57:             <input type="text" class="form-control" id="nama" placeholder="Nama Lengkap.." />   
58:          </li>   
59:        </div>   
60:          <div class="form-group">           
61:          <li class="list-group-item">   
62:             <label for="alamat">Alamat </label>   
63:             <textarea class="form-control" id="alamat" rows="3"></textarea>   
64:          </li>   
65:        </div>   
66:        <!-- Jenis Kelamin -->   
67:          <div class="form-group">   
68:          <li class="list-group-item">   
69:             <label>Jenis Kelamin </label>   
70:             <select class="form-control" id="jenis_kelamin">   
71:               <option>Laki-laki</option>   
72:               <option>Perempuan</option>   
73:             </select>   
74:          </li>   
75:        </div>   
76:          <!-- Agama -->   
77:          <div class="form-group">   
78:          <li class="list-group-item">   
79:      <label for="agama">Agama </label>   
80:      <select class="form-control" id="agama">   
81:       <option>Islam</option>   
82:       <option>Kristen</option>   
83:       <option>Hindu</option>   
84:       <option>Budha</option>   
85:       <option>Katolik</option>   
86:             <option>Atheis</option>  
87:      </select>   
88:       </li>   
89:     </div>   
90:          <div class="form-group">   
91:       <li class="list-group-item">   
92:      <label for="sekolah_asal">Sekolah Asal </label>   
93:      <input type="text" class="form-control" id="sekolah_asal" placeholder="Nama sekolah asal" />   
94:       </li>   
95:     </div>   
96:     <div class="form-group">   
97:          <li class="list-group-item">   
98:      <label for="sekolah_pilihan">Sekolah Pilihan </label>   
99:      <select class="form-control" id="sekolah_pilihan">   
100:       <option>SMAN 1 Bekasi</option>   
101:       <option>SMAN 2 Bekasi</option>   
102:       <option>SMAN 3 Bekasi</option>   
103:       <option>SMAN 4 Bekasi</option>   
104:       <option>SMAN 5 Bekasi</option>   
105:             <option>SMPN 1 Bekasi</option>   
106:       <option>SMPN 2 Bekasi</option>   
107:       <option>SMPN 3 Bekasi</option>   
108:       <option>SMPN 4 Bekasi</option>   
109:       <option>SMPN 5 Bekasi</option>   
110:      </select>   
111:       </li>   
112:       <br/>   
113:      <input class="btn btn-primary" type="button" value="Daftar" name="Daftar" onclick="insertData()" />   
114:      &emsp;   
115:      <a href="index.php"><input class="btn btn-primary" type="button" value="Menu Utama"/></a></input>   
116:      &emsp;   
117:      <a href="list-siswa.php"><input class="btn btn-primary" type="button" value="Pendaftar"/></a></input>   
118:     </div>   
119:        </ul>   
120:        </div>   
121:     </div>   
122:     </form>   
123:   </div>   
124:   </body>   
125:   </html>   

    • form-edit.php

1:  <?php   
2:   include("config.php");   
3:   // kalau tidak ada id di query string   
4:   if( !isset($_GET['id']) ){   
5:    header('Location: list-siswa.php');   
6:   }   
7:   //ambil id dari query string   
8:   $id = $_GET['id'];   
9:   // untuk mengambil data dari database   
10:   $sql = "SELECT * FROM calon_siswa WHERE id=$id";   
11:   ?>   
12:   <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
13:    <script type="text/javascript" src="js/jquery.js"></script>   
14:    <script type="text/javascript" src="js/bootstrap.js"></script>   
15:   <script type="text/javascript">   
16:     function updateData(){   
17:      var name = document.getElementById("nama").value;   
18:      var address = document.getElementById("alamat").value;   
19:      var gender = document.getElementById("jenis_kelamin").value;   
20:      var religion = document.getElementById("agama").value;   
21:      var school1 = document.getElementById("sekolah_asal").value;   
22:      var school2 = document.getElementById("sekolah_pilihan").value;   
23:      if(window.XMLHttpRequest)   
24:      {   
25:       xmlhttp = new XMLHttpRequest();   
26:      }   
27:      else   
28:      {   
29:       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
30:      }   
31:      xmlhttp.onreadystatechange=function()   
32:      {   
33:       if(this.readyState==4 && this.status==200)   
34:       {   
35:        alert(this.responseText);   
36:        if(this.responseText == "sukses")    
37:        {   
38:         alert('Perubahan berhasil disimpan');    
39:        }   
40:        else {   
41:         alert('Perubahan gagal disimpan');    
42:        }   
43:       }   
44:      }   
45:      xmlhttp.open("GET","proses-edit.php?id=" + <?php echo $id;?> + "&name=" + name + "&address=" + address + "&gender=" + gender + "&religion=" + religion + "&school1=" + school1 + "&school2=" + school2, true);   
46:      xmlhttp.send();   
47:     }   
48:   </script>   
49:   <!DOCTYPE html>   
50:   <html>   
51:   <head>   
52:    <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>   
53:    <style type="text/css">   
54:     body {    
55:     text-align: center;   
56:        background: darkred;        
57:    }    
58:    </style>   
59:   </head>   
60:   <body class="p-3 mb-2 text-white">   
61:    <header>   
62:     <h3 align="center">Penerimaan Peserta Didik Baru | Pembaharuan</h3>   
63:    </header>   
64:    <br/>  
65:     <div class="p-3 mb-2 bg-secondary text-white">   
66:      <form action="proses-edit.php">   
67:      <ul class="list-group">   
68:       <input type="hidden" name="id" value="<?php echo $siswa['id'] ?>" />   
69:       <div class="p-3 mb-2 bg-light text-dark">   
70:       <div class="form-group">   
71:        <li class="list-group-item">   
72:         <label for="nama">Nama </label>   
73:         <input type="text" class="form-control" id="nama" placeholder="Nama Lengkap.." />   
74:        </li>   
75:       </div>   
76:        <div class="form-group">      
77:        <li class="list-group-item">   
78:         <label for="alamat">Alamat </label>   
79:         <textarea class="form-control" id="alamat" rows="3"></textarea>   
80:        </li>   
81:       </div>   
82:       <!-- Jenis Kelamin -->   
83:        <div class="form-group">   
84:        <li class="list-group-item">   
85:         <label for="jenis_kelamin">Jenis Kelamin </label>   
86:         <select class="form-control" id="jenis_kelamin">   
87:          <option>Laki-laki</option>   
88:          <option>Perempuan</option>   
89:         </select>   
90:        </li>   
91:       </div>   
92:        <!-- Agama -->   
93:        <div class="form-group">   
94:        <li class="list-group-item">   
95:        <label for="agama">Agama </label>   
96:        <select class="form-control" id="agama">   
97:         <option>Islam</option>   
98:         <option>Kristen</option>   
99:         <option>Hindu</option>   
100:         <option>Budha</option>   
101:         <option>Katolik</option>   
102:                  <option>Atheis</option>  
103:        </select>   
104:        </li>   
105:       </div>   
106:       <div class="form-group">   
107:       <li class="list-group-item">   
108:        <label for="sekolah_asal">Sekolah Asal </label>   
109:        <input type="text" class="form-control" id="sekolah_asal" placeholder="Nama sekolah asal" />   
110:        </li>   
111:       </div>   
112:       <div class="form-group">   
113:       <li class="list-group-item">   
114:       <label for="sekolah_pilihan">Sekolah Pilihan </label>   
115:       <select class="form-control" id="sekolah_pilihan">   
116:                  <option>SMAN 1 Bekasi</option>   
117:                  <option>SMAN 2 Bekasi</option>   
118:                  <option>SMAN 3 Bekasi</option>   
119:                  <option>SMAN 4 Bekasi</option>   
120:                  <option>SMAN 5 Bekasi</option>   
121:                  <option>SMPN 1 Bekasi</option>   
122:                  <option>SMPN 2 Bekasi</option>   
123:                  <option>SMPN 3 Bekasi</option>   
124:                  <option>SMPN 4 Bekasi</option>   
125:                  <option>SMPN 5 Bekasi</option>    
126:       </select>   
127:       </li>   
128:        <br/>   
129:        <input class="btn btn-secondary" type="button" value="Update" name="update" onclick="updateData()" />   
130:        &emsp;   
131:        <a href="index.php"><input class="btn btn-secondary" type="button" name="home" value="Menu Utama"/></a>   
132:        &emsp;   
133:        <a href="list-siswa.php"><input class="btn btn-primary" type="button" value="Pendaftar"/></a></input>   
134:       </div>   
135:      </ul>   
136:     </div>   
137:    </form>   
138:    </body>   
139:   </html>   

    • list-siswa.php

1:   <?php include("config.php"); ?>   
2:   <!DOCTYPE html>   
3:   <html>   
4:   <head>   
5:    <title>Penerimaan Peserta Didik Baru | Kota Bekasi</title>   
6:    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
7:    <script type="text/javascript" src="js/jquery.js"></script>   
8:    <script type="text/javascript" src="js/bootstrap.js"></script>   
9:    <style type="text/css">   
10:    body {    
11:     text-align: center;  
12:        background: darkred !important;  
13:    }    
14:    </style>   
15:   </head>   
16:   <body class="p-3 mb-2 bg-primary text-white">   
17:    <header>   
18:     <h2 align="center">Penerimaan Peserta Didik Baru | Daftar Siswa</h2>   
19:    </header>   
20:       <br/>  
21:    <div class="p-3 mb-2 bg-secondary text-white">   
22:    <div id='data_siswa'></div>   
23:    </div>   
24:    </body>   
25:   </html>   
26:   <script type="text/javascript">   
27:    function hapus(id)    
28:    {   
29:     if(window.XMLHttpRequest)   
30:     { xmlhttp = new XMLHttpRequest(); }   
31:     else   
32:     { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }   
33:     xmlhttp.onreadystatechange=function(){   
34:     if(this.readyState==4 && this.status==200)   
35:     {   
36:      alert(this.responseText);    
37:      listData();    
38:     }   
39:    }   
40:     xmlhttp.open("GET","proses-hapus.php?id=" + id, true);   
41:     xmlhttp.send();   
42:    }   
43:    function listData()   
44:    {   
45:     if(window.XMLHttpRequest)   
46:     { xmlhttp = new XMLHttpRequest(); }   
47:     else   
48:     { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }   
49:     xmlhttp.onreadystatechange=function()   
50:     {   
51:      if(this.readyState==4 && this.status==200)   
52:      {   
53:       document.getElementById('data_siswa').innerHTML = this.responseText;    
54:      }   
55:     }   
56:     xmlhttp.open("GET","data-list-siswa.php", true);   
57:     xmlhttp.send();   
58:    }   
59:   listData();   
60:   </script>   

    • download.php

1:   <?php    
2:   //export.php    
3:   $connect = mysqli_connect("ppdb.zamlaa.com", "u5480750_admin", "djsghs1122", "u5480750_siswa");    
4:   $output = '';    
5:   if(isset($_POST["download"]))    
6:   {    
7:   $query = "SELECT * FROM calon_siswa";    
8:   $result = mysqli_query($connect, $query);    
9:   if(mysqli_num_rows($result) > 0)    
10:   {    
11:   $output .= '    
12:    <table class="table" bordered="1">    
13:      <tr>    
14:      <th>ID</th>    
15:      <th>Nama</th>    
16:      <th>Alamat</th>    
17:      <th>Jenis_Kelamin</th>    
18:      <th>Agama</th>    
19:      <th>Sekolah_asal</th>    
20:      <th>Sekolah_pilihan</th>    
21:      </tr>    
22:   ';    
23:   while($row = mysqli_fetch_array($result))    
24:   {    
25:    $output .= '    
26:    <tr>    
27:      <td>'.$row["id"].'</td>    
28:      <td>'.$row["nama"].'</td>    
29:      <td>'.$row["alamat"].'</td>    
30:      <td>'.$row["jenis_kelamin"].'</td>    
31:      <td>'.$row["agama"].'</td>    
32:      <td>'.$row["sekolah_asal"].'</td>    
33:      <td>'.$row["sekolah_pilihan"].'</td>    
34:      </tr>    
35:    ';    
36:   }    
37:   $output .= '</table>';    
38:   header('Content-Type: application/xls');    
39:   header('Content-Disposition: attachment; filename=download.xls');    
40:   echo $output;    
41:   }    
42:   }    
43:   ?>   

    • hapus.php

1:   <?php   
2:   include("config.php");   
3:   if( isset($_GET['id']) )   
4:   {   
5:    // mengambil ID dari query   
6:    $id = $_GET['id'];   
7:    // menghapus data dari database   
8:    $sql = "DELETE FROM calon_siswa WHERE id=$id";   
9:    $query = mysqli_query($db, $sql);   
10:    //jika query berhasil   
11:    if( $query )   
12:    {   
13:     alert('Data berhasil dihapus');   
14:     header('Location: list-siswa.php');   
15:    }    
16:    else    
17:    {   
18:     alert('gagal menghapus data');   
19:    }   
20:   }    
21:   else    
22:   {   
23:    alert('akses dilarang...');   
24:   }   
25:   ?>   

    • proses-edit.php

1:   <?php   
2:   include("config.php");   
3:   $id = $_GET['id'];   
4:   $name = $_GET['name'];   
5:   $address = $_GET['address'];   
6:   $gender = $_GET['gender'];   
7:   $religion = $_GET['religion'];   
8:   $school1 = $_GET['school1'];   
9:   $school2 = $_GET['school2'];   
10:    // buat query update   
11:    $sql = "UPDATE calon_siswa SET nama='$name', alamat='$address', jenis_kelamin='$gender', agama='$religion', sekolah_asal='$school1', sekolah_pilihan='$school2' WHERE id=$id";   
12:    $query = mysqli_query($db, $sql);   
13:    //jika query berhasil   
14:    if( $query ) {   
15:     // kalau berhasil alihkan ke halaman list-siswa.php   
16:     echo "Data berhasil diperbaharui";   
17:    }    
18:    else    
19:    {   
20:     //jika gagal   
21:     echo "Data gagal diperbaharui";   
22:    }   
23:   ?>   

    • proses-hapus.php

1:   <?php include("config.php"); ?>   
2:   <?php    
3:     mysqli_query($db, "delete from calon_siswa where id = ".$_GET['id']);    
4:   ?>   

    • proses-pendaftaran.php

1:   <?php   
2:     include("config.php");    
3:     $nama = $_GET['name'];   
4:     $alamat = $_GET['address'];   
5:     $jenis_kelamin = $_GET['gender'];   
6:     $agama = $_GET['religion'];   
7:     $sekolah_asal = $_GET['school1'];   
8:     $sekolah_pilihan = $_GET['school2'];   
9:     $sql = "INSERT INTO calon_siswa (nama, alamat, jenis_kelamin, agama, sekolah_asal, sekolah_pilihan) VALUE('$nama', '$alamat', '$jenis_kelamin', '$agama', '$sekolah_asal','$sekolah_pilihan')";   
10:     $query = mysqli_query($db,$sql);   
11:     if($query)    
12:     {   
13:      // jika berhasil   
14:      echo "Data berhasil dimasukkan.";    
15:     }    
16:     else    
17:     {   
18:      //jika gagal   
19:      echo "Data gagal dimasukkan.";   
20:     }   
21:   ?>   

    • server.php

1:   <?php    
2:   session_start();    
3:   // initializing variables    
4:   $username = "";    
5:   $email = "";    
6:   $errors = array();    
7:   // connect to the database    
8:   $db = mysqli_connect('ppdb.zamlaa.com', 'u5480750_admin', 'djsghs1122', 'u5480750_user');    
9:   // REGISTER USER    
10:   if (isset($_POST['reg_user'])) {    
11:   // receive all input values from the form    
12:   $username = mysqli_real_escape_string($db, $_POST['username']);    
13:   $email = mysqli_real_escape_string($db, $_POST['email']);    
14:   $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);    
15:   $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);    
16:   // form validation: ensure that the form is correctly filled ...    
17:   // by adding (array_push()) corresponding error unto $errors array    
18:   if (empty($username)) { array_push($errors, "Username is required"); }    
19:   if (empty($email)) { array_push($errors, "Email is required"); }    
20:   if (empty($password_1)) { array_push($errors, "Password is required"); }    
21:   if ($password_1 != $password_2) {    
22:    array_push($errors, "The two passwords do not match");    
23:   }    
24:   // first check the database to make sure    
25:   // a user does not already exist with the same username and/or email    
26:   $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";    
27:   $result = mysqli_query($db, $user_check_query);    
28:   $user = mysqli_fetch_assoc($result);    
29:   if ($user) { // if user exists    
30:    if ($user['username'] === $username) {    
31:    array_push($errors, "Username already exists");    
32:    }    
33:    if ($user['email'] === $email) {    
34:    array_push($errors, "email already exists");    
35:    }    
36:   }    
37:   // Finally, register user if there are no errors in the form    
38:   if (count($errors) == 0) {    
39:     $password = md5($password_1);//encrypt the password before saving in the database    
40:     $query = "INSERT INTO users (username, email, password)    
41:       VALUES('$username', '$email', '$password')";    
42:     mysqli_query($db, $query);    
43:     $_SESSION['username'] = $username;    
44:     $_SESSION['success'] = "You are now logged in";    
45:     header('location: index.php');    
46:   }    
47:   }    
48:   // LOGIN USER    
49:   if (isset($_POST['login_user'])) {    
50:   $username = mysqli_real_escape_string($db, $_POST['username']);    
51:   $password = mysqli_real_escape_string($db, $_POST['password']);    
52:   if (empty($username)) {    
53:    array_push($errors, "Username is required");    
54:   }    
55:   if (empty($password)) {    
56:    array_push($errors, "Password is required");    
57:   }    
58:   if (count($errors) == 0) {    
59:    $password = md5($password);    
60:    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";    
61:    $results = mysqli_query($db, $query);    
62:    if (mysqli_num_rows($results) == 1) {    
63:    $_SESSION['username'] = $username;    
64:    $_SESSION['success'] = "You are now logged in";    
65:    header('location: index.php');    
66:    }else {    
67:    array_push($errors, "Wrong username/password combination");    
68:    }    
69:   }    
70:   }    
71:   ?>   

    • data-list-siswa.php

1:  <?php include("config.php"); ?>   
2:   <!DOCTYPE html>   
3:   <html>   
4:   <head>   
5:    <title>List siswa</title>   
6:    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">   
7:    <script type="text/javascript" src="js/jquery.js"></script>   
8:    <script type="text/javascript" src="js/bootstrap.js"></script>   
9:    <style type="text/css">   
10:     body {    
11:    text-align: center;  
12:    }    
13:    </style>   
14:   </head>   
15:   <body>   
16:   <table class="table table-hover table-dark">   
17:    <thead>   
18:     <tr>   
19:      <th scope="col">No</th>   
20:      <th scope="col">Nama</th>   
21:      <th scope="col">Alamat</th>   
22:      <th scope="col">Jenis Kelamin</th>   
23:      <th scope="col">Agama</th>   
24:      <th scope="col">Sekolah Asal</th>   
25:      <th scope="col">Sekolah Pilihan</th>   
26:      <th scope="col">Tindakan</th>   
27:     </tr>   
28:    </thead>   
29:    <tbody>   
30:     <?php   
31:     $sql = "SELECT * FROM calon_siswa";   
32:     $query = mysqli_query($db, $sql);   
33:     $no = 0;   
34:     while($siswa = mysqli_fetch_array($query)){   
35:      echo "<tr>";   
36:      $no++;   
37:      echo "<th scope='row'>".$no."</td>";   
38:      echo "<td>".$siswa['nama']."</td>";   
39:      echo "<td>".$siswa['alamat']."</td>";   
40:      echo "<td>".$siswa['jenis_kelamin']."</td>";   
41:      echo "<td>".$siswa['agama']."</td>";   
42:      echo "<td>".$siswa['sekolah_asal']."</td>";   
43:      echo "<td>".$siswa['sekolah_pilihan']."</td>";   
44:      echo "<td>";   
45:      echo "<a href='form-edit.php?id=".$siswa['id']."'><input class='btn btn-dark' type='button' value='Edit'/></a> &emsp;";   
46:      echo "<input class='btn btn-dark' type='button' onclick=hapus('".$siswa['id']."') value='Hapus'>";   
47:      echo "</td>";   
48:      echo "</tr>";   
49:     }   
50:      echo "<tr>";   
51:      echo "<td colspan='8'>";   
52:      echo "<a href='index.php'><button class='btn btn-dark' type='button'>Menu Utama</button></a> &emsp;";   
53:      echo "<a href='form-daftar.php'><button class='btn btn-dark' type='button'>Tambah Data</button></a>";   
54:      echo "<form method='post' action='download.php'>    
55:    <input type='submit' name='download' class='btn btn-dark value='Download' />";   
56:     ?>   
57:    </tbody>   
58:   </table>   
59:   </body>   
60:   </html>   

    • logout.php

1:  <?php  
2:  session_start();  
3:  session_unset("user");  
4:  header("Location: login.php");  

    • errors.php

1:   <?php if (count($errors) > 0) : ?>    
2:   <div class="error">    
3:     <?php foreach ($errors as $error) : ?>    
4:     <p><?php echo $error ?></p>    
5:     <?php endforeach ?>    
6:   </div>    
7:   <?php endif ?>    



  • Screenshot


(Tampilan login)



(Tampilan Signup user)


(Tampilan menu utama)


(Tampilan formulir pendaftaran)


(Tampilan list data setelah daftar)


(Tampilan setelah hapus)

Monday, December 10, 2018

Evaluasi Akhir Semester PBO A

Nama : Haikal Almaz Said
NRP   : 05111740000068
Kelas  : PBO A


Pada postingan saya kali ini, saya akan membagikan hasil EAS PBO A saya yaitu membuat sebuah Image Viewer dengan berbagai filter dan fitur.

1. Gambarkan rancangan interfacenya.

Untuk interfacenya, ada 3 buah menu yaitu:


  • File yang berisi :
    • Open untuk membuka file image.
    • Close untuk menutup Image yang telah dibuka / reset aplikasi.
    • Save As untuk menyimpan gambar yang telah diedit.
    • Quit untuk keluar dari aplikasi.

  • Filter yang berisi :
    • Filter Darker
    • Filter Lighter
    • Filter Threshold
    • Filter Invert
    • Filter Solarize
    • Filter Smooth
    • Filter Pixelize
    • Filter Mirror
    • Filter Grayscale
    • Filter Edge Detection
    • Filter Fish Eye

  • Help untuk Informasi mengenai program Image Viewer

Selanjutnya ada panel di sebelah kiri yang berisi fitur yang terdapat pada Image Viewer. Adapun untuk fitur-fiturnya yaitu:
  • Smaller untuk memperkecil gambar.
  • Larger untuk memperbesar gambar.
  • Crop untuk memotong gambar.
  • Add Text untuk menambahkan teks pada gambar.
Kemudian untuk gambar akan ditampilkan disebelah kanan panel fitur dan nama file akan ditampilkan diatas panel fitur dengan status di bagian bawah kiri aplikasi.


2. Gambarkan dan jelaskan Class Diagram penyusun dari image viewer yang akan dibuat.


Disini, saya membuat 16 Class yang terdiri dari:
  • ImageViewer sebagai main class dari program Image Viewer tersebut sekaligus berisi fitur Smaller, Larger, Crop, dan Add Text.
  • OFImage sebagai class yang mendefinisikan sebuah gambar dalam Object First format.
  • ImageFileManager sebagai utility class untuk membuka dan menyimpan gambar.
  • ImagePanel sebagai class yang berisi komponen Swing dan menampilkan OFImage. 
  • Filter sebagai superclass abstrak untuk semua filter dalam Image Viewer.
  • SolarizeFilter class untuk memberi efek solarize pada gambar.
  • InvertFilter class untuk memberi efek invert pada gambar.
  • MirrorFilter class untuk memberi efek mirror/membalik gambar secara horizontal.
  • EdgeFilter class untuk mendeteksi edge dan memberi efek warna seperti pensil warna.
  • FishEyeFilter class untuk memberi efek Fish Eye pada gambar.
  • LighterFilter class untuk memberi efek terang pada gambar.
  • SmoothFilter class untuk melembutkan gambar seperti soft lens.
  • PixelizeFilter class untuk memberi efek pixelisasi pada gambar.
  • GrayScaleFilter class untuk memberi efek hitam-putih pada gambar.
  • ThresholdFilter class untuk memberi efek threshold pada gambar.
  • DarkerFilter class untuk memberi efek gelap pada gambar.

3. Implementasikan ke dalam program.

Berikut Source Code beserta Screenshot pemakaiannya.

  • Source Code
    • ImageViewer

1:  import java.awt.*;  
2:  import java.awt.event.*;  
3:  import java.awt.image.*;  
4:  import javax.swing.*;  
5:  import javax.swing.border.*;  
6:  import javax.swing.JOptionPane;  
7:  import javax.swing.JFrame;  
8:  import java.io.File;  
9:  import java.util.List;  
10:  import java.util.ArrayList;  
11:  import java.util.Iterator;  
12:  /**  
13:   * ImageViewer is the main class of the image viewer application. It builds and  
14:   * displays the application GUI and initialises all other components.  
15:   *   
16:   * To start the application, create an object of this class.  
17:   *   
18:   * @author Haikal Almaz Said  
19:   * @version 12/10/2018  
20:   */  
21:  public class ImageViewer  
22:  {  
23:    // static fields:  
24:    private static final String VERSION = "Version 3.1";  
25:    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"))     ;  
26:    // fields:  
27:    private JFrame frame;  
28:    private ImagePanel imagePanel;  
29:    private JLabel filenameLabel;  
30:    private JLabel statusLabel;  
31:    private JButton smallerButton;  
32:    private JButton largerButton;  
33:    private JButton textButton;  
34:    private JButton cropButton;  
35:    private OFImage currentImage;  
36:    private List<Filter> filters;  
37:    /**  
38:     * Create an ImageViewer and display its GUI on screen.  
39:     */  
40:    public ImageViewer()  
41:    {  
42:      currentImage = null;  
43:      filters = createFilters();  
44:      makeFrame();  
45:    }  
46:    // ---- implementation of menu functions ----  
47:    /**  
48:     * Open function: open a file chooser to select a new image file,  
49:     * and then display the chosen image.  
50:     */  
51:    private void openFile()  
52:    {  
53:      int returnVal = fileChooser.showOpenDialog(frame);  
54:      if(returnVal != JFileChooser.APPROVE_OPTION) {  
55:        return; // cancelled  
56:      }  
57:      File selectedFile = fileChooser.getSelectedFile();  
58:      currentImage = ImageFileManager.loadImage(selectedFile);  
59:      if(currentImage == null) {  // image file was not a valid image  
60:        JOptionPane.showMessageDialog(frame,  
61:            "The file was not in a recognized image file format.",  
62:            "Image Load Error",  
63:            JOptionPane.ERROR_MESSAGE);  
64:        return;  
65:      }  
66:      imagePanel.setImage(currentImage);  
67:      setButtonsEnabled(true);  
68:      showFilename(selectedFile.getPath());  
69:      showStatus("File loaded.");  
70:      frame.pack();  
71:    }  
72:    /**  
73:     * Close function: close the current image.  
74:     */  
75:    private void close()  
76:    {  
77:      currentImage = null;  
78:      imagePanel.clearImage();  
79:      showFilename(null);  
80:      setButtonsEnabled(false);  
81:    }  
82:    /**  
83:     * Save As function: save the current image to a file.  
84:     */  
85:    private void saveAs()  
86:    {  
87:      if(currentImage != null) {  
88:        int returnVal = fileChooser.showSaveDialog(frame);  
89:        if(returnVal != JFileChooser.APPROVE_OPTION) {  
90:          return; // cancelled  
91:        }  
92:        File selectedFile = fileChooser.getSelectedFile();  
93:        ImageFileManager.saveImage(currentImage, selectedFile);  
94:        showFilename(selectedFile.getPath());  
95:      }  
96:    }  
97:    /**  
98:     * Quit function: quit the application.  
99:     */  
100:    private void quit()  
101:    {  
102:      System.exit(0);  
103:    }  
104:    /**  
105:     * Apply a given filter to the current image.  
106:     *   
107:     * @param filter  The filter object to be applied.  
108:     */  
109:    private void applyFilter(Filter filter)  
110:    {  
111:      if(currentImage != null) {  
112:        filter.apply(currentImage);  
113:        frame.repaint();  
114:        showStatus("Applied: " + filter.getName());  
115:      }  
116:      else {  
117:        showStatus("No image loaded.");  
118:      }  
119:    }  
120:    /**  
121:     * 'About' function: show the 'about' box.  
122:     */  
123:    private void showAbout()  
124:    {  
125:      JOptionPane.showMessageDialog(frame,   
126:            "ImageViewer\n" + VERSION,  
127:            "About ImageViewer",   
128:            JOptionPane.INFORMATION_MESSAGE);  
129:    }  
130:    /**  
131:     * Make the current picture larger.  
132:     */  
133:    private void makeLarger()  
134:    {  
135:      if(currentImage != null) {  
136:        // create new image with double size  
137:        int width = currentImage.getWidth();  
138:        int height = currentImage.getHeight();  
139:        OFImage newImage = new OFImage(width * 2, height * 2);  
140:        // copy pixel data into new image  
141:        for(int y = 0; y < height; y++) {  
142:          for(int x = 0; x < width; x++) {  
143:            Color col = currentImage.getPixel(x, y);  
144:            newImage.setPixel(x * 2, y * 2, col);  
145:            newImage.setPixel(x * 2 + 1, y * 2, col);  
146:            newImage.setPixel(x * 2, y * 2 + 1, col);  
147:            newImage.setPixel(x * 2+1, y * 2 + 1, col);  
148:          }  
149:        }  
150:        currentImage = newImage;  
151:        imagePanel.setImage(currentImage);  
152:        frame.pack();  
153:      }  
154:    }  
155:    /**  
156:     * Make the current picture smaller.  
157:     */  
158:    private void makeSmaller()  
159:    {  
160:      if(currentImage != null) {  
161:        // create new image with double size  
162:        int width = currentImage.getWidth() / 2;  
163:        int height = currentImage.getHeight() / 2;  
164:        OFImage newImage = new OFImage(width, height);  
165:        // copy pixel data into new image  
166:        for(int y = 0; y < height; y++) {  
167:          for(int x = 0; x < width; x++) {  
168:            newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));  
169:          }  
170:        }  
171:        currentImage = newImage;  
172:        imagePanel.setImage(currentImage);  
173:        frame.pack();  
174:      }  
175:    }  
176:    private void crop()  
177:    {  
178:      if(currentImage != null) {  
179:        int x1,x2,y1,y2;  
180:        x1 = Integer.parseInt(JOptionPane.showInputDialog("X1 Coordinate"));  
181:        y1 = Integer.parseInt(JOptionPane.showInputDialog("Y1 Coordinate"));  
182:        x2 = Integer.parseInt(JOptionPane.showInputDialog("X2 Coordinate"));  
183:        y2 = Integer.parseInt(JOptionPane.showInputDialog("Y2 Coordinate"));  
184:        int width = x2-x1;  
185:        int height = y2-y1;  
186:        OFImage newImage = new OFImage(width, height);  
187:        // copy pixel data into new image  
188:        for(int y = 0; y < height; y++) {  
189:          for(int x = 0; x < width; x++) {  
190:            newImage.setPixel(x, y, currentImage.getPixel(x+x1, y+y1));  
191:          }  
192:        }  
193:        currentImage = newImage;  
194:        imagePanel.setImage(currentImage);  
195:        frame.pack();  
196:      }  
197:    }  
198:    private void makeText()  
199:    {  
200:      JTextField xField = new JTextField(5);  
201:      JTextField yField = new JTextField(5);  
202:      JTextField zField = new JTextField(5);  
203:      JPanel myPanel = new JPanel();  
204:      myPanel.add(new JLabel("R"));  
205:      myPanel.add(xField);  
206:      myPanel.add(Box.createVerticalStrut(15)); // a spacer  
207:      myPanel.add(new JLabel("G"));  
208:      myPanel.add(yField);  
209:      myPanel.add(Box.createVerticalStrut(15)); // a spacer  
210:      myPanel.add(new JLabel("B"));  
211:      myPanel.add(zField);  
212:      if(currentImage != null) {  
213:        int width = currentImage.getWidth();  
214:        int height = currentImage.getHeight();  
215:        int xPosition = Integer.parseInt(JOptionPane.showInputDialog("Pixel Position X"));  
216:        int yPosition = Integer.parseInt(JOptionPane.showInputDialog("Pixel Position Y"));  
217:        float fontSize = Float.parseFloat(JOptionPane.showInputDialog("Font Size"));  
218:        String addText = JOptionPane.showInputDialog("Write Something..");  
219:        int result = JOptionPane.showConfirmDialog(null, myPanel, "Font Color", JOptionPane.O   K_CANCEL_OPTION);  
220:        OFImage newImage = new OFImage(width, height);  
221:        // copy pixel data into new image  
222:        for(int y = 0; y < height; y++) {  
223:          for(int x = 0; x < width; x++) {  
224:            Color col = currentImage.getPixel(x, y);  
225:            newImage.setPixel(x, y, col);  
226:          }  
227:        }  
228:        int r = Integer.parseInt(xField.getText());  
229:        int gr = Integer.parseInt(yField.getText());  
230:        int b = Integer.parseInt(zField.getText());  
231:        Color c = new Color(r,gr,b);  
232:        Graphics g = newImage.getGraphics();  
233:        g.setFont(g.getFont().deriveFont(fontSize));  
234:        g.setColor(c);  
235:        g.drawString(addText, xPosition, yPosition);  
236:        g.dispose();  
237:        currentImage = newImage;  
238:        imagePanel.setImage(currentImage);  
239:      }  
240:    }  
241:    // ---- support methods ----  
242:    /**  
243:     * Show the file name of the current image in the fils display label.  
244:     * 'null' may be used as a parameter if no file is currently loaded.  
245:     *   
246:     * @param filename The file name to be displayed, or null for 'no file'.  
247:     */  
248:    private void showFilename(String filename)  
249:    {  
250:      if(filename == null) {  
251:        filenameLabel.setText("No file displayed.");  
252:      }  
253:      else {  
254:        filenameLabel.setText("File: " + filename);  
255:      }  
256:    }  
257:    /**  
258:     * Show a message in the status bar at the bottom of the screen.  
259:     * @param text The status message.  
260:     */  
261:    private void showStatus(String text)  
262:    {  
263:      statusLabel.setText(text);  
264:    }  
265:    /**  
266:     * Enable or disable all toolbar buttons.  
267:     *   
268:     * @param status 'true' to enable the buttons, 'false' to disable.  
269:     */  
270:    private void setButtonsEnabled(boolean status)  
271:    {  
272:      smallerButton.setEnabled(status);  
273:      largerButton.setEnabled(status);  
274:      cropButton.setEnabled(status);  
275:    }  
276:    /**  
277:     * Create a list with all the known filters.  
278:     * @return The list of filters.  
279:     */  
280:    private List<Filter> createFilters()  
281:    {  
282:      List<Filter> filterList = new ArrayList<Filter>();  
283:      filterList.add(new DarkerFilter("Darker"));  
284:      filterList.add(new LighterFilter("Lighter"));  
285:      filterList.add(new ThresholdFilter("Threshold"));  
286:      filterList.add(new InvertFilter("Invert"));  
287:      filterList.add(new SolarizeFilter("Solarize"));  
288:      filterList.add(new SmoothFilter("Smooth"));  
289:      filterList.add(new PixelizeFilter("Pixelize"));  
290:      filterList.add(new MirrorFilter("Mirror"));  
291:      filterList.add(new GrayScaleFilter("Grayscale"));  
292:      filterList.add(new EdgeFilter("Edge Detection"));  
293:      filterList.add(new FishEyeFilter("Fish Eye"));  
294:      return filterList;  
295:    }  
296:    // ---- Swing stuff to build the frame and all its components and menus ----  
297:    /**  
298:     * Create the Swing frame and its content.  
299:     */  
300:    private void makeFrame()  
301:    {  
302:      frame = new JFrame("ImageViewer");  
303:      JPanel contentPane = (JPanel)frame.getContentPane();  
304:      contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));  
305:      makeMenuBar(frame);  
306:      // Specify the layout manager with nice spacing  
307:      contentPane.setLayout(new BorderLayout(6, 6));  
308:      // Create the image pane in the center  
309:      imagePanel = new ImagePanel();  
310:      imagePanel.setBorder(new EtchedBorder());  
311:      contentPane.add(imagePanel, BorderLayout.CENTER);  
312:      // Create two labels at top and bottom for the file name and status messages  
313:      filenameLabel = new JLabel();  
314:      contentPane.add(filenameLabel, BorderLayout.NORTH);  
315:      statusLabel = new JLabel(VERSION);  
316:      contentPane.add(statusLabel, BorderLayout.SOUTH);  
317:      // Create the toolbar with the buttons  
318:      JPanel toolbar = new JPanel();  
319:      toolbar.setLayout(new GridLayout(0, 1));  
320:      smallerButton = new JButton("Smaller");  
321:      smallerButton.addActionListener(new ActionListener() {  
322:                  public void actionPerformed(ActionEvent e) { makeSmaller(); }  
323:                });  
324:      toolbar.add(smallerButton);  
325:      largerButton = new JButton("Larger");  
326:      largerButton.addActionListener(new ActionListener() {  
327:                  public void actionPerformed(ActionEvent e) { makeLarger(); }  
328:                });  
329:      toolbar.add(largerButton);  
330:      cropButton = new JButton("Crop");  
331:      cropButton.addActionListener(new ActionListener() {  
332:                  public void actionPerformed(ActionEvent e) { crop(); }  
333:                });  
334:      toolbar.add(cropButton);  
335:      textButton = new JButton("Add Text");  
336:      textButton.addActionListener(new ActionListener() {  
337:                  public void actionPerformed(ActionEvent e) { makeText();}  
338:                });  
339:                toolbar.add(textButton);  
340:      // Add toolbar into panel with flow layout for spacing  
341:      JPanel flow = new JPanel();  
342:      flow.add(toolbar);  
343:      contentPane.add(flow, BorderLayout.WEST);  
344:      // building is done - arrange the components     
345:      showFilename(null);  
346:      setButtonsEnabled(false);  
347:      frame.pack();  
348:      // place the frame at the center of the screen and show  
349:      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
350:      frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);  
351:      frame.setVisible(true);  
352:    }  
353:    /**  
354:     * Create the main frame's menu bar.  
355:     *   
356:     * @param frame  The frame that the menu bar should be added to.  
357:     */  
358:    private void makeMenuBar(JFrame frame)  
359:    {  
360:      final int SHORTCUT_MASK =  
361:        Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
362:      JMenuBar menubar = new JMenuBar();  
363:      frame.setJMenuBar(menubar);  
364:      JMenu menu;  
365:      JMenuItem item;  
366:      // create the File menu  
367:      menu = new JMenu("File");  
368:      menubar.add(menu);  
369:      item = new JMenuItem("Open...");  
370:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));  
371:        item.addActionListener(new ActionListener() {  
372:                  public void actionPerformed(ActionEvent e) { openFile(); }  
373:                });  
374:      menu.add(item);  
375:      item = new JMenuItem("Close");  
376:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));  
377:        item.addActionListener(new ActionListener() {  
378:                  public void actionPerformed(ActionEvent e) { close(); }  
379:                });  
380:      menu.add(item);  
381:      menu.addSeparator();  
382:      item = new JMenuItem("Save As...");  
383:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));  
384:        item.addActionListener(new ActionListener() {  
385:                  public void actionPerformed(ActionEvent e) { saveAs(); }  
386:                });  
387:      menu.add(item);  
388:      menu.addSeparator();  
389:      item = new JMenuItem("Quit");  
390:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));  
391:        item.addActionListener(new ActionListener() {  
392:                  public void actionPerformed(ActionEvent e) { quit(); }  
393:                });  
394:      menu.add(item);  
395:      // create the Filter menu  
396:      menu = new JMenu("Filter");  
397:      menubar.add(menu);  
398:      for(final Filter filter : filters) {  
399:        item = new JMenuItem(filter.getName());  
400:        item.addActionListener(new ActionListener() {  
401:                  public void actionPerformed(ActionEvent e) {   
402:                    applyFilter(filter);  
403:                  }  
404:                });  
405:         menu.add(item);  
406:       }  
407:      // create the Help menu  
408:      menu = new JMenu("Help");  
409:      menubar.add(menu);  
410:      item = new JMenuItem("About ImageViewer...");  
411:        item.addActionListener(new ActionListener() {  
412:                  public void actionPerformed(ActionEvent e) { showAbout(); }  
413:                });  
414:      menu.add(item);  
415:    }  
416:  }  


    • OFImage

1:  import java.awt.*;  
2:  import java.awt.image.*;  
3:  import javax.swing.*;  
4:  /**  
5:   * OFImage is a class that defines an image in OF (Objects First) format.  
6:   *   
7:   * @author Haikal Almaz Said  
8:   * @version 12/10/2018  
9:   */  
10:  public class OFImage extends BufferedImage  
11:  {  
12:    /**  
13:     * Create an OFImage copied from a BufferedImage.  
14:     * @param image The image to copy.  
15:     */  
16:    public OFImage(BufferedImage image)  
17:    {  
18:       super(image.getColorModel(), image.copyData(null),   
19:          image.isAlphaPremultiplied(), null);  
20:    }  
21:    /**  
22:     * Create an OFImage with specified size and unspecified content.  
23:     * @param width The width of the image.  
24:     * @param height The height of the image.  
25:     */  
26:    public OFImage(int width, int height)  
27:    {  
28:      super(width, height, TYPE_INT_RGB);  
29:    }  
30:    /**  
31:     * Set a given pixel of this image to a specified color. The  
32:     * color is represented as an (r,g,b) value.  
33:     * @param x The x position of the pixel.  
34:     * @param y The y position of the pixel.  
35:     * @param col The color of the pixel.  
36:     */  
37:    public void setPixel(int x, int y, Color col)  
38:    {  
39:      int pixel = col.getRGB();  
40:      setRGB(x, y, pixel);  
41:    }  
42:    /**  
43:     * Get the color value at a specified pixel position.  
44:     * @param x The x position of the pixel.  
45:     * @param y The y position of the pixel.  
46:     * @return The color of the pixel at the given position.  
47:     */  
48:    public Color getPixel(int x, int y)  
49:    {  
50:      int pixel = getRGB(x, y);  
51:      return new Color(pixel);  
52:    }  
53:  }  


    • ImageFileManager

1:  import java.awt.image.*;  
2:  import javax.imageio.*;  
3:  import java.io.*;  
4:  /**  
5:   * ImageFileManager is a small utility class with static methods to load  
6:   * and save images.  
7:   *   
8:   * The files on disk can be in JPG or PNG image format. For files written  
9:   * by this class, the format is determined by the constant IMAGE_FORMAT.  
10:   *   
11:   * @author Haikal Almaz Said  
12:   * @version 12/10/2018  
13:   */  
14:  public class ImageFileManager  
15:  {  
16:    // A constant for the image format that this writer uses for writing.  
17:    // Available formats are "jpg" and "png".  
18:    private static final String IMAGE_FORMAT = "jpg";  
19:    /**  
20:     * Read an image file from disk and return it as an image. This method  
21:     * can read JPG and PNG file formats. In case of any problem (e.g the file   
22:     * does not exist, is in an undecodable format, or any other read error)   
23:     * this method returns null.  
24:     *   
25:     * @param imageFile The image file to be loaded.  
26:     * @return      The image object or null is it could not be read.  
27:     */  
28:    public static OFImage loadImage(File imageFile)  
29:    {  
30:      try {  
31:        BufferedImage image = ImageIO.read(imageFile);  
32:        if(image == null || (image.getWidth(null) < 0)) {  
33:          // we could not load the image - probably invalid file format  
34:          return null;  
35:        }  
36:        return new OFImage(image);  
37:      }  
38:      catch(IOException exc) {  
39:        return null;  
40:      }  
41:    }  
42:    /**  
43:     * Write an image file to disk. The file format is JPG. In case of any   
44:     * problem the method just silently returns.  
45:     *   
46:     * @param image The image to be saved.  
47:     * @param file  The file to save to.  
48:     */  
49:    public static void saveImage(OFImage image, File file)  
50:    {  
51:      try {  
52:        ImageIO.write(image, IMAGE_FORMAT, file);  
53:      }  
54:      catch(IOException exc) {  
55:        return;  
56:      }  
57:    }  
58:  }  


    • ImagePanel

1:  import java.awt.*;  
2:  import javax.swing.*;  
3:  import java.awt.image.*;  
4:  /**  
5:   * An ImagePanel is a Swing component that can display an OFImage.  
6:   * It is constructed as a subclass of JComponent with the added functionality  
7:   * of setting an OFImage that will be displayed on the surface of this  
8:   * component.  
9:   *   
10:   * @author Haikal Almaz Said  
11:   * @version 12/10/2018  
12:   */  
13:  public class ImagePanel extends JComponent  
14:  {  
15:    // The current width and height of this panel  
16:    private int width, height;  
17:    // An internal image buffer that is used for painting. For  
18:    // actual display, this image buffer is then copied to screen.  
19:    private OFImage panelImage;  
20:    /**  
21:     * Create a new, empty ImagePanel.  
22:     */  
23:    public ImagePanel()  
24:    {  
25:      width = 360;  // arbitrary size for empty panel  
26:      height = 240;  
27:      panelImage = null;  
28:    }  
29:    /**  
30:     * Set the image that this panel should show.  
31:     *   
32:     * @param image The image to be displayed.  
33:     */  
34:    public void setImage(OFImage image)  
35:    {  
36:      if(image != null) {  
37:        width = image.getWidth();  
38:        height = image.getHeight();  
39:        panelImage = image;  
40:        repaint();  
41:      }  
42:    }  
43:    /**  
44:     * Clear the image on this panel.  
45:     */  
46:    public void clearImage()  
47:    {  
48:      Graphics imageGraphics = panelImage.getGraphics();  
49:      imageGraphics.setColor(Color.LIGHT_GRAY);  
50:      imageGraphics.fillRect(0, 0, width, height);  
51:      repaint();  
52:    }  
53:    // The following methods are redefinitions of methods  
54:    // inherited from superclasses.  
55:    /**  
56:     * Tell the layout manager how big we would like to be.  
57:     * (This method gets called by layout managers for placing  
58:     * the components.)  
59:     *   
60:     * @return The preferred dimension for this component.  
61:     */  
62:    public Dimension getPreferredSize()  
63:    {  
64:      return new Dimension(width, height);  
65:    }  
66:    /**  
67:     * This component needs to be redisplayed. Copy the internal image   
68:     * to screen. (This method gets called by the Swing screen painter   
69:     * every time it want this component displayed.)  
70:     *   
71:     * @param g The graphics context that can be used to draw on this component.  
72:     */  
73:    public void paintComponent(Graphics g)  
74:    {  
75:      Dimension size = getSize();  
76:      g.clearRect(0, 0, size.width, size.height);  
77:      if(panelImage != null) {  
78:        g.drawImage(panelImage, 0, 0, null);  
79:      }  
80:    }  
81:  }  


    • Filter

1:  /**  
2:   * Filter is an abstract superclass for all image filters in this  
3:   * application. Filters can be applied to OFImages by invoking the apply   
4:   * method.  
5:   *   
6:   * @author Haikal Almaz Said  
7:   * @version 12/10/2018  
8:   */  
9:  public abstract class Filter  
10:  {  
11:    private String name;  
12:    /**  
13:     * Create a new filter with a given name.  
14:     * @param name The name of the filter.  
15:     */  
16:    public Filter(String name)  
17:    {  
18:      this.name = name;  
19:    }  
20:    /**  
21:     * Return the name of this filter.  
22:     *   
23:     * @return The name of this filter.  
24:     */  
25:    public String getName()  
26:    {  
27:      return name;  
28:    }  
29:    /**  
30:     * Apply this filter to an image.  
31:     *   
32:     * @param image The image to be changed by this filter.  
33:     */  
34:    public abstract void apply(OFImage image);  
35:  }  


    • SolarizeFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to create a solarization effect.  
4:   *   
5:   * @author Haikal Almaz Said  
6:   * @version 12/10/2018  
7:   */  
8:  public class SolarizeFilter extends Filter  
9:  {  
10:    /**  
11:     * Constructor for objects of class Solarize.  
12:     * @param name The name of the filter.  
13:     */  
14:    public SolarizeFilter(String name)  
15:    {  
16:      super(name);  
17:    }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width; x++) {  
29:          Color pix = image.getPixel(x, y);  
30:          int red = pix.getRed();  
31:          if(red <= 127) {  
32:            red = 255 - red;  
33:          }  
34:          int green = pix.getGreen();  
35:          if(green <= 127) {  
36:            green = 255 - green;  
37:          }  
38:          int blue = pix.getBlue();  
39:          if(blue <= 127) {  
40:            blue = 255 - blue;  
41:          }  
42:          image.setPixel(x, y, new Color(red, green, blue));  
43:        }  
44:      }  
45:    }  
46:  }  


    • InvertFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to invert colors.  
4:   *   
5:   * @author Haikal Almaz Said  
6:   * @version 12/10/2018  
7:   */  
8:  public class InvertFilter extends Filter  
9:  {  
10:    /**  
11:     * Constructor for objects of class InvertFilter.  
12:     * @param name The name of the filter.  
13:     */  
14:    public InvertFilter(String name)  
15:    {  
16:      super(name);  
17:    }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width; x++) {  
29:          Color pix = image.getPixel(x, y);  
30:          image.setPixel(x, y, new Color(255 - pix.getRed(),  
31:                          255 - pix.getGreen(),  
32:                          255 - pix.getBlue()));  
33:        }  
34:      }  
35:    }  
36:  }  


    • MirrorFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to mirror (flip) the image horizontally.  
4:   *   
5:   * @author Haikal Almaz Said  
6:   * @version 12/10/2018  
7:   */  
8:  public class MirrorFilter extends Filter  
9:  {  
10:       /**  
11:        * Constructor for objects of class MirrorFilter.  
12:     * @param name The name of the filter.  
13:        */  
14:       public MirrorFilter(String name)  
15:    {  
16:      super(name);  
17:       }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width / 2; x++) {  
29:          Color left = image.getPixel(x, y);  
30:          image.setPixel(x, y, image.getPixel(width - 1 - x, y));  
31:          image.setPixel(width - 1 - x, y, left);  
32:        }  
33:      }  
34:    }  
35:  }  


    • EdgeFilter

1:  import java.awt.Color;  
2:  import java.util.List;  
3:  import java.util.ArrayList;  
4:  /**  
5:   * An image filter to detect edges and highlight them, a bit like   
6:   * a colored pencil drawing.  
7:   *   
8:   * @author Haikal Almaz Said  
9:   * @version 12/10/2018  
10:   */  
11:  public class EdgeFilter extends Filter  
12:  {  
13:    private static final int TOLERANCE = 20;  
14:    private OFImage original;  
15:    private int width;  
16:    private int height;  
17:    /**  
18:     * Constructor for objects of class EdgeFilter.  
19:     * @param name The name of the filter.  
20:     */  
21:    public EdgeFilter(String name)  
22:    {  
23:      super(name);  
24:    }  
25:    /**  
26:     * Apply this filter to an image.  
27:     *   
28:     * @param image The image to be changed by this filter.  
29:     */  
30:    public void apply(OFImage image)  
31:    {  
32:      original = new OFImage(image);  
33:      width = original.getWidth();  
34:      height = original.getHeight();  
35:      for(int y = 0; y < height; y++) {  
36:        for(int x = 0; x < width; x++) {  
37:          image.setPixel(x, y, edge(x, y));  
38:        }  
39:      }  
40:    }  
41:    /**  
42:     * Return a new color that is the smoothed color of a given  
43:     * position. The "smoothed color" is the color value that is the  
44:     * average of this pixel and all the adjacent pixels.  
45:     * @param xpos The x position of the pixel.  
46:     * @param ypos The y position of the pixel.  
47:     * @return The smoothed color.  
48:     */  
49:    private Color edge(int xpos, int ypos)  
50:    {  
51:      List<Color> pixels = new ArrayList<Color>(9);  
52:      for(int y = ypos-1; y <= ypos+1; y++) {  
53:        for(int x = xpos-1; x <= xpos+1; x++) {  
54:          if( x >= 0 && x < width && y >= 0 && y < height ) {  
55:            pixels.add(original.getPixel(x, y));  
56:          }  
57:        }  
58:      }  
59:      return new Color(255 - diffRed(pixels), 255 - diffGreen(pixels), 255 - diffBlue(pixels));  
60:    }  
61:    /**  
62:     * @param pixels The list of pixels to be averaged.  
63:     * @return The average of all the red values in the given list of pixels.  
64:     */  
65:    private int diffRed(List<Color> pixels)  
66:    {  
67:      int max = 0;  
68:      int min = 255;  
69:      for(Color color : pixels) {  
70:        int val = color.getRed();  
71:        if(val > max) {  
72:          max = val;  
73:        }  
74:        if(val < min) {  
75:          min = val;  
76:        }  
77:      }  
78:      int difference = max - min - TOLERANCE;  
79:      if(difference < 0) {  
80:        difference = 0;  
81:      }  
82:      return difference;  
83:    }  
84:    /**  
85:     * @param pixels The list of pixels to be averaged.  
86:     * @return The average of all the green values in the given list of pixels.  
87:     */  
88:    private int diffGreen(List<Color> pixels)  
89:    {  
90:      int max = 0;  
91:      int min = 255;  
92:      for(Color color : pixels) {  
93:        int val = color.getGreen();  
94:        if(val > max) {  
95:          max = val;  
96:        }  
97:        if(val < min) {  
98:          min = val;  
99:        }  
100:      }  
101:      int difference = max - min - TOLERANCE;  
102:      if(difference < 0) {  
103:        difference = 0;  
104:      }  
105:      return difference;  
106:    }  
107:    /**  
108:     * @param pixels The list of pixels to be averaged.  
109:     * @return The average of all the blue values in the given list of pixels.  
110:     */  
111:    private int diffBlue(List<Color> pixels)  
112:    {  
113:      int max = 0;  
114:      int min = 255;  
115:      for(Color color : pixels) {  
116:        int val = color.getBlue();  
117:        if(val > max) {  
118:          max = val;  
119:        }  
120:        if(val < min) {  
121:          min = val;  
122:        }  
123:      }  
124:      int difference = max - min - TOLERANCE;  
125:      if(difference < 0) {  
126:        difference = 0;  
127:      }  
128:      return difference;  
129:    }  
130:  }  


    • FishEyeFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to create an effect similar to a fisheye camera lens.  
4:   * (Works especially well on portraits.)  
5:   *   
6:   * @author Haikal Almaz Said  
7:   * @version 12/10/2018  
8:   */  
9:  public class FishEyeFilter extends Filter  
10:  {  
11:    // constants:  
12:    private final static int SCALE = 20;  // this defines the strenght of the filter  
13:    private final static double TWO_PI = 2 * Math.PI;  
14:    /**  
15:     * Constructor for objects of class LensFilter.  
16:     * @param name The name of the filter.  
17:     */  
18:    public FishEyeFilter(String name)  
19:    {  
20:      super(name);  
21:    }  
22:    /**  
23:     * Apply this filter to an image.  
24:     *   
25:     * @param image The image to be changed by this filter.  
26:     */  
27:    public void apply(OFImage image)  
28:    {  
29:      int height = image.getHeight();  
30:      int width = image.getWidth();  
31:      OFImage original = new OFImage(image);  
32:      int[] xa = computeXArray(width);  
33:      int[] ya = computeYArray(height);  
34:      for(int y = 0; y < height; y++) {  
35:        for(int x = 0; x < width; x++) {  
36:          image.setPixel(x, y, original.getPixel(x + xa[x], y + ya[y]));  
37:        }  
38:      }  
39:    }  
40:    /**  
41:     * Compute and return an array of horizontal offsets for each pixel column.  
42:     * These can then be applied as the horizontal offset for each pixel.  
43:     */  
44:    private int[] computeXArray(int width)  
45:    {  
46:      int[] xArray = new int[width];  
47:      for(int i=0; i < width; i++) {  
48:        xArray[i] = (int)(Math.sin( ((double)i / width) * TWO_PI) * SCALE);  
49:      }  
50:      return xArray;  
51:    }  
52:    /**  
53:     * Compute and return an array of vertical offsets for each pixel row.  
54:     * These can then be applied as the vertical offset for each pixel.  
55:     */  
56:    private int[] computeYArray(int height)  
57:    {  
58:      int[] yArray = new int[height];  
59:      for(int i=0; i < height; i++) {  
60:        yArray[i] = (int)(Math.sin( ((double)i / height) * TWO_PI) * SCALE);  
61:      }  
62:      return yArray;  
63:    }  
64:  }  


    • LighterFilter

1:  /**  
2:   * An image filter to make the image a bit lighter.  
3:   *   
4:   * @author Haikal Almaz Said  
5:   * @version 12/10/2018  
6:   */  
7:  public class LighterFilter extends Filter  
8:  {  
9:       /**  
10:        * Constructor for objects of class LighterFilter.  
11:     * @param name The name of the filter.  
12:        */  
13:       public LighterFilter(String name)  
14:    {  
15:      super(name);  
16:       }  
17:    /**  
18:     * Apply this filter to an image.  
19:     *   
20:     * @param image The image to be changed by this filter.  
21:     */  
22:    public void apply(OFImage image)  
23:    {  
24:      int height = image.getHeight();  
25:      int width = image.getWidth();  
26:      for(int y = 0; y < height; y++) {  
27:        for(int x = 0; x < width; x++) {  
28:          image.setPixel(x, y, image.getPixel(x, y).brighter());  
29:        }  
30:      }  
31:    }  
32:  }  


    • SmoothFilter

1:  import java.awt.Color;  
2:  import java.util.List;  
3:  import java.util.ArrayList;  
4:  /**  
5:   * An image filter to reduce sharp edges and pixelization. A bit like  
6:   * a soft lens.  
7:   *   
8:   * @author Haikal Almaz Said  
9:   * @version 12/10/2018  
10:   */  
11:  public class SmoothFilter extends Filter  
12:  {  
13:    private OFImage original;  
14:    private int width;  
15:    private int height;  
16:    /**  
17:     * Constructor for objects of class SmoothFilter.  
18:     * @param name The name of the filter.  
19:     */  
20:    public SmoothFilter(String name)  
21:    {  
22:      super(name);  
23:    }  
24:    /**  
25:     * Apply this filter to an image.  
26:     *   
27:     * @param image The image to be changed by this filter.  
28:     */  
29:    public void apply(OFImage image)  
30:    {  
31:      original = new OFImage(image);  
32:      width = original.getWidth();  
33:      height = original.getHeight();  
34:      for(int y = 0; y < height; y++) {  
35:        for(int x = 0; x < width; x++) {  
36:          image.setPixel(x, y, smooth(x, y));  
37:        }  
38:      }  
39:    }  
40:    /**  
41:     * Return a new color that is the smoothed color of a given  
42:     * position. The "smoothed color" is the color value that is the  
43:     * average of this pixel and all the adjacent pixels.  
44:     * @param xpos The xposition of the pixel.  
45:     * @param ypos The yposition of the pixel.  
46:     * @return The smoothed color.  
47:     */  
48:    private Color smooth(int xpos, int ypos)  
49:    {  
50:      List<Color> pixels = new ArrayList<Color>(9);  
51:      for(int y = ypos - 1; y <= ypos + 1; y++) {  
52:        for(int x = xpos - 1; x <= xpos + 1; x++) {  
53:          if( x >= 0 && x < width && y >= 0 && y < height )  
54:            pixels.add(original.getPixel(x, y));  
55:        }  
56:      }  
57:      return new Color(avgRed(pixels), avgGreen(pixels), avgBlue(pixels));  
58:    }  
59:    /**  
60:     * @param pixels The list of pixels.  
61:     * @return The average of all the red values in the given list of pixels.  
62:     */  
63:    private int avgRed(List<Color> pixels)  
64:    {  
65:      int total = 0;  
66:      for(Color color : pixels) {  
67:        total += color.getRed();  
68:      }  
69:      return total / pixels.size();  
70:    }  
71:    /**  
72:     * @param pixels The list of pixels.  
73:     * @return The average of all the green values in the given list of pixels.  
74:     */  
75:    private int avgGreen(List<Color> pixels)  
76:    {  
77:      int total = 0;  
78:      for(Color color : pixels) {  
79:        total += color.getGreen();  
80:      }  
81:      return total / pixels.size();  
82:    }  
83:    /**  
84:     * @param pixels The list of pixels.  
85:     * @return The average of all the blue values in the given list of pixels.  
86:     */  
87:    private int avgBlue(List<Color> pixels)  
88:    {  
89:      int total = 0;  
90:      for(Color color : pixels) {  
91:        total += color.getBlue();  
92:      }  
93:      return total / pixels.size();  
94:    }  
95:  }  


    • PixelizeFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to create a pixelization effect, like an enlarged  
4:   * low-resolution digital image.  
5:   *   
6:   * @author Haikal Almaz Said  
7:   * @version 12/10/2018  
8:   */  
9:  public class PixelizeFilter extends Filter  
10:  {  
11:    /**  
12:     * Constructor for objects of class PixelizeFilter.  
13:     * @param name The name of the filter.  
14:     */  
15:    public PixelizeFilter(String name)  
16:    {  
17:      super(name);  
18:    }  
19:    /**  
20:     * Apply this filter to an image.  
21:     *   
22:     * @param image The image to be changed by this filter.  
23:     */  
24:    public void apply(OFImage image)  
25:    {  
26:      final int PIXEL_SIZE = 5;  
27:      int width = image.getWidth();  
28:      int height = image.getHeight();  
29:      for(int y = 0; y < height; y += PIXEL_SIZE) {  
30:        for(int x = 0; x < width; x += PIXEL_SIZE) {  
31:          Color pix = image.getPixel(x, y);  
32:          for(int dy = y; dy < y + PIXEL_SIZE; dy++) {  
33:            for(int dx = x; dx < x + PIXEL_SIZE; dx++) {  
34:              if( dx < width && dy < height )  
35:                image.setPixel(dx, dy, pix);  
36:            }  
37:          }  
38:        }  
39:      }  
40:    }  
41:  }  


    • GrayScaleFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to remove color from an image.  
4:   *   
5:   * @author Haikal Almaz Said  
6:   * @version 12/10/2018  
7:   */  
8:  public class GrayScaleFilter extends Filter  
9:  {  
10:       /**  
11:        * Constructor for objects of class GrayScaleFilter.  
12:        * @param name The name of the filter.  
13:        */  
14:       public GrayScaleFilter(String name)  
15:    {  
16:      super(name);  
17:       }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width; x++) {  
29:          Color pix = image.getPixel(x, y);  
30:          int avg = (pix.getRed() + pix.getGreen() + pix.getBlue()) / 3;  
31:          image.setPixel(x, y, new Color(avg, avg, avg));  
32:        }  
33:      }  
34:    }  
35:  }  


    • ThresholdFilter

1:  import java.awt.Color;  
2:  /**  
3:   * An three-level gray-based threshold filter.  
4:   *   
5:   * @author Haikal Almaz Said  
6:   * @version 12/10/2018  
7:   */  
8:  public class ThresholdFilter extends Filter  
9:  {  
10:       /**  
11:        * Constructor for objects of class ThresholdFilter.  
12:     * @param name The name of the filter.  
13:        */  
14:       public ThresholdFilter(String name)  
15:    {  
16:      super(name);  
17:       }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width; x++) {  
29:          Color pixel = image.getPixel(x, y);  
30:          int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;  
31:          if(brightness <= 85) {  
32:            image.setPixel(x, y, Color.BLACK);  
33:          }  
34:          else if(brightness <= 170) {  
35:            image.setPixel(x, y, Color.GRAY);  
36:          }  
37:          else {  
38:            image.setPixel(x, y, Color.WHITE);  
39:          }  
40:        }  
41:      }  
42:    }  
43:  }  


    • DarkerFilter

1:  /**  
2:   * An image filter to make the image a bit darker.  
3:   *   
4:   * @author Haikal Almaz Said  
5:   * @version 12/10/2018  
6:   */  
7:  public class DarkerFilter extends Filter  
8:  {  
9:    /**  
10:     * Constructor for objects of class DarkerFilter.  
11:     * @param name The name of the filter.  
12:     */  
13:    public DarkerFilter(String name)  
14:    {  
15:      super(name);  
16:    }  
17:    /**  
18:     * Apply this filter to an image.  
19:     *   
20:     * @param image The image to be changed by this filter.  
21:     */  
22:    public void apply(OFImage image)  
23:    {  
24:      int height = image.getHeight();  
25:      int width = image.getWidth();  
26:      for(int y = 0; y < height; y++) {  
27:        for(int x = 0; x < width; x++) {  
28:          image.setPixel(x, y, image.getPixel(x, y).darker());  
29:        }  
30:      }  
31:    }  
32:  }  



  • Screenshot

(Tampilan ketika pertama menjalankan program)


(Tampilan ketika membuka file gambar)


(Tampilan ketika menggunakan fitur Smaller dimana ukuran gambar mengecil)


(Tampilan ketika menggunakan fitur Larger dimana ukuran gambar membesar)


(Memasukkan koordinat X awal untuk Add Text)

(Memasukkan koordinat Y untuk Add Text)

(Memasukkan Font Size untuk Add Text)

(Menambahkan Warna dengan RGB pada Add Text)


(Memasukkan Teks yang akan di-Insert)

(Tampilan setelah Add Text)




(Setelah Crop dengan koordinat 0,0 ; 400,300)


(Setelah diberi filter Darker)


(Setelah diberi filter Edge Detection)


(Setelah diberi filter Fish Eye)


(Setelah diberi filter Gray Scale)


(Setelah diberi Filter Invert)

(Setelah diberi filter Lighter)


(Setelah diberi filter Pixelize)


(Setelah diberi filter Mirror)


(Setelah diberi filter Smooth)

(Setelah diberi filter Solarize)


(Setelah diberi filter Threshold)