⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipcity.java

📁 此源代码为java语音识别的开源系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Constructs the main Panel of this LiveFrame.
     *
     * @return the main Panel of this LiveFrame
     */
    private JPanel createMainPanel() {
        JPanel mainPanel = getJPanel(new FlowLayout(FlowLayout.LEFT));
        speakButton = new JButton("Speak");
        speakButton.setEnabled(false);
        speakButton.setMnemonic('s');
        // if this is continuous mode, don't add the speak button
        // since it is not necessary
        if (!continuousMode) {
            mainPanel.add(speakButton);
        }
        mainPanel.add(createMessagePanel());
        return mainPanel;
    }

    /**
     * Constructs the image Panel 
     *
     * @return the image panel
     */
    private JPanel createImagePanel() {
        JPanel panel = getJPanel(new FlowLayout());
        JLabel imageLabel = new JLabel(createImageIcon("s4.jpg", "s4-logo"));
        panel.add(imageLabel);
        return panel;
    }


    /**
     * Creates the map panel
     *
     * @return the map panel
     */
    private JPanel createMapPanel() {
        return new MapPanel();
    }

    /**
     * A panel that draws a map of the US based upon the
     * zip code database
     */
    class MapPanel extends JPanel {

        private final static float MIN_LAT = 24.0f;
        private final static float MAX_LAT = 52f;
        private final static float MAX_LONG = 126f;
        private final static float MIN_LONG = 65f;
        private final static float RANGE_LAT = MAX_LAT - MIN_LAT;
        private final static float RANGE_LONG = MAX_LONG - MIN_LONG;
        private final static int NORMAL_SIZE = 2;
        private final static int HIGHLIGHT_SIZE = 10;

        private final static float DEFAULT_LAT = 25;
        private final static float DEFAULT_LONG = 120;

        private final static int WIDTH_OFFSET = 10;
        private final static int HEIGHT_OFFSET = 8;
        private final static int MARGIN = 10;
        private Graphics g; 
        private Dimension size;


        /**
         * Creates the map panel
         */
        MapPanel() {
            setFont(labelFont);
            setBackground(backgroundColor);
        }


        /**
         * Updates the map
         */
        public void paintComponent(Graphics graphics) {
            g = graphics;
            size = getSize();
            super.paintComponent(g);
            if (zipDB != null) {
                g.setColor(NORM_COLOR);
                for (Iterator i = zipDB.iterator();  i.hasNext(); ) {
                    ZipInfo zi = (ZipInfo) i.next();
                    plot(zi, false);
                }
                if (currentInfo != null) {
                    g.setColor(HIGHLIGHT_COLOR);
                    plot(currentInfo, true);
                }

            }
        }

        /**
         * Plots a zip info with given size
         *
         * @param zi the zip info to plot
         * @param highlight if true this info should be highlighted
         *
         */
        void plot(ZipInfo zi, boolean highlight) {
            //System.out.println("ll " + zi.getLongitude() + " " +
            //       zi.getLatitude());
            if (highlight) {
                drawZipInfo(zi);
            } else {
                int x = mapx(zi.getLongitude());
                int y = mapy(zi.getLatitude());
                plot(x,y, NORMAL_SIZE);
            }
        }

        /**
         * Draws the zip info on the map
         *
         * @param zi the zip info
         */
        void drawZipInfo( ZipInfo zi) {
            int x, y;
            String label = zi.getCity() + ", " + zi.getState() + " " +
                zi.getZip();
            Dimension d = getStringDimension(g, label);

            if (zi.getLatitude() < MIN_LAT || zi.getLatitude() > MAX_LAT 
                    || zi.getLongitude() < MIN_LONG ||
                    zi.getLongitude() > MAX_LONG) {
                x = mapx(DEFAULT_LONG);
                y = mapy(DEFAULT_LAT);
            } else {
                x = mapx(zi.getLongitude());
                y = mapy(zi.getLatitude());
                g.fillOval(x, y, HIGHLIGHT_SIZE, HIGHLIGHT_SIZE);
            }
            int xpos = x - WIDTH_OFFSET;
            int ypos = y - d.height / 2;

            if (xpos + d.width + MARGIN > size.width) {
                xpos -= (xpos + d.width - size.width) + MARGIN;
            }
            g.drawString(label, xpos, ypos);
        }

        /**
         * plot a point with the given pixel size
         *
         * @param x the x position of the point
         * @param y the y position of the point
         * @param size the size of the point
         */
        void plot(int x, int y, int size) {
             // g.fillOval(x, y, size, size);
             g.fillRect(x, y, size, size);
        }

        /**
         * Maps the given x position to the map panel
         * @param x the x position
         *
         * @return the x position mapped onto the map panel
         */
        private int mapx(float x) {
            return size.width - 
                (int) (size.width * (x - MIN_LONG) / RANGE_LONG);
        }

        /**
         * Maps the given y position to the map panel
         * @param x the y position
         *
         * @return the y position mapped onto the map panel
         */
        private int mapy(float y)  {
            return size.height - 
                (int) (size.height * (y - MIN_LAT) / RANGE_LAT);
        }


        /**
         * Gets the width and height in pixels of the given string
         *
         * @param g the current graphics context
         * @param s the string of interest
         * @return the dimension, in pixels of the string
         */
        private Dimension getStringDimension(Graphics g, String s) {
            FontMetrics fm = g.getFontMetrics();
            Rectangle2D r2d = fm.getStringBounds(s, g);
            return new Dimension((int) (r2d.getWidth() + .5),
                                 (int) (r2d.getHeight() + .5));
        }
    }

    /**
     * Creates a Panel that contains a label for messages.
     * This Panel should be located at the bottom of this Frame.
     *
     * @return a Panel that contains a label for messages
     */
    private JPanel createMessagePanel() {
        JPanel messagePanel = getJPanel(new BorderLayout());
        messageTextField = new JTextField
            ("Please wait while I'm loading...", 40);
        messageTextField.setBackground(backgroundColor);
        messageTextField.setForeground(HIGHLIGHT_COLOR);
        messageTextField.setEditable(false);
        messageTextField.setBorder(new EmptyBorder(1,1,1,1));
        messageTextField.setFont(labelFont);
        messagePanel.add(messageTextField, BorderLayout.CENTER);
        return messagePanel;
    }

    /** 
     * Returns an ImageIcon, or null if the path was invalid. 
     *
     * @param path the path to the image resource.
     * @param description a description of the resource
     *
     * @return the image icon or null if the resource could not be
     * found.
     */
    protected ImageIcon createImageIcon(String path, String description) {
        URL imgURL = ZipCity.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            return null;
        }
    }

    /**
     * Displays the usage message
     */
    private static void usage() {
        System.out.println("Usage:  ZipCity [-continuous] [-help]");
    }

    /**
     * The main program for zip city.  Creates the ZipCity frame,
     * displays it, and  runs it.
     *
     * Usage:
     *
     *   java -mx200m ZipCity [-continuous]
     *
     * @param args program arguments 
     */
    public static void main(String[] args) {
        boolean continuous = false;

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-continuous")) {
                continuous = true;
            } else {
                usage();
                System.exit(0);
            }
        }

        ZipCity zipCity = new ZipCity(continuous);
        zipCity.setVisible(true);
        zipCity.go();
    }
}


⌨️ 快捷键说明

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