Rabu, 12 Desember 2018

PWEB C EAS

Nama : Shawn Timothy Mulya Putra
NRP   : 05111740000095

Rancangan Basis Data
interfaces

Berikut Source Codenya:
1. Config.php
 <?php   
  $server = "localhost";   
  $user = "root";   
  $password = "kopi";   
  $nama_database = "pendaftaran_siswa";   
  $db = mysqli_connect($server, $user, $password="", $nama_database);   
  if( !$db ){   
   die("Gagal terhubung dengan database: " . mysqli_connect_error());   
  }   
  ?>   

2. Download.php
 <?php    
  $connect = mysqli_connect("localhost", "root", "", "pendaftaran_siswa");   
  $output = '';   
  if(isset($_POST["download"]))   
  {   
  $query = "SELECT * FROM calon_siswa";   
  $result = mysqli_query($connect, $query);   
  if(mysqli_num_rows($result) > 0)   
  {   
  $output .= '   
   <table class="table" bordered="1">    
       <tr>    
        <th>ID</th>    
        <th>Nama</th>    
        <th>Alamat</th>   
        <th>Jenis_Kelamin</th>   
        <th>Agama</th>   
        <th>Sekolah_asal</th>   
        <th>Sekolah_pilihan</th>    
       </tr>   
  ';   
  while($row = mysqli_fetch_array($result))   
  {   
   $output .= '   
   <tr>    
        <td>'.$row["ID"].'</td>    
        <td>'.$row["Nama"].'</td>    
        <td>'.$row["Alamat"].'</td>    
        <td>'.$row["Jenis_Kelamin"].'</td>   
        <td>'.$row["Agama"].'</td>   
        <td>'.$row["Sekolah_asal"].'</td>   
        <td>'.$row["Sekolah_pilihan"].'</td>   
       </tr>   
   ';   
  }   
  $output .= '</table>';   
  header('Content-Type: application/xls');   
  header('Content-Disposition: attachment; filename=download.xls');   
  echo $output;   
  }   
  }   
  ?>   

3. Errors.php
  <?php if (count($errors) > 0) : ?>   
  <div class="error">   
     <?php foreach ($errors as $error) : ?>   
     <p><?php echo $error ?></p>   
     <?php endforeach ?>   
  </div>   
  <?php endif ?>   

4. Form-daftar.php
 <!DOCTYPE html>   
  <html>   
  <head>   
   <title>Formulir Pendaftaran Siswa Baru 2019| SEKOLAH MENENGAH ATAS DAERAH TANGERANG</title>   
   <style type="text/css">    
   body {   
    background: #5F9EA0;   
    font-family: sans-serif;   
   }   
   .login {   
    padding: 1em;   
    margin: 2em auto;   
    width: 20em;   
    background: #fff;   
    border-radius: 3px;   
   }   
   label {   
    font-size: 10pt;   
    color: #555;   
   }   
   textarea {   
    padding: 8px;   
    width: 95%;   
    background: #efefef;   
    border: 0;   
    font-size: 10pt;   
    margin: 6px 0px;   
   }   
   .tombol {   
    background: #7A0000;   
    color: #fff;   
    border: 0;   
    padding: 5px 8px;   
   }   
  </head>   
  </style>   
  <body>   
   <header>   
    <h2 align="center">FORMULIR PENDAFTARAN SISWA BARU 2019</h2>   
    <h3 align="center">SEKOLAH MENENGAH ATAS DAERAH TANGERANG</h3>   
    <h4 align="center">PERINGATAN! HANYA DIPERBOLEHKAN MENGISI FORMULIR SATU KALI</h4>   
   </header>   
   <div class="login">   
   <form action="proses-pendaftaran.php" method="POST">   
    <fieldset>   
    <p>   
     <label for="nama">Nama: </label>   
     <input type="text" name="nama" placeholder="Nama lengkap" />   
    </p>   
    <p>   
     <label for="alamat">Alamat: </label>   
     <textarea name="alamat"></textarea>   
    </p>   
    <p>   
     <label for="jenis_kelamin">Jenis Kelamin: </label>   
     <label><input type="radio" name="jenis_kelamin" value="laki-laki"> Laki-laki</label>   
     <label><input type="radio" name="jenis_kelamin" value="perempuan"> Perempuan</label>   
    </p>   
    <p>   
     <label for="agama">Agama: </label>   
     <select name="agama">   
      <option>Islam</option>   
      <option>Kristen</option>   
      <option>Hindu</option>   
      <option>Budha</option>   
      <option>Katholik</option>   
     </select>   
    </p>   
    <p>   
     <label for="sekolah_asal">Sekolah Asal: </label>   
     <input type="text" name="sekolah_asal" placeholder="Nama sekolah" />   
    </p>   
    <p>   
     <label for="sekolah_pilihan">Sekolah Pilihan: </label>   
     <select name="sekolah_pilihan">   
      <option>SMA N 1 Tangerang</option>   
      <option>SMA N 2 Tangerang</option>   
      <option>SMA N 3 Tangerang</option>   
      <option>SMA N 4 Tangerang</option>    
     </select>   
    </p>   
    <p>   
     <input type="submit" value="Daftar" name="daftar" class="tombol" />   
    </p>   
    </fieldset>   
   </form>   
   </body>   
  </html>   

5. Form-edit.php
 <?php   
  include("config.php");   
  // kalau tidak ada id di query string   
  if( !isset($_GET['id']) ){   
   header('Location: list-siswa.php');   
  }   
  //ambil id dari query string   
  $id = $_GET['id'];   
  // buat query untuk ambil data dari database   
  $sql = "SELECT * FROM calon_siswa WHERE id=$id";   
  $query = mysqli_query($db, $sql);   
  $siswa = mysqli_fetch_assoc($query);   
  // jika data yang di-edit tidak ditemukan   
  if( mysqli_num_rows($query) < 1 ){   
   die("data tidak ditemukan...");   
  }   
  ?>   
  <!DOCTYPE html>   
  <html>   
  <head>   
   <title>Formulir Edit Siswa | SEKOLAH MENENGAH ATAS DAERAH BOJONEGORO</title>   
   <style type="text/css">    
   body {   
    background: #5F9EA0;   
    font-family: sans-serif;   
   }   
   .login {   
    padding: 1em;   
    margin: 2em auto;   
    width: 20em;   
    background: #fff;   
    border-radius: 3px;   
   }   
   label {   
    font-size: 10pt;   
    color: #555;   
   }   
   textarea {   
    padding: 8px;   
    width: 95%;   
    background: #efefef;   
    border: 0;   
    font-size: 10pt;   
    margin: 6px 0px;   
   }   
   .tombol {   
    background: #7A0000;   
    color: #fff;   
    border: 0;   
    padding: 5px 8px;   
   }   
  </head>   
  </style>   
  <body>   
   <header>   
    <h3>Formulir Edit Siswa</h3>   
   </header>   
   <div class="login">   
   <form action="proses-edit.php" method="POST">   
    <fieldset>   
     <input type="hidden" name="id" value="<?php echo $siswa['id'] ?>" />   
    <p>   
     <label for="nama">Nama: </label>   
     <input type="text" name="nama" placeholder="nama lengkap" value="<?php echo $siswa['nama'] ?>" />   
    </p>   
    <p>   
     <label for="alamat">Alamat: </label>   
     <textarea name="alamat"><?php echo $siswa['alamat'] ?></textarea>   
    </p>   
    <p>   
     <label for="jenis_kelamin">Jenis Kelamin: </label>   
     <?php $jk = $siswa['jenis_kelamin']; ?>   
     <label><input type="radio" name="jenis_kelamin" value="laki-laki" <?php echo ($jk == 'laki-laki') ? "checked": "" ?>> Laki-laki</label>   
     <label><input type="radio" name="jenis_kelamin" value="perempuan" <?php echo ($jk == 'perempuan') ? "checked": "" ?>> Perempuan</label>   
    </p>   
    <p>   
     <label for="agama">Agama: </label>   
     <?php $agama = $siswa['agama']; ?>   
     <select name="agama">   
      <option <?php echo ($agama == 'Islam') ? "selected": "" ?>>Islam</option>   
      <option <?php echo ($agama == 'Kristen') ? "selected": "" ?>>Kristen</option>   
      <option <?php echo ($agama == 'Hindu') ? "selected": "" ?>>Hindu</option>   
      <option <?php echo ($agama == 'Budha') ? "selected": "" ?>>Budha</option>   
      <option <?php echo ($agama == 'Katholik') ? "selected": "" ?>>Katholik</option>   
     </select>   
    </p>   
    <p>   
     <label for="sekolah_asal">Sekolah Asal: </label>   
     <input type="text" name="sekolah_asal" placeholder="nama sekolah" value="<?php echo $siswa['sekolah_asal'] ?>" />   
    </p>   
    <p>   
     <label for="sekolah_pilihan">Sekolah Pilihan: </label>   
     <?php $sekolah_pilihan = $siswa['sekolah_pilihan']; ?>   
     <select name="sekolah_pilihan">   
      <option <?php echo ($sekolahp == 'SMA N 1 Tangerang') ? "selected": "" ?>>SMA N 1 Tangerang</option>   
      <option <?php echo ($sekolahp == 'SMA N 2 Tangerang') ? "selected": "" ?>>SMA N 2 Tangerang</option>   
      <option <?php echo ($sekolahp == 'SMA N 3 Tangerang') ? "selected": "" ?>>SMA N 3 Tangerang</option>   
      <option <?php echo ($sekolahp == 'SMA N 4 Tangerang') ? "selected": "" ?>>SMA N 4 Tangerang</option>   
     </select>   
    </p>   
    <p>   
     <input type="submit" value="Simpan" name="simpan" />   
    </p>   
    </fieldset>   
   </form>   
   </body>   
  </html>   

6. hapus.php
 <?php   
  include("config.php");   
  if( isset($_GET['id']) ){   
   // ambil id dari query string   
   $id = $_GET['id'];   
   // buat query hapus   
   $sql = "DELETE FROM calon_siswa WHERE id=$id";   
   $query = mysqli_query($db, $sql);   
   // apakah query hapus berhasil?   
   if( $query ){   
    header('Location: list-siswa.php');   
   } else {   
    die("gagal menghapus...");   
   }   
  } else {   
   die("akses dilarang...");   
  }   
  ?>   

7. home.php
 <!DOCTYPE html>   
  <html>   
  <head>   
   <title>Pendaftaran Siswa Baru 2019 | SEKOLAH MENENGAH ATAS DAERAH TANGERANG</title>   
   <style type="text/css">    
   #topbar {   
    background-color: #5F9EA0;   
    width: 100%;   
    height: 100px;   
    color: white;   
   }   
   h4 {   
    color: #7A0000;   
   }   
   li {   
    color: #7A0000;   
   }   
  </head>   
  </style>   
  <body>   
  <div id="topbar">   
   <header>   
    <h1 align="center">Pendaftaran Siswa Baru 2019</h3>   
    <h3 align="center">SEKOLAH MENENGAH ATAS DAERAH BOJONEGORO</h1>   
    <hr align="nilai">   
   </header>   
   <h4>Menu</h4>   
   <nav>   
    <ul>   
     <li><a href="form-daftar.php">Isi Formulir</a></li>   
     <li><a href="list-siswa.php">Edit/Hapus</a></li>   
     <li><a href="view.php">Lihat Formulir</a></li>   
    </ul>   
   </nav>   
   </body>   
  </html>   

8. index.php
  <?php    
  session_start();    
  if (!isset($_SESSION['username'])) {   
     $_SESSION['msg'] = "You must log in first";   
     header('location: login.php');   
  }   
  if (isset($_GET['logout'])) {   
     session_destroy();   
     unset($_SESSION['username']);   
     header("location: login.php");   
  }   
  ?>   
  <!DOCTYPE html>   
  <html>   
  <head>   
    <title>Home</title>   
    <link rel="stylesheet" type="text/css" href="style.css">   
  </head>   
  <body>   
  <div class="header">   
    <h2>Home Page</h2>   
  </div>   
  <div class="content">   
     <!-- notification message -->   
     <?php if (isset($_SESSION['success'])) : ?>   
   <div class="error success" >   
      <h3>   
    <?php    
       echo $_SESSION['success'];    
       unset($_SESSION['success']);   
    ?>   
      </h3>   
   </div>   
     <?php endif ?>   
   <!-- logged in user information -->   
   <?php if (isset($_SESSION['username'])) : ?>   
     <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>   
   <p> <a href="home.php" style="color: red;">Fill The Formulir</a> </p>   
     <p> <a href="index.php?logout='1'" style="color: red;">Logout</a> </p>   
   <?php endif ?>   
  </div>   
  </body>   
  </html>   

9.list-siswa.php
 <?php include("config.php"); ?>   
  <!DOCTYPE html>   
  <html>   
  <head>   
   <title>Formulir Pendaftaran Siswa Baru 2019| SEKOLAH MENENGAH ATAS DAERAH TANGERANG</title>   
   <style type="text/css">    
   body {   
    background: #5F9EA0;   
    font-family: sans-serif;   
   }   
   .login {   
    padding: 1em;   
    margin: 2em auto;   
    width: 30em;   
    background: #fff;   
    border-radius: 3px;   
   }   
   label {   
    font-size: 10pt;   
    color: #555;   
   }   
   textarea {   
    padding: 8px;   
    width: 95%;   
    background: #efefef;   
    border: 0;   
    font-size: 10pt;   
    margin: 6px 0px;   
   }   
   .tombol {   
    background: #7A0000;   
    color: #fff;   
    border: 0;   
    padding: 5px 8px;   
   }   
  </head>   
  </style>   
  <body>   
    <header>   
    <h2 align="center">FORMULIR PENDAFTARAN SISWA BARU 2019</h2>   
    <h3 align="center">SEKOLAH MENENGAH ATAS DAERAH TANGERANG</h3>   
    </header>   
    <div class="login">   
    <fieldset>   
    <p>   
    <?php   
    $sql = "SELECT * FROM calon_siswa";   
    $query = mysqli_query($db, $sql);   
    $siswa = mysqli_fetch_array($query);   
    echo "<p align='center'> FORMULIR PENDAFTARAN SISWA BARU 2019</p>";   
    echo " <p align='center'> SEKOLAH MENENGAH ATAS DAERAH TANGERANG</p>";   
    echo "__________________________________________________";   
    echo "<br />";   
    echo "<br />";   
    echo "<strong>ID</strong><br>";   
    echo $siswa['ID']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Nama</strong><br>";   
    echo $siswa['Nama']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Alamat</strong><br>";   
    echo $siswa['Alamat']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Jenis Kelamin</strong><br>";   
    echo $siswa['Jenis_Kelamin']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Agama</strong><br>";   
    echo $siswa['Agama']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Sekolah Asal</strong><br>";   
    echo $siswa['Sekolah_asal']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Sekolah Pilihan</strong><br>";   
    echo $siswa['Sekolah_pilihan']; echo "<br>";   
    echo "<br /></br>";   
    echo "<td>";   
    echo "<a href='form-edit.php?id=".$siswa['ID']."'>Edit</a> | ";   
    echo "<a href='hapus.php?id=".$siswa['ID']."'>Hapus</a>";   
    echo "</td>";   
    ?>   
    </p>   
  </html>    
  </body>   

10.login.php
 <?php include('server.php') ?>   
  <!DOCTYPE html>   
  <html>   
  <head>   
  <title>Registrasi Pendaftaran Siswa</title>   
  <link rel="stylesheet" type="text/css" href="style.css">   
  </head>   
  <body>   
  <div class="header">   
     <h2>Login</h2>   
  </div>   
  <form method="post" action="login.php">   
     <?php include('errors.php'); ?>   
     <div class="input-group">   
       <label>Username</label>   
       <input type="text" name="username" >   
     </div>   
     <div class="input-group">   
       <label>Password</label>   
       <input type="password" name="password">   
     </div>   
     <div class="input-group">   
       <button type="submit" class="btn" name="login_user">Login</button>   
     </div>   
     <p>   
       Belum memiliki akun? <a href="register.php">Sign up</a>   
     </p>   
  </form>   
  </body>   
  </html>   

11.proses-edit.php
 <?php   
  include("config.php");    
  if(isset($_POST['simpan'])){   
   $id = $_POST['ID'];   
   $nama = $_POST['Nama'];   
   $alamat = $_POST['Alamat'];   
   $jk = $_POST['Jenis_Kelamin'];   
   $agama = $_POST['Agama'];   
   $sekolah = $_POST['Sekolah_asal'];   
   $sekolahp = $_POST['Sekolah_pilihan'];   
   $sql = "UPDATE calon_siswa SET Nama='$nama', Alamat='$alamat', Jenis_Kelamin='$jk', Agama='$agama', Sekolah_asal='$sekolah', Sekolah_pilihan='$sekolahp' WHERE ID=$id";   
   $query = mysqli_query($db, $sql);   
   if( $query ) {    
    header('Location: list-siswa.php');   
   } else {   
    die("Gagal menyimpan perubahan...");   
   }   
  } else {   
   die("Akses dilarang...");   
  }   
  ?>   

12. peoses-pendaftaran.php
 <?php   
  include("config.php");   
  if(isset($_POST['daftar'])){   
   $nama = $_POST['nama'];   
   $alamat = $_POST['alamat'];   
   $jk = $_POST['jenis_kelamin'];   
   $agama = $_POST['agama'];   
   $sekolah = $_POST['sekolah_asal'];   
   $sekolahp = $_POST['sekolah_pilihan'];   
   $sql = "INSERT INTO calon_siswa (nama, alamat, jenis_kelamin, agama, sekolah_asal, sekolah_pilihan) VALUE ('$nama', '$alamat', '$jk', '$agama', '$sekolah', '$sekolahp')";   
   $query = mysqli_query($db, $sql);   
   if( $query ) {   
    header('Location: home.php?status=sukses');   
   } else {   
   header('Location: home.php?status=gagal');   
   }   
  } else {   
   die("Akses dilarang...");   
  }   
  ?>   

13.server.php
 <?php   
  session_start();   
  $username = "";   
  $email = "";   
  $errors = array();    
  $db = mysqli_connect('localhost', 'root', '', 'registration');   
  if (isset($_POST['reg_user'])) {   
  $username = mysqli_real_escape_string($db, $_POST['username']);   
  $email = mysqli_real_escape_string($db, $_POST['email']);   
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);   
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);   
  if (empty($username)) { array_push($errors, "Username is required"); }   
  if (empty($email)) { array_push($errors, "Email is required"); }   
  if (empty($password_1)) { array_push($errors, "Password is required"); }   
  if ($password_1 != $password_2) {   
    array_push($errors, "The two passwords do not match");   
  }   
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";   
  $result = mysqli_query($db, $user_check_query);   
  $user = mysqli_fetch_assoc($result);   
  if ($user) {   
   if ($user['username'] === $username) {   
   array_push($errors, "Username already exists");   
   }   
   if ($user['email'] === $email) {   
   array_push($errors, "email already exists");   
   }   
  }   
  if (count($errors) == 0) {   
     $password = md5($password_1)  
        ;$query = "INSERT INTO users (username, email, password)    
          VALUES('$username', '$email', '$password')";   
     mysqli_query($db, $query);   
     $_SESSION['username'] = $username;   
     $_SESSION['success'] = "You are now logged in";   
     header('location: index.php');   
  }   
  }   
  if (isset($_POST['login_user'])) {   
  $username = mysqli_real_escape_string($db, $_POST['username']);   
  $password = mysqli_real_escape_string($db, $_POST['password']);   
  if (empty($username)) {   
   array_push($errors, "Username is required");   
  }   
  if (empty($password)) {   
   array_push($errors, "Password is required");   
  }   
  if (count($errors) == 0) {   
   $password = md5($password);   
   $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";   
   $results = mysqli_query($db, $query);   
   if (mysqli_num_rows($results) == 1) {   
   $_SESSION['username'] = $username;   
   $_SESSION['success'] = "You are now logged in";   
   header('location: index.php');   
   }else {   
   array_push($errors, "Wrong username/password combination");   
   }   
  }   
  }   
  ?>   

14. views.php
 <?php include("config.php"); ?>   
  <!DOCTYPE html>   
  <html>   
  <head>   
   <title>Formulir Pendaftaran Siswa Baru 2019| SEKOLAH MENENGAH ATAS DAERAH BOJONEGORO</title>   
   <style type="text/css">    
   body {   
    background: #5F9EA0;   
    font-family: sans-serif;   
   }   
   .login {   
    padding: 1em;   
    margin: 2em auto;   
    width: 30em;   
    background: #fff;   
    border-radius: 3px;   
   }   
   label {   
    font-size: 10pt;   
    color: #555;   
   }   
   textarea {   
    padding: 8px;   
    width: 95%;   
    background: #efefef;   
    border: 0;   
    font-size: 10pt;   
    margin: 6px 0px;   
   }   
   .tombol {   
    background: #7A0000;   
    color: #fff;   
    border: 0;   
    padding: 5px 8px;   
   }   
  </head>   
  </style>   
  <body>   
    <header>   
    <h2 align="center">FORMULIR PENDAFTARAN SISWA BARU 2019</h2>   
    <h3 align="center">SEKOLAH MENENGAH ATAS DAERAH BOJONEGORO</h3>   
    </header>   
    <div class="login">   
    <fieldset>   
    <p>   
    <?php   
    $sql = "SELECT * FROM calon_siswa";   
    $query = mysqli_query($db, $sql);   
    $siswa = mysqli_fetch_array($query);   
    echo "<p align='center'> FORMULIR PENDAFTARAN SISWA BARU 2019</p>";   
    echo " <p align='center'> SEKOLAH MENENGAH ATAS DAERAH BOJONEGORO</p>";   
    echo "__________________________________________________";   
    echo "<br />";   
    echo "<br />";   
    echo "<strong>ID</strong><br>";   
    echo $siswa['ID']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Nama</strong><br>";   
    echo $siswa['Nama']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Alamat</strong><br>";   
    echo $siswa['Alamat']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Jenis Kelamin</strong><br>";   
    echo $siswa['Jenis_Kelamin']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Agama</strong><br>";   
    echo $siswa['Agama']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Sekolah Asal</strong><br>";   
    echo $siswa['Sekolah_asal']; echo "<br>";   
    echo "<br />";   
    echo "<strong>Sekolah Pilihan</strong><br>";   
    echo $siswa['Sekolah_pilihan']; echo "<br>";   
    echo "<br /></br></br></br>";   
    echo "<p align='right'>Tertanda</p>";   
    echo "</br></br>";   
    echo "<p align='right'>Panitia PPDB</p>";   
    ?>   
    </p>   
    <form method="post" action="download.php">   
    <input type="submit" name="download" class="btn btn-success" value="Download" />   
  </html>    
  </body>   

Hasilnya disini https://pwebc.000webhostapp.com/

Senin, 10 Desember 2018

PBO A EAS

Pada kesempatan kali ini mendapat tugas  membuat image editor seperti photoshop. Berikut hasilnya:
1. diagram
2 edge filter

3. display awal

4. invert filter

 5. mirror filter

Berikut source codenya:
1. OFImage

 import java.awt.*;  
 import java.awt.image.*;  
 import javax.swing.*;  
 public class OFImage extends BufferedImage  
 {  
   public OFImage(BufferedImage image)  
   {  
      super(image.getColorModel(), image.copyData(null),   
         image.isAlphaPremultiplied(), null);  
   }  
   public OFImage(int width, int height)  
   {  
     super(width, height, TYPE_INT_RGB);  
   }  
   public void setPixel(int x, int y, Color col)  
   {  
     int pixel = col.getRGB();  
     setRGB(x, y, pixel);  
   }  
   public Color getPixel(int x, int y)  
   {  
     int pixel = getRGB(x, y);  
     return new Color(pixel);  
   }  
 }  
2. ImageFileManager

 import java.awt.image.*;  
 import javax.imageio.*;  
 import java.io.*;  
 public class ImageFileManager  
 {  
   private static final String IMAGE_FORMAT = "jpg";  
   public static OFImage loadImage(File imageFile)  
   {  
     try {  
       BufferedImage image = ImageIO.read(imageFile);  
       if(image == null || (image.getWidth(null) < 0)) {  
           return null;  
       }  
       return new OFImage(image);  
     }  
     catch(IOException exc) {  
       return null;  
     }  
   }  
   public static void saveImage(OFImage image, File file)  
   {  
     try {  
       ImageIO.write(image, IMAGE_FORMAT, file);  
     }  
     catch(IOException exc) {  
       return;  
     }  
   }  
 }  
3. ImageViewer

 import java.awt.*;  
 import java.awt.event.*;  
 import java.awt.image.*;  
 import javax.swing.*;  
 import javax.swing.border.*;  
 import java.io.File;  
 import java.util.List;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 public class ImageViewer  
 {  
   private static final String VERSION = "Version 3.1";  
   private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
   private JFrame frame;  
   private ImagePanel imagePanel;  
   private JLabel filenameLabel;  
   private JLabel statusLabel;  
   private JButton smallerButton;  
   private JButton largerButton;  
   private OFImage currentImage;  
   private List<Filter> filters;  
   public ImageViewer()  
   {  
     currentImage = null;  
     filters = createFilters();  
     makeFrame();  
   }  
   private void openFile()  
   {  
     int returnVal = fileChooser.showOpenDialog(frame);  
     if(returnVal != JFileChooser.APPROVE_OPTION) {  
       return;   
     }  
     File selectedFile = fileChooser.getSelectedFile();  
     currentImage = ImageFileManager.loadImage(selectedFile);  
     if(currentImage == null) {  // image file was not a valid image  
       JOptionPane.showMessageDialog(frame,  
           "The file was not in a recognized image file format.",  
           "Image Load Error",  
           JOptionPane.ERROR_MESSAGE);  
       return;  
     }  
     imagePanel.setImage(currentImage);  
     setButtonsEnabled(true);  
     showFilename(selectedFile.getPath());  
     showStatus("File loaded.");  
     frame.pack();  
   }  
   private void close()  
   {  
     currentImage = null;  
     imagePanel.clearImage();  
     showFilename(null);  
     setButtonsEnabled(false);  
   }  
   private void saveAs()  
   {  
     if(currentImage != null) {  
       int returnVal = fileChooser.showSaveDialog(frame);  
       if(returnVal != JFileChooser.APPROVE_OPTION) {  
         return;   
       }  
       File selectedFile = fileChooser.getSelectedFile();  
       ImageFileManager.saveImage(currentImage, selectedFile);  
       showFilename(selectedFile.getPath());  
     }  
   }  
   private void quit()  
   {  
     System.exit(0);  
   }  
   private void applyFilter(Filter filter)  
   {  
     if(currentImage != null) {  
       filter.apply(currentImage);  
       frame.repaint();  
       showStatus("Applied: " + filter.getName());  
     }  
     else {  
       showStatus("No image loaded.");  
     }  
   }  
   private void showAbout()  
   {  
     JOptionPane.showMessageDialog(frame,   
           "ImageViewer\n" + VERSION,  
           "About ImageViewer",   
           JOptionPane.INFORMATION_MESSAGE);  
   }  
   private void makeLarger()  
   {  
     if(currentImage != null) {  
       int width = currentImage.getWidth();  
       int height = currentImage.getHeight();  
       OFImage newImage = new OFImage(width * 2, height * 2);  
       for(int y = 0; y < height; y++) {  
         for(int x = 0; x < width; x++) {  
           Color col = currentImage.getPixel(x, y);  
           newImage.setPixel(x * 2, y * 2, col);  
           newImage.setPixel(x * 2 + 1, y * 2, col);  
           newImage.setPixel(x * 2, y * 2 + 1, col);  
           newImage.setPixel(x * 2+1, y * 2 + 1, col);  
         }  
       }  
       currentImage = newImage;  
       imagePanel.setImage(currentImage);  
       frame.pack();  
     }  
   }  
   private void makeSmaller()  
   {  
     if(currentImage != null) {  
       int width = currentImage.getWidth() / 2;  
       int height = currentImage.getHeight() / 2;  
       OFImage newImage = new OFImage(width, height);  
       for(int y = 0; y < height; y++) {  
         for(int x = 0; x < width; x++) {  
           newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));  
         }  
       }  
       currentImage = newImage;  
       imagePanel.setImage(currentImage);  
       frame.pack();  
     }  
   }  
   private void showFilename(String filename)  
   {  
     if(filename == null) {  
       filenameLabel.setText("No file displayed.");  
     }  
     else {  
       filenameLabel.setText("File: " + filename);  
     }  
   }  
   private void showStatus(String text)  
   {  
     statusLabel.setText(text);  
   }  
   private void setButtonsEnabled(boolean status)  
   {  
     smallerButton.setEnabled(status);  
     largerButton.setEnabled(status);  
   }  
   private List<Filter> createFilters()  
   {  
     List<Filter> filterList = new ArrayList<Filter>();  
     filterList.add(new InvertFilter("Invert"));  
     filterList.add(new MirrorFilter("Mirror"));  
     filterList.add(new GrayScaleFilter("Grayscale"));  
     filterList.add(new EdgeFilter("Edge Detection"));  
     return filterList;  
   }  
   private void makeFrame()  
   {  
     frame = new JFrame("ImageViewer");  
     JPanel contentPane = (JPanel)frame.getContentPane();  
     contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));  
     makeMenuBar(frame);  
     contentPane.setLayout(new BorderLayout(6, 6));  
     imagePanel = new ImagePanel();  
     imagePanel.setBorder(new EtchedBorder());  
     contentPane.add(imagePanel, BorderLayout.CENTER);  
     filenameLabel = new JLabel();  
     contentPane.add(filenameLabel, BorderLayout.NORTH);  
     statusLabel = new JLabel(VERSION);  
     contentPane.add(statusLabel, BorderLayout.SOUTH);  
     JPanel toolbar = new JPanel();  
     toolbar.setLayout(new GridLayout(0, 1));  
     smallerButton = new JButton("Smaller");  
     smallerButton.addActionListener(new ActionListener()   
     {  
     public void actionPerformed(ActionEvent e) { makeSmaller(); }  
               });  
     toolbar.add(smallerButton);  
     largerButton = new JButton("Larger");  
     largerButton.addActionListener(new ActionListener()  
     {  
     public void actionPerformed(ActionEvent e) { makeLarger(); }  
               });  
     toolbar.add(largerButton);  
     JPanel flow = new JPanel();  
     flow.add(toolbar);  
     contentPane.add(flow, BorderLayout.WEST);  
     showFilename(null);  
     setButtonsEnabled(false);  
     frame.pack();  
     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
     frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);  
     frame.setVisible(true);  
   }  
   private void makeMenuBar(JFrame frame)  
   {  
     final int SHORTCUT_MASK =  
       Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
     JMenuBar menubar = new JMenuBar();  
     frame.setJMenuBar(menubar);  
     JMenu menu;  
     JMenuItem item;  
     menu = new JMenu("File");  
     menubar.add(menu);  
     item = new JMenuItem("Open...");  
       item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));  
       item.addActionListener(new ActionListener()   
       {  
       public void actionPerformed(ActionEvent e) { openFile(); }  
               });  
     menu.add(item);  
     item = new JMenuItem("Close");  
       item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));  
       item.addActionListener(new ActionListener()   
       {  
        public void actionPerformed(ActionEvent e) { close(); }  
               });  
     menu.add(item);  
     menu.addSeparator();  
     item = new JMenuItem("Save As...");  
       item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));  
       item.addActionListener(new ActionListener()   
       {  
       public void actionPerformed(ActionEvent e) { saveAs(); }  
               });  
     menu.add(item);  
     menu.addSeparator();  
     item = new JMenuItem("Quit");  
       item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));  
       item.addActionListener(new ActionListener()   
       {  
       public void actionPerformed(ActionEvent e) { quit(); }  
               });  
     menu.add(item);  
     menu = new JMenu("Filter");  
     menubar.add(menu);  
     for(final Filter filter : filters) {  
       item = new JMenuItem(filter.getName());  
       item.addActionListener(new ActionListener()   
       {  
         public void actionPerformed(ActionEvent e) {   
         applyFilter(filter);  
                 }  
               });  
        menu.add(item);  
      }  
     menu = new JMenu("Help");  
     menubar.add(menu);  
     item = new JMenuItem("About ImageViewer...");  
       item.addActionListener(new ActionListener()  
       {  
        public void actionPerformed(ActionEvent e) { showAbout(); }  
               });  
     menu.add(item);  
   }  
 }  
4. Filter

 public abstract class Filter  
 {  
   private String name;  
   public Filter(String name)  
   {  
     this.name = name;  
   }  
   public String getName()  
   {  
     return name;  
   }  
   public abstract void apply(OFImage image);  
 }  
5. ImagePanel

 import java.awt.*;  
 import javax.swing.*;  
 import java.awt.image.*;  
 public class ImagePanel extends JComponent  
 {  
   private int width, height;  
   private OFImage panelImage;  
   public ImagePanel()  
   {  
     width = 360;    
     height = 240;  
     panelImage = null;  
   }  
   public void setImage(OFImage Image)  
   {  
     if(Image != null) {  
       width = Image.getWidth();  
       height = Image.getHeight();  
       panelImage = Image;  
       repaint();  
     }  
   }  
   public void clearImage()  
   {  
     Graphics imageGraphics = panelImage.getGraphics();  
     imageGraphics.setColor(Color.LIGHT_GRAY);  
     imageGraphics.fillRect(0, 0, width, height);  
     repaint();  
   }  
   public Dimension getPreferredSize()  
   {  
     return new Dimension(width, height);  
   }  
   public void paintComponent(Graphics g)  
   {  
     Dimension size = getSize();  
     g.clearRect(0, 0, size.width, size.height);  
     if(panelImage != null) {  
       g.drawImage(panelImage, 0, 0, null);  
     }  
   }  
 }  
6. GrayScaleFilter

 import java.awt.Color;  
 public class GrayScaleFilter extends Filter  
 {  
      public GrayScaleFilter(String name)  
   {  
     super(name);  
      }  
   public void apply(OFImage image)  
   {  
     int height = image.getHeight();  
     int width = image.getWidth();  
     for(int y = 0; y < height; y++) {  
       for(int x = 0; x < width; x++) {  
         Color pix = image.getPixel(x, y);  
         int avg = (pix.getRed() + pix.getGreen() + pix.getBlue()) / 3;  
         image.setPixel(x, y, new Color(avg, avg, avg));  
       }  
     }  
   }  
 }  
7. MirrorFilter

 import java.awt.Color;  
 public class MirrorFilter extends Filter  
 {  
      public MirrorFilter(String name)  
   {  
     super(name);  
      }  
   public void apply(OFImage image)  
   {  
     int height = image.getHeight();  
     int width = image.getWidth();  
     for(int y = 0; y < height; y++) {  
       for(int x = 0; x < width / 2; x++) {  
         Color left = image.getPixel(x, y);  
         image.setPixel(x, y, image.getPixel(width - 1 - x, y));  
         image.setPixel(width - 1 - x, y, left);  
       }  
     }  
   }  
 }  
8. InvertFilter

 import java.awt.Color;  
 public class InvertFilter extends Filter  
 {  
   public InvertFilter(String name)  
   {  
     super(name);  
   }  
   public void apply(OFImage image)  
   {  
     int height = image.getHeight();  
     int width = image.getWidth();  
     for(int y = 0; y < height; y++) {  
       for(int x = 0; x < width; x++) {  
         Color pix = image.getPixel(x, y);  
         image.setPixel(x, y, new Color(255 - pix.getRed(),  
                         255 - pix.getGreen(),  
                         255 - pix.getBlue()));  
       }  
     }  
   }  
 }  
9. EdgeFilter

 import java.awt.Color;  
 import java.util.List;  
 import java.util.ArrayList;  
 public class EdgeFilter extends Filter  
 {  
   private static final int TOLERANCE = 20;  
   private OFImage original;  
   private int width;  
   private int height;  
   public EdgeFilter(String name)  
   {  
     super(name);  
   }  
   public void apply(OFImage image)  
   {  
     original = new OFImage(image);  
     width = original.getWidth();  
     height = original.getHeight();  
     for(int y = 0; y < height; y++) {  
       for(int x = 0; x < width; x++) {  
         image.setPixel(x, y, edge(x, y));  
       }  
     }  
   }  
   private Color edge(int xpos, int ypos)  
   {  
     List<Color> pixels = new ArrayList<Color>(9);  
     for(int y = ypos-1; y <= ypos+1; y++) {  
       for(int x = xpos-1; x <= xpos+1; x++) {  
         if( x >= 0 && x < width && y >= 0 && y < height ) {  
           pixels.add(original.getPixel(x, y));  
         }  
       }  
     }  
     return new Color(255 - diffRed(pixels), 255 - diffGreen(pixels), 255 - diffBlue(pixels));  
   }  
   private int diffRed(List<Color> pixels)  
   {  
     int max = 0;  
     int min = 255;  
     for(Color color : pixels) {  
       int val = color.getRed();  
       if(val > max) {  
         max = val;  
       }  
       if(val < min) {  
         min = val;  
       }  
     }  
     int difference = max - min - TOLERANCE;  
     if(difference < 0) {  
       difference = 0;  
     }  
     return difference;  
   }  
   private int diffGreen(List<Color> pixels)  
   {  
     int max = 0;  
     int min = 255;  
     for(Color color : pixels) {  
       int val = color.getGreen();  
       if(val > max) {  
         max = val;  
       }  
       if(val < min) {  
         min = val;  
       }  
     }  
     int difference = max - min - TOLERANCE;  
     if(difference < 0) {  
       difference = 0;  
     }  
     return difference;  
   }  
   private int diffBlue(List<Color> pixels)  
   {  
     int max = 0;  
     int min = 255;  
     for(Color color : pixels) {  
       int val = color.getBlue();  
       if(val > max) {  
         max = val;  
       }  
       if(val < min) {  
         min = val;  
       }  
     }  
     int difference = max - min - TOLERANCE;  
     if(difference < 0) {  
       difference = 0;  
     }  
     return difference;  
   }  
 }  

Rabu, 05 Desember 2018

PWEB C CodeIgniter

Pada kesempatan kali ini saya diperkenalkan dengan framework CodeIgniter. Langkah- langkahnya terdapat website petani kode saya belum dapat menyelesaikan semuanya karena ada masalah dibagian bootstrap. Berikut ada lah hasil yang telah kita pelajari

1. halaman awal

2. halaman about us

3. halaman contact us