Priscilliaagnes's Blog

May 21, 2010

OOP week 14

Filed under: Uncategorized — Priscillia @ 12:48 pm

Pada hari Kamis, 20 Mei 2010 pk 8.20-11.40 diadakan kelas OOP week 14. Pada pertemuan itu mengerjakan project untuk semester 2 mengenai Rush Hour. Dan project tesebut harus sudah jadi sampai bergerak. Hasil codingnya adalah sebagai berikut:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GarageGUI {

 int n = 5;
 private JFrame frame;
 private JPanel contentPane, garagePanel, mainPanel, actionPanel;
 private JButton doButton;
 private JComboBox vehicleCombo, directionCombo;
 private JLabel stepLabel;
 private JTextField stepField;
 private char[][] map;
 private List listOfVehicle = new ArrayList();
 private JButton buttons;
 private Color[] colors = {Color.WHITE, Color.BLUE, Color.CYAN, Color.GREEN,
 Color.MAGENTA, Color.ORANGE, Color.PINK, Color.CYAN, Color.RED,
 Color.LIGHT_GRAY, Color.YELLOW};
 private String[] variables = {"i", "c", "u", "f", "g", "e", "b"};
 private String[] directions = {"up", "down", "left", "right"};
 private Color buttonColor;
 private EventHandler handler = new EventHandler();

 public String getMovement(ArrayList coordinate1, ArrayList coordinate2) {
 String movement = "";
 if (coordinate1.get(0) == coordinate2.get(0)) {
 movement = "Horizontal";
 } else if (coordinate1.get(1) == coordinate2.get(1)) {
 movement = "Vertical";
 }
 return movement;

 }

 public String cek(Object mobilku) {
 ArrayList coordinateCar = new ArrayList();
 ArrayList xy = new ArrayList();
//        int[] xy = new int[2];
 for (int i = 0; i < n; i++) {
 for (int j = 0; j < n; j++) {
 if (map[i][j] == mobilku) {
//                    System.out.println("ij = " + i + " " + j);
 xy = new ArrayList();
 xy.add(i);
 xy.add(j);
 coordinateCar.add(xy);
 }
 }
 }
 String movement = "";
 if (coordinateCar.size() == 1) {
 movement = "horizontal-vertical";
 } else {
 movement = getMovement((ArrayList)coordinateCar.get(0),
 (ArrayList)coordinateCar.get(1));
 }
//        System.out.println("move: " + movement);
//        System.out.println("akhir");
 return movement;
 }

 public void initializeItems() {
 doButton = new JButton("DO");
 vehicleCombo = new JComboBox(variables);
 directionCombo = new JComboBox(directions);
 stepLabel = new JLabel("Step: ");
 stepField = new JTextField();
 }

 public void initializePanels() {
 mainPanel = new JPanel(new BorderLayout());
 actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
 garagePanel = new JPanel(new GridLayout(5, 5));
 contentPane = new JPanel(new BorderLayout(5, 5));
 }

 public void initialization() {
 initializeItems();
 initializePanels();
 }

 public void populateDefaultSetting() {
 }

 public void registerEventListener() {
 doButton.addActionListener(handler);
 }

 public void buildLayout() {
 int garageBorderCounter = -1;
 JButton grassButton;
 for (int i = 0; i < n; i++) {
 garageBorderCounter = 0;
 for (int j = 0; j < n + 2; j++) {
 if (garageBorderCounter < n) {

 if (map[i][j] != '0') {
 buttons = new JButton("" + String.valueOf(map[i][j]));
 garagePanel.add(buttons);
 buttonColor = getColorOfVehicle(map[i][j]);
 if (buttonColor != null) {
 buttons.setForeground(Color.BLACK);
 buttons.setBackground(buttonColor);
 buttons.setEnabled(false);
 }
 garagePanel.add(buttons);
 } else {
 garagePanel.add(new JLabel());
 }
 } else {
 if (i != (n / 2)) {
 grassButton = new JButton("@");
 grassButton.setBackground(new Color(30, 30, 30));
 grassButton.setForeground(Color.WHITE);
 grassButton.setEnabled(false);
 garagePanel.add(grassButton);
 } else {
 grassButton = new JButton("*");
 grassButton.setEnabled(false);
 garagePanel.add(grassButton);
 }
 }
 garageBorderCounter++;
 }
 actionPanel.add(vehicleCombo);
 actionPanel.add(directionCombo);
 actionPanel.add(stepLabel);
 actionPanel.add(stepField);
 actionPanel.add(doButton);

 mainPanel.add(garagePanel, BorderLayout.CENTER);
 mainPanel.add(actionPanel, BorderLayout.SOUTH);

 contentPane.add(mainPanel, BorderLayout.CENTER);
 contentPane.add(new JPanel(), BorderLayout.SOUTH);
 contentPane.add(new JPanel(), BorderLayout.NORTH);
 contentPane.add(new JPanel(), BorderLayout.WEST);
 contentPane.add(new JPanel(), BorderLayout.EAST);
 }
 }

 public void buildGUI() {
 readFile();
 initialization();
 buildLayout();
 populateDefaultSetting();
 registerEventListener();
 frame = new JFrame("Rush Hour");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(370, 350);
 frame.setLocationRelativeTo(null);
 frame.setResizable(false);
 frame.setContentPane(contentPane);
 frame.setVisible(true);

 }

 public GarageGUI() {
 buildGUI();
 for (int x = 0; x < listOfVehicle.size(); x++) {
 System.out.println("" + listOfVehicle.get(x) + " move = " +
 cek(listOfVehicle.get(x)));
 }
 }

 public void readFile() {
 try {
 File file = new File("D:/map.txt");
 System.out.println("masuk");
 FileReader fr = new FileReader(file);
 BufferedReader br = new BufferedReader(fr);
 String s;
 int x = 0, y = 0, width;
 while (null != (s = br.readLine())) {
 StringTokenizer st = new StringTokenizer(s, ",");
 if (y == 0) {
 width = st.countTokens();
 map = new char[width][width];
 }
 x = 0;
 while (st.hasMoreTokens()) {
 map[y][x] = st.nextToken(",").charAt(0);
 if (!listOfVehicle.contains(map[y][x]) && map[y][x] != '0') {
 listOfVehicle.add(map[y][x]);
 }
 x++;
 }
 y++;
 }

 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public Color getColorOfVehicle(char x) {
 char a = '0';
 for (int i = 0; i < listOfVehicle.size(); i++) {
 a = listOfVehicle.get(i).toString().charAt(0);
 if (a == x) {
 return colors[i];
 }
 }
 return null;
 }

 public static void main(String[] args) {
 GarageGUI garageGUI = new GarageGUI();
 }

 private class EventHandler implements ActionListener {

 public void actionPerformed(ActionEvent e) {
 Object source = e.getSource();
 if (source != null && source == doButton) {
 String myVehicle = vehicleCombo.getSelectedItem().toString();
 String myDirection = directionCombo.getSelectedItem().toString();
 int myStep = -1;
 try {
 myStep = Integer.parseInt(stepField.getText());
 } catch (Exception ex) {
 myStep = 0;
 }

 System.out.println(myVehicle + " " + myDirection + " " + myStep);
 if (myStep > 0) {
 System.out.println("do someething");
 } else {
 System.out.println("illegal move");
 }
 }
 }
 }
}

Hasil tampilan dr hasil coding di atas adalah:

Sedangkan hasil outputnya adalah:

masuk
c move = Horizontal
d move = Horizontal
e move = horizontal-vertical
b move = Vertical
a move = Horizontal
f move = Horizontal
g move = Vertical
i move = horizontal-vertical

Demikian pelajaran OOP week 14 …. Week 14 juga merupakan week terakhir untuk kelas OOP.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: