日期:2014-05-20  浏览次数:20904 次

把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();
    }
}