seatingplanui.java

来自「SUN公司发布的SmartTicket 2.0蓝图」· Java 代码 · 共 422 行

JAVA
422
字号
/* * Copyright 2001, 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., the 'Java Smart Ticket * Sample Application', 'Java', 'Java'-based names, or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Java Smart Ticket Sample Application 1.2 graphics and images may not * be reproduced in any form, in whole or in part, by any means without * prior written authorization of Sun and its licensors, if any. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. * $Id: SeatingPlanUI.java,v 1.4 2003/06/02 08:01:32 ro89390 Exp $ */package com.sun.j2me.blueprints.smartticket.client.midp.ui;import javax.microedition.lcdui.*;import java.util.*;import com.sun.j2me.blueprints.smartticket.client.midp.util.ApplicationUtilities;import com.sun.j2me.blueprints.smartticket.shared.midp.model.Movie;import com.sun.j2me.blueprints.smartticket.shared.midp.model.SeatingPlan;import com.sun.j2me.blueprints.smartticket.client.midp.ui.UIController.EventIds;public class SeatingPlanUI extends Canvas implements CommandListener {    // Often-used colors.    private static final int WHITE = 0xffffff;    private static final int RED = 0xff6d00;    private static final int GREEN = 0x006d55;    private static final int BLUE = 0x0000ff;    private static final int BURGUNDY = 0xb60055;    private Command reserveCommand;    private Command buyCommand;    private Command cancelCommand;    private int currentRowNumber;    private int currentSeatNumber;    // Off-screen buffer.    private Image buffer;    // Canvas and individual seat dimensions.    private int canvasW, canvasH, seatH, seatW;    // Margins for the seating plan.    private int marginT, marginB, marginL, marginR;    // Is the cursor on?    private Timer timer;    private boolean cursorOn;    private UIController uiController;    private SeatingPlan seatingPlan;    /**     * @link dependency     */    /* # TicketPurchaseUI lnkTicketPurchaseUI; */    public SeatingPlanUI(UIController uiController) {        this.uiController = uiController;        cancelCommand =             new Command(uiController.getString(UIMessageCodes.CANCEL),                         Command.CANCEL, 1);        addCommand(cancelCommand);        reserveCommand =             new Command(uiController.getString(UIMessageCodes.RESERVE),                         Command.SCREEN, 1);        setCommandListener(this);    }    /**     * Initialize the seating canvas variables.     * @exception IOException  when some type of network error occurs.     */    public void init(SeatingPlan seatingPlan, Movie movie, int[] showTime) {        this.seatingPlan = seatingPlan;        removeCommand(reserveCommand);        canvasW = getWidth();        canvasH = getHeight();        buffer = Image.createImage(canvasW, canvasH);        Graphics g = buffer.getGraphics();        Font f = g.getFont();        // Paint the canvas background.        g.setColor(WHITE);        g.fillRect(0, 0, canvasW, canvasH);        // Draw static textual information on canvas.        g.setColor(BURGUNDY);        g.drawString(uiController.getString(UIMessageCodes.SCREEN), canvasW,                      0, Graphics.TOP | Graphics.RIGHT);        // FIXME        String titleText = movie.getTitle();        String showTimeText = ApplicationUtilities.showTimeToString(showTime);        int titleWidth = f.stringWidth(titleText);        int showTimeWidth = f.stringWidth(showTimeText);        if (titleWidth + showTimeWidth < canvasW) {            g.drawString(titleText, 0, canvasH,                          Graphics.BASELINE | Graphics.LEFT);        } else {            int targetWidth = canvasW - showTimeWidth;            int l = titleText.length() - 1;            while (l > 0) {                if (f.substringWidth(titleText, 0, l) < targetWidth) {                    break;                } else {                    l--;                }             }             g.drawString(titleText.substring(0, l), 0, canvasH,                          Graphics.BASELINE | Graphics.LEFT);        }         g.setColor(BLUE);        g.drawString(showTimeText, canvasW, canvasH,                      Graphics.BASELINE | Graphics.RIGHT);        // Adjust left and top margins to center seating plan.        marginT = f.getHeight() + (canvasH % seatingPlan.getRowCount()) / 2;        marginB = f.getHeight();        marginL = (canvasW % seatingPlan.getRowLength()) / 2;        marginR = 0;        // Calculate seat width and height.        seatW = (canvasW - (marginL + marginR)) / seatingPlan.getRowLength();        seatH = (canvasH - (marginT + marginB)) / seatingPlan.getRowCount();        // Position the cursor on the first empty seat.        for (int r = 0; r != seatingPlan.getRowCount(); r++) {            for (int s = 0; s != seatingPlan.getRowLength(); s++) {                if (seatingPlan.getStatusOf(r, s) == SeatingPlan.AVAILABLE) {                    currentRowNumber = r;                    currentSeatNumber = s;                }             }         }         // Draw the seating plan.        for (int r = 0; r != seatingPlan.getRowCount(); r++) {            for (int s = 0; s != seatingPlan.getRowLength(); s++) {                drawSeat(r, s);            }         }         drawSeatID();    }     public void commandAction(Command command, Displayable displayable) {        if (command == reserveCommand) {            uiController.handleEvent(EventIds.EVENT_ID_SEATS_SELECTED,                                      new Object[] {                seatingPlan.getBookedSeats()            });        } else if (command == cancelCommand) {            uiController.handleEvent(EventIds.EVENT_ID_SEAT_SELECTION_CANCELLED);        } else {            uiController.commandAction(command, displayable);        }     }     /**     * Draw individual seat with current status.     */    void drawSeat(int rowNumber, int seatNumber) {        drawSeat(rowNumber, seatNumber, -1);    }     /**     * Draw individual seat with over-riding color.     * @param  color the color to draw seat.     */    synchronized void drawSeat(int rowNumber, int seatNumber, int color) {        int x = marginL + seatNumber * seatW;        int y = marginT + rowNumber * seatH;        // If no color was specified, pick an appropriate color.        if (color == -1) {            switch (seatingPlan.getStatusOf(rowNumber, seatNumber)) {            case SeatingPlan.AVAILABLE:                color = WHITE;                break;            case SeatingPlan.BOOKED:                color = BLUE;                break;            case SeatingPlan.UNAVAILABLE:                color = RED;                break;            default:                return;            }        }         // Paint the seat and draw its border.        if (seatingPlan.getStatusOf(rowNumber, seatNumber)                 != SeatingPlan.AISLE) {            Graphics g = buffer.getGraphics();            g.setColor(color);            g.fillRect(x, y, seatW, seatH);            g.setColor(GREEN);            g.drawRect(x, y, seatW, seatH);            repaint(x, y, seatW, seatH);        }     }     /**     * Draw the position on the screen.     */    synchronized void drawSeatID() {        Graphics g = buffer.getGraphics();        Font f = g.getFont();        String pos = "R" + currentRowNumber + " S" + currentSeatNumber;        g.setColor(WHITE);        g.fillRect(0, 0, f.stringWidth(pos + "  "), f.getHeight());        g.setColor(BLUE);        g.drawString(pos, 0, 0, Graphics.TOP | Graphics.LEFT);        repaint(0, 0, f.stringWidth(pos + "  "), f.getHeight());    }     /**     * Draw the cursor object.     */    void drawCursor() {        if (cursorOn) {            drawSeat(currentRowNumber, currentSeatNumber, BURGUNDY);        } else {            drawSeat(currentRowNumber, currentSeatNumber);        }     }     /**     * Book or unbook the given seat.     */    void toggleSeat(int rowNumber, int seatNumber) {        if (seatingPlan.getStatusOf(rowNumber, seatNumber)                 == SeatingPlan.BOOKED) {            seatingPlan.setStatusOf(rowNumber, seatNumber,                                     SeatingPlan.AVAILABLE);        } else if (seatingPlan.getStatusOf(rowNumber, seatNumber)                    == SeatingPlan.AVAILABLE) {            seatingPlan.setStatusOf(rowNumber, seatNumber,                                     SeatingPlan.BOOKED);        } else {            return;        }         if (seatingPlan.getBookedSeatsCount() > 0) {            addCommand(reserveCommand);        } else {            removeCommand(reserveCommand);        }     }     // Overridden methods    protected void paint(Graphics g) {        g.drawImage(buffer, 0, 0, Graphics.TOP | Graphics.LEFT);    }     protected void keyPressed(int keyCode) {        int oldRowNumber = currentRowNumber;        int oldSeatNumber = currentSeatNumber;        int action = getGameAction(keyCode);        switch (action) {        case Canvas.FIRE:            toggleSeat(currentRowNumber, currentSeatNumber);            break;        case Canvas.LEFT:            for (int s = (currentSeatNumber - 1); s >= 0; s--) {                int status = seatingPlan.getStatusOf(currentRowNumber, s);                if (status == SeatingPlan.BOOKED                         || status == SeatingPlan.AVAILABLE) {                    currentSeatNumber = s;                    break;                }             }             break;        case Canvas.RIGHT:            for (int s = (currentSeatNumber + 1);                     s < seatingPlan.getRowLength(); s++) {                int status = seatingPlan.getStatusOf(currentRowNumber, s);                if (status == SeatingPlan.BOOKED                         || status == SeatingPlan.AVAILABLE) {                    currentSeatNumber = s;                    break;                }             }             break;        case Canvas.UP:            for (int r = (currentRowNumber - 1); r >= 0; r--) {                int status = seatingPlan.getStatusOf(r, currentSeatNumber);                if (status == SeatingPlan.BOOKED                         || status == SeatingPlan.AVAILABLE) {                    currentRowNumber = r;                    break;                }             }             break;        case Canvas.DOWN:            for (int r = (currentRowNumber + 1);                     r < seatingPlan.getRowCount(); r++) {                int status = seatingPlan.getStatusOf(r, currentSeatNumber);                if (status == SeatingPlan.BOOKED                         || status == SeatingPlan.AVAILABLE) {                    currentRowNumber = r;                    break;                }             }             break;        default:            return;        }        // Draw new seat and re-draw old seat.        drawCursor();        drawSeat(oldRowNumber, oldSeatNumber);        drawSeatID();    }     protected void pointerPressed(int x, int y) {        // Get the clicked position based on x and y coordinates.        int rowNumber = (y - marginT) / seatH;        int seatNumber = (x - marginL) / seatW;        if (rowNumber < 0 || rowNumber >= seatingPlan.getRowCount()                 || seatNumber < 0                 || seatNumber >= seatingPlan.getRowLength()) {            return;        }         int oldRowNumber = currentRowNumber;        int oldSeatNumber = currentSeatNumber;        int currentStatus = seatingPlan.getStatusOf(currentRowNumber,                                                     currentSeatNumber);        if (currentStatus == SeatingPlan.AVAILABLE                 || currentStatus == SeatingPlan.BOOKED) {            currentRowNumber = rowNumber;            currentSeatNumber = seatNumber;            toggleSeat(currentRowNumber, currentSeatNumber);            // Draw new seat and re-draw old seat.            drawCursor();            drawSeat(oldRowNumber, oldSeatNumber);            drawSeatID();        }     }     protected void showNotify() {        // Start the timer for the cursor.        timer = new Timer();        TimerTask task = new TimerTask() {            public void run() {                cursorOn = !cursorOn;                drawCursor();            }         };        timer.schedule(task, 750, 750);    }     protected void hideNotify() {        // Stop the timer for the cursor.        timer.cancel();    } }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?