Minggu, 14 Oktober 2018

UTS PBO

Pada hari ini, saya membuat program sistem parkir elektronik dengan java. berikut hasil dan source codenya.





Class Main

 import java.util.Scanner;  
 /**  
  * Write a description of class Main here.  
  *  
  * @author (Ismail)  
  * @version (15102018)  
  */  
 public class Main  
 {  
   public static void main(String args[])  
   {  
     Scanner scan = new Scanner(System.in);  
     int menu, harga;  
     harga = 2000;  
     SistemParkir tiket = new SistemParkir(harga);  
     System.out.println("Silahkan pilih tombol yang di inginkan \n");  
     System.out.println("Pos Masuk Parkir");  
     System.out.println("1. Parkir");  
     System.out.println("2. Bantuan\n");  
     System.out.println("Pos Keluar Parkir");  
     System.out.println("3. Scan Barcode Keluar");  
     System.out.println("4. Print Bukti Parkir");  
     int tipe=1;  
     while(tipe == 1)  
     {  
       menu = scan.nextInt();  
       switch(menu)  
       {  
         case 1:  
         System.out.println("Masukkan Waktu Parkir (contoh : 1700) : ");  
         int awal = scan.nextInt();  
         tiket.masukparkir(awal);  
         tiket.Tiketparkir();  
         break;  
         case 2:  
         System.out.println("Harap menunggu operator");  
         break;  
         case 3:  
         System.out.println("Masukkan Waktu Keluar Parkir (contoh 1800) : ");  
         int akhir = scan.nextInt();  
         tiket.keluarparkir(akhir);  
         tiket.Scanbarcode();  
         break;  
         case 4:  
         tiket.buktiparkir();  
         tipe=0;  
         break;  
       }  
     }  
   }  
 }  


Class SistemParkir

 /**  
  * Write a description of class SistemParkir here.  
  *  
  * @author (Ismail)  
  * @version (16102018)  
  */  
 public class SistemParkir  
 {  
   private int tiket;  
   private int masuk;  
   private int biaya;  
   private int keluar;  
   public SistemParkir(int harga)  
   {  
     masuk = 0;  
     biaya = harga;  
   }  
   public void Tiketparkir()  
   {  
     System.out.println("##############");  
     System.out.println("Selamat Datang");  
     System.out.println("Waktu Parkir : "+masuk);  
     System.out.println("############");  
   }  
   public int masukparkir(int awal)  
   {  
     return masuk=awal;  
   }  
   public void keluarparkir(int akhir)  
   {  
     keluar = akhir;  
     akhir -= masuk;
     akhir = akhir/1000;  
     biaya += akhir*1000;  
   }  
   public void Scanbarcode()  
   {  
     System.out.println("Biaya Parkir : Rp "+biaya);  
   }  
   public void buktiparkir()  
   {  
     System.out.println("#############");  
     System.out.println("Selamat Jalan");  
     System.out.println("Bukti Parkir");  
     System.out.println("Jam Parkir : "+masuk);  
     System.out.println("Jam Keluar : "+keluar);  
     System.out.println("Rp. "+biaya);  
     System.out.println("#############");  
   }  
 }  

Music Player

Pada kali ini, saya mendapat tugas untuk membuat program music organizer dengan java. Berikut source code dan hasil output programnya.

Class Music Organizer

 import java.util.ArrayList;  
 /**  
  *   
  *  
  * @author (Ismail)  
  * @version (14102018)  
  */  
 public class Musicorganizer  
 {  
   private ArrayList<String> files;  
   private Musicplayer player;  
   //membuat sebuah music organizer  
   public Musicorganizer()  
   {  
     files = new ArrayList<>();  
     player = new Musicplayer();  
   }  
   //menambahkan file ke koleksi  
   public void addFile(String filename)  
   {  
     files.add(filename);  
   }  
   //memasukkan jumlah file ke koleksi  
   public int getNumberOfFiles()  
   {  
     return files.size();  
   }  
   //list file dari koleksi  
   public void listFile(int index)  
   {  
     if(index >= 0 && index < files.size()){  
       String filename = files.get(index);  
       System.out.println(filename);  
     }  
   }  
   //menghapus file di koleksi  
   public void removeFile(int index)  
   {  
     if(index >= 0 && index < files.size()){  
       files.remove(index);  
     }  
   }  
   //memainkan file musik di koleksi  
   public void startPlaying(int index)  
   {  
     String filename = files.get(index);  
     player.startPlaying(filename);  
   }  
   //menghentikan player musik  
   public void stopPlaying()  
   {  
     player.stopPlaying();  
   }  
 }  


Class Music Player

 /**  
  *   
  *  
  * @author (Ismail)  
  * @version (14102018)  
  */  
 public class Musicplayer  
 {  
   private String music;  
   public Musicplayer()  
   {  
     music = null;  
   }  
   public void startPlaying(String filename)  
   {  
     music = filename;  
     System.out.println(music+ " Is Now Playing");  
   }  
   public void stopPlaying()  
   {  
     System.out.println("The Music Stop Playing");  
   }  
 }  


Hasil Output program

1. Menambahkan lagu ke playlist


2. Mengecek list lagu



3. Memainkan dan menghentikan lagu






Selasa, 09 Oktober 2018

Auction

Pada pertemuan kali ini, kami mendapatkan tugas untuk membuat program pelelangan. Berikut hasil dan source code-nya.







Auction

 import java.util.ArrayList;  
 /**  
  * class Auction  
  *  
  * @author (Ismail)  
  * @version (09102018)  
  */  
 public class Auction    
 {    
   private ArrayList<Lot> lots;   
   private int LotNumber;  
   //Membuat Auction baru  
   public Auction()    
   {    
     lots = new ArrayList<Lot>();   
     LotNumber = 1;    
   }  
   //Menambahkan barang baru  
   public void enterLot(String description)    
   {   
     lots.add(new Lot(LotNumber, description));    
     LotNumber++;    
   }  
   //Menampilkan barang yang dilelang  
   public void showLots()  
   {   
     for(Lot lot : lots) {    
      System.out.println(lot.detail());    
     }  
   }  
   //Melakukan tawaran  
   public void MakeBid(int CurrentlotNumber, Person bidder, long value)    
   {    
     Lot selectedLot = getLot(CurrentlotNumber);    
     if(selectedLot != null)  
     {  
       boolean succes = selectedLot.bidFor(new Bid(bidder, value));    
       if(succes)  
       {  
         System.out.println("The bid for lot number " +CurrentlotNumber+ " was successful.");  
       }  
       else  
       {  
         Bid highestBid = selectedLot.getHighestBid();    
         System.out.println("Lot number: " +CurrentlotNumber+ " already has a bid of: " +highestBid.getBid());    
       }  
     }  
   }  
   public Lot getLot(int CurrentlotNumber)    
   {  
     if((CurrentlotNumber >= 1) && (CurrentlotNumber < LotNumber))  
     {  
       Lot selectedLot = lots.get(CurrentlotNumber - 1);   
       if(selectedLot.getId() != CurrentlotNumber)  
       {  
         System.out.println("Internal error: Lot number " +selectedLot.getId()+ " was returned instead of " +CurrentlotNumber);   
         selectedLot = null;    
       }  
       return selectedLot;    
     }  
     else  
     {  
       System.out.println("Lot number : " + CurrentlotNumber +  " does not exist.");    
       return null;    
     }  
   }  
 }  


Person

 /**  
  * class Person  
  *  
  * @author (Ismail)  
  * @version (09102018)  
  */  
 public class Person  
 {  
   private final String name;  
   public Person(String newName)  
   {  
     this.name = newName;  
   }  
   public String getName()  
   {  
     return name;  
   }  
 }  


Lot

 /**  
  * Class Lot  
  *  
  * @author (Ismail)  
  * @version (09102018)  
  */  
 public class Lot  
 {  
   private final int Id;  
   private String description;  
   private Bid highestBid;  
   public Lot(int number, String description)  
   {  
     this.Id = number;  
     this.description = description;  
   }  
   public boolean bidFor(Bid bid)  
   {  
     if((highestBid ==null) || (bid.getBid() > highestBid.getBid()))  
     {  
       highestBid = bid;  
       return true;  
     }  
     else  
     {  
       return false;  
     }  
   }  
   public String detail()  
   {  
     String details = Id+ ": " +description;  
     if(highestBid != null)  
     {  
       details += " Bid: " +highestBid.getBid();  
     }  
     else  
     {  
       details += " (No Bid)";  
     }  
     return details;  
   }  
   public int getId()  
   {  
     return Id;  
   }  
   public String getDescription()  
   {  
     return description;  
   }  
   public Bid getHighestBid()  
   {  
     return highestBid;  
   }  
 }  


Bid

 /**  
  * class Bid  
  *  
  * @author (Ismail)  
  * @version (09102018)  
  */  
 public class Bid  
 {  
   private final Person bidder;  
   private final long value;  
   public Bid(Person bidder, long value)  
   {  
     this.bidder = bidder;  
     this.value = value;  
   }  
   public Person getBidder()  
   {  
     return bidder;  
   }  
   public long getBid()  
   {  
     return value;  
   }  
 }  

Minggu, 30 September 2018

Jam Digital

Berikut hasil dan source code-nya.




Executor

 /**  
  * Write a description of class Executor here.  
  *  
  * @author (Ismail)  
  * @version (01102018)  
  */  
 public class Executor  
 {  
   public static void main(String[]args)  
   {  
     new Executor();  
   }  
   public Executor()  
   {  
     new waktu();  
   }  
 }  


Waktu

 /**  
  * Write a description of class waktu here.  
  *  
  * @author (Ismail)  
  * @version (01102018)  
  */  
 import javax.swing.*;  
 import java.awt.*;  
 import java.awt.event.*;  
 import java.util.Calendar;  
 public class waktu extends JFrame  
 {  
   private static final long serialVersionUID = 1L;  
   JTextField timeF;  
   JPanel panel;  
   public waktu()  
   {  
     super("Java Clock by Ismail Arifin");  
     setSize(500,200);  
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     setVisible(true);  
     setResizable(true);  
     setLocationRelativeTo(null);  
     panel = new JPanel();  
     panel.setLayout(new FlowLayout());  
     timeF = new JTextField(10);  
     timeF.setEditable(false);  
     timeF.setFont(new Font("Arial",Font.PLAIN,50));  
     panel.add(timeF);  
     add(panel);  
     Timer t = new Timer(1000,new Listener());  
     t.start();  
   }  
   class Listener implements ActionListener  
   {  
     public void actionPerformed(ActionEvent e)  
     {  
       Calendar rightNow = Calendar.getInstance();  
       int hour = rightNow.get(Calendar.HOUR_OF_DAY);  
       int min = rightNow.get(Calendar.MINUTE);  
       int sec = rightNow.get(Calendar.SECOND);  
       timeF.setText(hour+":"+min+":"+sec);  
     }  
   }  
 }  

Minggu, 23 September 2018

Tugas 4 PBO

Pada perkuliahan minggu sebelumnya, kelas saya mendapatkan tugas untuk membuat projek remot tv menggunakan BlueJ. Berikut hasil beserta source code-nya.






Remote TV

 /**  
  * RemotTV  
  *  
  * @author (Ismail)  
  * @version (23/09/2018)  
  */  
 public class RemotTV  
 {  
   private int channel;  
   private int volume;  
   public RemotTV()  
   {  
     channel = 0;  
     volume = 50;  
   }  
   public void cekTV()  
   {  
     System.out.println("##########################");  
     System.out.println("# Anda sedang menonton #");  
     if(channel > 0 && channel <8)  
     {  
       switch(channel)  
       {  
         case 1:  
         System.out.println("#\tSCTV");  
         break;  
         case 2:  
         System.out.println("#\tINDOSIAR");  
         break;  
         case 3:  
         System.out.println("#\tRCTI");  
         break;  
         case 4:  
         System.out.println("#\tGLOBAL TV");  
         break;  
         case 5:  
         System.out.println("#\tTRANS TV");  
         break;  
         case 6:  
         System.out.println("#\tANTV");  
         break;  
         case 7:  
         System.out.println("#\tNET TV");  
         break;  
       }  
     }  
     else  
     {  
       System.out.println("# Channel tidak tersedia");  
     }  
     System.out.println("# Selamat Menyaksikan #");  
     System.out.println("##########################");  
   }  
   public int gantichannel(int nomor)  
   {  
     return channel=nomor;  
   }  
   public int cekvolume()  
   {  
     return volume;  
   }  
   public int tambahvolume()  
   {  
     return volume+=1;  
   }  
   public int kurangivolume()  
   {  
     return volume-=1;  
   }  
 }  


Main

 /**  
  * Main.  
  *  
  * @author (Ismail)  
  * @version (23/09/2018)  
  */  
 import java.util.Scanner;  
 public class Main  
 {  
   public static void main(String args[])  
   {  
     Scanner scan = new Scanner(System.in);  
     int menu, channel, volume;  
     RemotTV remot = new RemotTV();  
     System.out.println("1. Nonton TV");  
     System.out.println("2. Ganti Channel");  
     System.out.println("3. Cek Volume");  
     System.out.println("4. Tambah Volume");  
     System.out.println("5. Kurangi Volume");  
     System.out.println("6. Matikan TV");  
     int tipe=1;  
     while(tipe == 1)  
     {  
       menu = scan.nextInt();  
       switch(menu)  
       {  
         case 1:  
         remot.cekTV();  
         break;  
         case 2:  
         System.out.println("Pilih channel yang ingin ditonton");  
         System.out.println("1 - SCTV");  
         System.out.println("2 - INDOSIAR");                  
         System.out.println("3 - RCTI");          
         System.out.println("4 - GLOBAL TV");          
         System.out.println("5 - TRANS TV");          
         System.out.println("6 - ANTV");          
         System.out.println("7 - NET TV");  
         int angka = scan.nextInt();  
         remot.gantichannel(angka);  
         break;  
         case 3:  
         System.out.println("Volume : " +remot.cekvolume()+ "\n");  
         break;  
         case 4:  
         int nilai = remot.cekvolume();  
         if(nilai < 100)  
         System.out.println("Volume : " +remot.tambahvolume()+ "\n");  
         break;  
         case 5:  
         int nilai_ = remot.cekvolume();  
         if(nilai_ > 0)  
         System.out.println("Volume : " +remot.kurangivolume()+ "\n");  
         break;  
         case 6:  
         tipe = 0;  
         System.out.println("##############");  
         System.out.println("#TV Dimatikan#");  
         System.out.println("##############");  
         break;  
       }  
     }  
   }  
 }  

Senin, 17 September 2018

Tugas 3 PBO

Minggu ke-3 Perkuliahan, membuat class dan object studi kasus ticket machine menggunakan BlueJ. Berikut hasil dan source code-nya.




Ticket Machine

 /**  
  * Ticket Machine  
  *  
  * @author (Ismail)  
  * @version (16092018)  
  */  
 public class TicketMachine  
 {  
   private int price;  
   private int balance;  
   private int total;  
   public TicketMachine(int ticketCost)  
   {  
     price = ticketCost;  
     balance = 0;  
     total = 0;  
   }  
   public int getPrice()  
   {  
     return price;  
   }  
   public int getBalance()  
   {  
     return balance;  
   }  
   public void insertMoney(int amount)  
   {  
     balance += amount;  
   }  
   public void printTicket()  
   {  
     int bayar = balance;  
     balance = balance - price;  
     System.out.println("####################");  
     System.out.println("# Cinema XVIII");  
     System.out.println("# Tiket");  
     System.out.println("# " + price + " Rupiah");  
     System.out.println("Bayar "+bayar+" Rupiah");  
     System.out.println("Kembalian "+balance+" Rupiah");  
     System.out.println("####################");  
     System.out.println();  
     total = total + balance;  
     balance = 0;  
   }  
 }  


Main

 /**  
  * Main  
  *  
  * @author (Ismail)  
  * @version (16092018)  
  */  
 import java.util.Scanner;  
 public class IntMain  
 {  
   public static void main(String args[])  
   {  
     Scanner scan = new Scanner(System.in);  
     int cost, menu;  
     System.out.println("Masukkan harga tiket \n");  
     cost = scan.nextInt();  
     TicketMachine ticket = new TicketMachine(cost);  
     System.out.println("1. Get Price");  
     System.out.println("2. Get Balance");  
     System.out.println("3. Insert Money");  
     System.out.println("4. Print Ticket");  
     System.out.println("5. Exit");  
     int tipe=1;  
     while(tipe == 1)  
     {  
       menu = scan.nextInt();  
       switch(menu)  
       {  
         case 1:  
         cost = ticket.getPrice();  
         System.out.println(cost);  
         break;  
         case 2:  
         System.out.print(ticket.getBalance()+"\n");  
         break;  
         case 3:  
         int money = scan.nextInt();  
         ticket.insertMoney(money);  
         break;  
         case 4:  
         ticket.printTicket();  
         break;  
         case 5:  
         tipe=0;  
         break;  
       }  
     }  
   }  
 }  

Minggu, 16 September 2018

Tugas 2 PBO

Pada pertemuan Minggu ke-3 sebelumnya, kami medapatkan tugas untuk membuat sebuah pemandangan menggunakan BlueJ. Berikut hasil dan source code-nya.




Picture

 /**  
  * This class represents a simple picture. You can draw the picture using  
  * the draw method. But wait, there's more: being an electronic picture, it  
  * can be changed. You can set it to black-and-white display and back to  
  * colors (only after it's been drawn, of course).  
  *  
  * This class was written as an early example for teaching Java with BlueJ.  
  *   
  * @author Michael Kölling and David J. Barnes  
  * @version 1.1 (24 May 2001)  
  */  
 public class Picture  
 {  
   private Square kotak;  
   private Triangle gunung1;  
   private Triangle gunung2;  
   private Triangle jalan;  
   private Circle sun;  
   private Circle awan1;  
   private Circle awan2;  
   private Circle awan3;  
   private Circle awan4;  
   private Circle awan5;  
   private Square rumah1;  
   private Square rumah2;  
   private Triangle rumah3;  
   /**  
    * Constructor for objects of class Picture  
    */  
   public Picture()  
   {  
     // nothing to do... instance variables are automatically set to null  
   }  
   /**  
    * Draw this picture.  
    */  
   public void draw()  
   {  
     kotak = new Square();  
     kotak.changeColor("blue");  
     kotak.moveVertical(-150);  
     kotak.moveHorizontal(0);  
     kotak.changeSize(300);  
     kotak.makeVisible();  
     jalan = new Triangle();  
     jalan.changeColor("black");  
     jalan.moveVertical(140);  
     jalan.moveHorizontal(140);  
     jalan.changeSize(170, 70);  
     jalan.makeVisible();  
     sun = new Circle();  
     sun.changeColor("yellow");  
     sun.moveVertical(100);  
     sun.moveHorizontal(130);  
     sun.changeSize(50);  
     sun.makeVisible();  
     gunung1 = new Triangle();  
     gunung1.changeColor("green");  
     gunung1.moveVertical(100);  
     gunung1.moveHorizontal(80);  
     gunung1.changeSize(60, 200);  
     gunung1.makeVisible();  
     gunung2 = new Triangle();  
     gunung2.changeColor("green");  
     gunung2.moveVertical(100);  
     gunung2.moveHorizontal(220);  
     gunung2.changeSize(60, 200);  
     gunung2.makeVisible();  
     awan1 = new Circle();  
     awan1.changeColor("white");  
     awan1.moveVertical(40);  
     awan1.moveHorizontal(100);  
     awan1.changeSize(40);  
     awan1.makeVisible();  
     awan2 = new Circle();  
     awan2.changeColor("white");  
     awan2.moveVertical(40);  
     awan2.moveHorizontal(80);  
     awan2.changeSize(37);  
     awan2.makeVisible();  
     awan3 = new Circle();  
     awan3.changeColor("white");  
     awan3.moveVertical(40);  
     awan3.moveHorizontal(80);  
     awan3.changeSize(35);  
     awan3.makeVisible();  
     awan4 = new Circle();  
     awan4.changeColor("white");  
     awan4.moveVertical(40);  
     awan4.moveHorizontal(200);  
     awan4.changeSize(30);  
     awan4.makeVisible();  
     awan5 = new Circle();  
     awan5.changeColor("white");  
     awan5.moveVertical(40);  
     awan5.moveHorizontal(220);  
     awan5.changeSize(30);  
     awan5.makeVisible();  
     rumah1 = new Square();  
     rumah1.changeColor("red");  
     rumah1.moveVertical(240);  
     rumah1.moveHorizontal(20);  
     rumah1.changeSize(70);  
     rumah1.makeVisible();  
     rumah2 = new Square();  
     rumah2.changeColor("black");  
     rumah2.moveVertical(260);  
     rumah2.moveHorizontal(45);  
     rumah2.changeSize(20);  
     rumah2.makeVisible();  
     rumah3 = new Triangle();  
     rumah3.changeColor("brown");  
     rumah3.moveVertical(200);  
     rumah3.moveHorizontal(55);  
     rumah3.changeSize(50, 100);  
     rumah3.makeVisible();  
   }  
 }  


Canvas

 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
 /**  
  * Canvas is a class to allow for simple graphical drawing on a canvas.  
  * This is a modification of the general purpose Canvas, specially made for  
  * the BlueJ "shapes" example.   
  *  
  * @author: Bruce Quig  
  * @author: Michael Kölling (mik)  
  *  
  * @version: 1.6 (shapes)  
  */  
 public class Canvas  
 {  
   // Note: The implementation of this class (specifically the handling of  
   // shape identity and colors) is slightly more complex than necessary. This  
   // is done on purpose to keep the interface and instance fields of the  
   // shape objects in this project clean and simple for educational purposes.  
   private static Canvas canvasSingleton;  
   /**  
    * Factory method to get the canvas singleton object.  
    */  
   public static Canvas getCanvas()  
   {  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300,   
           Color.white);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  
   // ----- instance part -----  
   private JFrame frame;  
   private CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColour;  
   private Image canvasImage;  
   private List<Object> objects;  
   private HashMap<Object, ShapeDescription> shapes;  
   /**  
    * Create a Canvas.  
    * @param title title to appear in Canvas Frame  
    * @param width the desired width for the canvas  
    * @param height the desired height for the canvas  
    * @param bgClour the desired background colour of the canvas  
    */  
   private Canvas(String title, int width, int height, Color bgColour)  
   {  
     frame = new JFrame();  
     canvas = new CanvasPane();  
     frame.setContentPane(canvas);  
     frame.setTitle(title);  
     canvas.setPreferredSize(new Dimension(width, height));  
     backgroundColour = bgColour;  
     frame.pack();  
     objects = new ArrayList<Object>();  
     shapes = new HashMap<Object, ShapeDescription>();  
   }  
   /**  
    * Set the canvas visibility and brings canvas to the front of screen  
    * when made visible. This method can also be used to bring an already  
    * visible canvas to the front of other windows.  
    * @param visible boolean value representing the desired visibility of  
    * the canvas (true or false)   
    */  
   public void setVisible(boolean visible)  
   {  
     if(graphic == null) {  
       // first time: instantiate the offscreen image and fill it with  
       // the background colour  
       Dimension size = canvas.getSize();  
       canvasImage = canvas.createImage(size.width, size.height);  
       graphic = (Graphics2D)canvasImage.getGraphics();  
       graphic.setColor(backgroundColour);  
       graphic.fillRect(0, 0, size.width, size.height);  
       graphic.setColor(Color.black);  
     }  
     frame.setVisible(visible);  
   }  
   /**  
    * Draw a given shape onto the canvas.  
    * @param referenceObject an object to define identity for this shape  
    * @param color      the color of the shape  
    * @param shape      the shape object to be drawn on the canvas  
    */  
    // Note: this is a slightly backwards way of maintaining the shape  
    // objects. It is carefully designed to keep the visible shape interfaces  
    // in this project clean and simple for educational purposes.  
   public void draw(Object referenceObject, String color, Shape shape)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     objects.add(referenceObject);   // add at the end  
     shapes.put(referenceObject, new ShapeDescription(shape, color));  
     redraw();  
   }  
   /**  
    * Erase a given shape's from the screen.  
    * @param referenceObject the shape object to be erased   
    */  
   public void erase(Object referenceObject)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     shapes.remove(referenceObject);  
     redraw();  
   }  
   /**  
    * Set the foreground colour of the Canvas.  
    * @param newColour  the new colour for the foreground of the Canvas   
    */  
   public void setForegroundColor(String colorString)  
   {  
     if(colorString.equals("red"))  
       graphic.setColor(Color.red);  
     else if(colorString.equals("black"))  
       graphic.setColor(Color.black);  
     else if(colorString.equals("blue"))  
       graphic.setColor(Color.blue);  
     else if(colorString.equals("yellow"))  
       graphic.setColor(Color.yellow);  
     else if(colorString.equals("green"))  
       graphic.setColor(Color.green);  
     else if(colorString.equals("magenta"))  
       graphic.setColor(Color.magenta);  
     else if(colorString.equals("white"))  
       graphic.setColor(Color.white);  
     else  
       graphic.setColor(Color.black);  
   }  
   /**  
    * Wait for a specified number of milliseconds before finishing.  
    * This provides an easy way to specify a small delay which can be  
    * used when producing animations.  
    * @param milliseconds the number   
    */  
   public void wait(int milliseconds)  
   {  
     try  
     {  
       Thread.sleep(milliseconds);  
     }   
     catch (Exception e)  
     {  
       // ignoring exception at the moment  
     }  
   }  
   /**  
    * Redraw ell shapes currently on the Canvas.  
    */  
   private void redraw()  
   {  
     erase();  
     for(Iterator i=objects.iterator(); i.hasNext(); ) {  
       ((ShapeDescription)shapes.get(i.next())).draw(graphic);  
     }  
     canvas.repaint();  
   }  
   /**  
    * Erase the whole canvas. (Does not repaint.)  
    */  
   private void erase()  
   {  
     Color original = graphic.getColor();  
     graphic.setColor(backgroundColour);  
     Dimension size = canvas.getSize();  
     graphic.fill(new Rectangle(0, 0, size.width, size.height));  
     graphic.setColor(original);  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class CanvasPane extends JPanel  
   {  
     public void paint(Graphics g)  
     {  
       g.drawImage(canvasImage, 0, 0, null);  
     }  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class ShapeDescription  
   {  
     private Shape shape;  
     private String colorString;  
     public ShapeDescription(Shape shape, String color)  
     {  
       this.shape = shape;  
       colorString = color;  
     }  
     public void draw(Graphics2D graphic)  
     {  
       setForegroundColor(colorString);  
       graphic.fill(shape);  
     }  
   }  
 }  


Triangle

 import java.awt.*;  
 /**  
  * A triangle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Michael Kölling and David J. Barnes  
  * @version 1.0 (15 July 2000)  
  */  
 public class Triangle  
 {  
   private int height;  
   private int width;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new triangle at default position with default color.  
    */  
   public Triangle()  
   {  
     height = 30;  
     width = 40;  
     xPosition = 0;  
     yPosition = 0;  
     color = "green";  
     isVisible = false;  
   }  
   /**  
    * Make this triangle visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this triangle invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the triangle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the triangle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the triangle a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the triangle a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the triangle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the triangle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the triangle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the triangle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newHeight, int newWidth)  
   {  
     erase();  
     height = newHeight;  
     width = newWidth;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the triangle with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };  
       int[] ypoints = { yPosition, yPosition + height, yPosition + height };  
       canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the triangle on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  


Square

 import java.awt.*;  
 /**  
  * A square that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Michael Kölling and David J. Barnes  
  * @version 1.0 (15 July 2000)  
  */  
 public class Square  
 {  
   private int size;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new square at default position with default color.  
    */  
   public Square()  
   {  
     size = 30;  
     xPosition = 0;  
     yPosition = 0;  
     color = "red";  
     isVisible = false;  
   }  
   /**  
    * Make this square visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this square invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the square a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the square a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the square a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the square a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the square horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the square vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the square horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the square vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newSize)  
   {  
     erase();  
     size = newSize;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the square with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color,  
           new Rectangle(xPosition, yPosition, size, size));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the square on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  


Circle

 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * A circle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Michael Kölling and David J. Barnes  
  * @version 1.0 (15 July 2000)  
  */  
 public class Circle  
 {  
   private int diameter;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new circle at default position with default color.  
    */  
   public Circle()  
   {  
     diameter = 30;  
     xPosition = 0;  
     yPosition = 0;  
     color = "blue";  
     isVisible = false;  
   }  
   /**  
    * Make this circle visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this circle invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the circle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the circle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the circle a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the circle a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the circle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the circle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the circle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the circle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newDiameter)  
   {  
     erase();  
     diameter = newDiameter;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the circle with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,   
           diameter, diameter));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the circle on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

EAS PBO B

1. Rancangan interface image editor. 2. Class diagram dari image editor yang akan dibuat terdiri atas 10 class seperti berikut. ...