<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-16  評論-50  文章-2  trackbacks-0

    首先做一道改錯題吧。

    下面的程序是個簡化的彈珠游戲。每按一下 Start 按鈕,就從左上角彈出一個球,并開始在球框中運動和彈跳。界面如下:

    bounce 
    圖1

    但運行下面的程序,你會發現一旦按了Start 按鈕,整個界面就像死機一樣,球不動,對界面的任何操作也無反應。這肯定不是程序想要的效果。為什么會這樣呢?要怎么辦呢?
     

    源代碼

    下載地址:http://m.tkk7.com/Files/jeff-lau/bounce_nothread.zip

    /**
    * @(#) Bounce.java 2007-12-31
    *
    * Copyright? 2007 Jeff. 該源代碼遵循BSD開源協議。
    */
    package joj.thread.swing.bounce.nothread;

    import javax.swing.JFrame;

    /**
    * 這是Bounce程序的啟動類。
    *
    * @since 5.0
    * @author Jeff
    */
    public class Bounce {

        /**
         * 程序啟動函數。
         *
         * @param args
         *            不理睬該參數
         */
        public static void main(String[] args) {
            JFrame frame = new BounceFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

    }

     

    /**
    * @(#) BounceFrame.java 2007-12-31
    *
    * Copyright? 2007 Jeff. 該源代碼遵循BSD開源協議。
    */
    package joj.thread.swing.bounce.nothread;

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    /**
    * Bounce程序的主窗體
    *
    * @author Jeff
    *
    */
    @SuppressWarnings("serial")
    public class BounceFrame extends JFrame {

        private static final int DEFAULT_HEIGHT = 300;
        private static final int DEFAULT_WIDTH = 400;

        private BallPanel ballPanel;

        public BounceFrame() throws HeadlessException {
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            add(getBallPanel(), BorderLayout.CENTER);
            add(getButtonPanel(), BorderLayout.SOUTH);
        }

        private Component getBallPanel() {
            if (ballPanel == null) {
                ballPanel = new BallPanel();
            }
            return ballPanel;
        }

        private Component getButtonPanel() {
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(getStartButton());
            buttonPanel.add(getCloseButton());
            return buttonPanel;
        }

        private Component getStartButton() {
            JButton startButton = new JButton("Start");

            startButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    addBall();
                }
            });

            return startButton;
        }

        private Component getCloseButton() {
            JButton closeButton = new JButton("Close");

            closeButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }

            });

            return closeButton;
        }

        private void addBall() {
            Ball ball = ballPanel.addBall();

            try {
                for (int i = 0; i < 1000; i++) {
                    ball.move(ballPanel.getBounds());
                    ballPanel.paintComponent(ballPanel.getGraphics());
                    Thread.sleep(3);
                }
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
    * @(#) BallPanel.java 2007-12-31
    *
    * Copyright? 2007 Jeff.
    * 該源代碼遵循BSD開源協議。
    */
    package joj.thread.swing.bounce.nothread;

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.JPanel;

    @SuppressWarnings("serial")
    public class BallPanel extends JPanel {

        private List balls = new ArrayList();

        public Ball addBall() {
            Ball ball = new Ball();
            balls.add(ball);
            return ball;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            for (Ball ball : balls) {
                g2.fill(ball.getShape());
            }
        }

    }


    /**
    * @(#) Ball.java 2007-12-31
    *
    * Copyright? 2007 Jeff. 該源代碼遵循BSD開源協議。
    */
    package joj.thread.swing.bounce.nothread;

    import java.awt.Shape;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;

    public class Ball {
       
        private double left = 0;
        private double top = 0;
        private double width = 15;
        private double height = 15;
        private double dx = 5;
        private double dy = 5;

        public Shape getShape() {
            return new Ellipse2D.Double(left, top, width, height);
        }

        public void move(Rectangle2D bounds) {
            left += dx;
            top += dy;

            if (left < bounds.getMinX()) {
                left = bounds.getMinX();
                dx = -dx;
            }
           
            if (left + width > bounds.getMaxX()) {
                left = bounds.getMaxX() - width;
                dx = -dx;
            }       

            if (top < bounds.getMinY()) {
                top = bounds.getMinY();
                dy = -dy;
            }

            if (top + height > bounds.getMaxY()) {
                top = bounds.getMaxY() - height;
                dy = -dy;
            }

        }
    }
    posted on 2007-12-31 22:00 Jeff Lau 閱讀(367) 評論(0)  編輯  收藏 所屬分類: 跟老劉學Java
    主站蜘蛛池模板: 少妇亚洲免费精品| 全部一级一级毛片免费看| 又粗又大又黑又长的免费视频| 91久久亚洲国产成人精品性色| 久久久久成人片免费观看蜜芽 | 亚洲国产V高清在线观看| 日韩色日韩视频亚洲网站| 国产hs免费高清在线观看| 黄页网站在线观看免费| 亚洲美女在线国产| 成人无码区免费A∨直播| 亚洲国产另类久久久精品小说| 久久国产乱子精品免费女| 亚洲国产综合专区在线电影| 亚洲视频免费在线播放| 亚洲精品美女在线观看播放| 3344免费播放观看视频| 亚洲a级成人片在线观看| 99热在线精品免费全部my| 在线观看亚洲AV日韩AV| 国产精品jizz在线观看免费 | 国产乱子精品免费视观看片| 亚洲av无码一区二区三区天堂古代| 大地资源二在线观看免费高清| 亚洲s码欧洲m码吹潮| 免费国产在线观看| 国产高清对白在线观看免费91 | 日韩精品无码免费视频| 综合亚洲伊人午夜网| 华人在线精品免费观看| 亚洲美女视频网站| 成全影视免费观看大全二| 国产亚洲午夜精品| 激情97综合亚洲色婷婷五| 午夜精品射精入后重之免费观看| 亚洲成人免费电影| 暖暖日本免费在线视频| 无码人妻一区二区三区免费视频| 亚洲精品无码不卡在线播放HE| 桃子视频在线观看高清免费视频| 国产婷婷综合丁香亚洲欧洲|