首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

把canvas 改成 Jpanel, 就不会主动刷新了,为什么解决方案

2012-01-31 
把canvas 改成 Jpanel, 就不会主动刷新了,为什么Java codeimport javax.swing.JFrameimport javax.swing.

把canvas 改成 Jpanel, 就不会主动刷新了,为什么

Java code
import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Graphics;import java.awt.Dimension;import java.awt.Color;import java.awt.Font;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.Timer;import java.util.Random;public class SwingPinball {    // panel    private static final int TABLE_WIDTH = 480;    private static final int TABLE_HEIGHT = 640;    // plank    private static final int PLANK_WIDTH = 60;    private static final int PLANK_HEIGHT = 15;    private static final int PLANK_Y = 600;    // ball      private static final int BALL_2R = 15;        // Random  plank & ball x,y    private Random rand = new Random();    private int nPlank_X = rand.nextInt(TABLE_WIDTH - PLANK_WIDTH);    private int nBall_X = rand.nextInt(TABLE_WIDTH - BALL_2R);    private int nBall_Y = BALL_2R;    //  ball speed;    private int nBallSpeed_Y = 10;    private int nBallSpeed_X = (rand.nextInt() - 1) * nBallSpeed_Y;        private JFrame jfrmMain = new JFrame("Pinball");    private JMainPanel jPinball = new JMainPanel();        private Timer tmrTask = null;    private boolean isLose = false;    public void init()    {        jPinball.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));        jPinball.setBackground(Color.WHITE);        jfrmMain.add(jPinball);                KeyAdapter LeftRightListener = new KeyAdapter()        {            public void keyPressed(KeyEvent ke)            {                if (ke.getKeyCode() == KeyEvent.VK_LEFT)                {                    if (nPlank_X - 10 > 0)                    {                        nPlank_X -= 10;                    }                    else                    {                        nPlank_X = 0;                    }                }                if (ke.getKeyCode() == KeyEvent.VK_RIGHT)                {                    if (nPlank_X + PLANK_WIDTH + 10 < TABLE_WIDTH)                    {                        nPlank_X += 10;                    }                    else                    {                        nPlank_X = TABLE_WIDTH - PLANK_WIDTH;                    }                }            }        };        jPinball.addKeyListener(LeftRightListener);        jfrmMain.addKeyListener(LeftRightListener);                tmrTask = new Timer(100, new ActionListener()        {            public void actionPerformed(ActionEvent ae)            {                if (nBall_X == 0 || nBall_X == TABLE_WIDTH - BALL_2R)                {                    nBallSpeed_X = -nBallSpeed_X;                }                                if (nBall_Y <= 0 || (nBall_Y + BALL_2R >= PLANK_Y && nBall_X >= nPlank_X && nPlank_X + PLANK_WIDTH >= nBall_X))                {                    nBallSpeed_Y = -nBallSpeed_Y;                }                                if (nBall_Y + BALL_2R >= PLANK_Y && (nBall_X < nPlank_X || nPlank_X + PLANK_WIDTH < nBall_X))                {                    tmrTask.stop();                    isLose = true;                    jPinball.repaint();                }                                nBall_X += nBallSpeed_X;                nBall_Y += nBallSpeed_Y;                jPinball.repaint();            }        });        tmrTask.start();                jfrmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        jfrmMain.pack();        jfrmMain.setVisible(true);    }            class JMainPanel extends JPanel    {        public void paint(Graphics g)        {            if (isLose)            {                g.setColor(Color.RED);                g.setFont(new Font("Times", Font.BOLD, 32));                g.drawString("Game over", 50, 200);            }            else            {                g.setColor(Color.YELLOW);                g.fillOval(nBall_X, nBall_Y, BALL_2R, BALL_2R);                g.setColor(Color.GREEN);                g.fillRect(nPlank_X, PLANK_Y, PLANK_WIDTH, PLANK_HEIGHT);            }        }    }        public static void main(String[] args)    {        new SwingPinball().init();    }} 



摘自某书的 乒乓球例子,因为他是用 awt 的canvas写的,我 用 jpanel 改写一下,但是动不了了。为什么???

[解决办法]
Java code
    class JMainPanel extends JPanel    {        @Override public void paintComponent(Graphics g)        {            super.paintComponent(g);            Graphics2D g2d = (Graphics2D) g;            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);            if (isLose)                {                    g2d.setColor(Color.RED);                    g2d.setFont(new Font("Times", Font.BOLD, 32));                    g2d.drawString("Game over", 50, 200);                }            else                {                    g2d.setColor(Color.YELLOW);                    g2d.fillOval(nBall_X, nBall_Y, BALL_2R, BALL_2R);                    g2d.setColor(Color.GREEN);                    g2d.fillRect(nPlank_X, PLANK_Y, PLANK_WIDTH, PLANK_HEIGHT);                }        }    } 

热点排行