当前位置: 代码迷 >> Java相关 >> 这个到底是哪出了问题
  详细解决方案

这个到底是哪出了问题

热度:313   发布时间:2011-08-28 18:38:49.0
这个到底是哪出了问题
import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent.*;
import static java.util.EventObject.*;
import javax.swing.Timer;
import java.awt.event.KeyAdapter.*;
import static java.awt.AWTEvent.*;
public class PinBall
{
//桌面的宽度
private final int TABLE_WIDTH=300;
//桌面的高度
private final int TABLE_HEIGHT=400;
//球拍的垂直位置
private final int RACKET_Y=340;
//下面定义球拍的高度和宽度
private final int RACKET_HEIGHT=20;
private final int RACKET_WIDTH=60;
//小球的大小
private final int BALL_SIZE=16;
private Frame f =new Frame("弹球游戏");
Random rand=new Random();
//小球纵向的运行速度
private int ySpeed=10;
//返回一个-0.5-0.5的比率,用于控制小球的运行方向
private double xyRate=rand.nextDouble() - 0.5;
//小球横向的运行速度
private int xSpeed=(int)(ySpeed * xyRate * 2);
//ballx和bally代表小球的坐标
private int ballx=rand.nextInt(200)+20;
private int bally=rand.nextInt(10)+20;
    //racketX代表球拍的水平位置
private int racketX=rand.nextInt(200);
    private MyCanvas tableArea=new MyCanvas();
//用于保存需要绘制什么图形的字符串属性
private String shape="";
Timer timer;
//游戏是否结束的旗标
private boolean isLose=false;//963446064
public void init()
    {
    tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
    f.add(tableArea);
    //定义键盘监听器
    KeyAdapter keyProcessor=new KeyAdapter()
        {
        public void keyPressed(KeyEvent ke)
            {
            //按下向左、向右键时,球拍水平坐标分别减少、增加
            if (ke.getKeyCode()==KeyEvent.VK_LEFT)
            {
            if(racketX>0)
                racketX-=10;
            }
            if(ke.getkeycode()==KeyEvent.VK_RIGHT)
                {
                if(racketX<TABLE_WIDTH-RACKEY_WIDTH)
                    racketX+=10;
                }
            }
        };

        //为窗口和tableArea对象分别添加键盘监听器
        f.addKeyListener(keyProcessor);
        tableArea.addkeyListener(keyProcessor);
        //定义每0.1秒执行一次的时间监听器。
        ActionListener taskPerformer=new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //如果小球碰到左边边框
                if(ballx<0||ballx>=TABLE_WIDTH-BALL_SIZE)
                {
                    xSpeed=-xSpeed;
                }
                //如果小球高度超出了球拍位置,且横向不在球拍范围内,游戏结束。
                if(ballx>=RACKET_Y-BALL_SIZE&&
                    (ballX<racketX||ballX>racketX+RACKET_WIDTH))
                {
                    timer.stop();
                    //设置游戏是否结束的旗标为true.
                    isLose=true;
                    tableArea.repaint();
                }
                //如果小球位于球拍之内,且到达球拍位置,小球反弹
                else if (bally<=0||
                    (bally>=RACKET_Y-BALL_Size
                    &&ballX>racketX&&ballX<=racketX+RACKET_WIDTH))
                {
                    ySpeed=-ySpeed;
                }
                //小球坐标增加
                bally+=ySpeed;
                ballX+=xSpeed;
                tableArea.repaint();
            }
        };
        timer=new Timer(100,taskPerformer);
        timer.start();
        f.setVisible(true);

    }
    public static void main(String[]args)
    {
        new PinBall().init();
    }
    class MyCanvas extends Canvas
    {
        //重写Canvas的paint方法,实现绘画
        public void paint(Graphics g)
        {
            //如果游戏已经结束
            if(isLose)
            {
                g.setColor(new Color(255,0,0));
                g.setFont(new Font("Times",Font.BOLD,30));
                g.drawString("游戏已结束!",50,200);
                    }
                //如果游戏还未结束
                else
            {
                    //设置颜色,并绘制小球
                    g.setColor(new Color(240,240,80));
                    g.fillOval(ballx,bally,BALL_SIZE,BALL_SIZE);
                    //设置颜色,并绘制球拍
                    g.setColor(new Color(80,80,200));
                    g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);
            }
        }
    }
}
            
            
            
            
   
   
               
搜索更多相关的解决方案: private  

----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽
2011-08-28 11:36:09
qq727755316

等 级:新手上路
帖 子:3
专家分:0
注 册:2011-8-28
  得分:0 
回复 2楼 付政委
。。。没啊
。。。。。才怪


[ 本帖最后由 qq727755316 于 2011-8-28 21:10 编辑 ]
----------------解决方案--------------------------------------------------------
具体内容我没有看,只是根据IDE提示把错误改了一下,可以运行了。不知道是不是你想的结果。
程序代码:
import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
public class PinBall
{
//桌面的宽度
private final int TABLE_WIDTH=300;
//桌面的高度
private final int TABLE_HEIGHT=400;
//球拍的垂直位置
private final int RACKET_Y=340;
//下面定义球拍的高度和宽度
private final int RACKET_HEIGHT=20;
private final int RACKET_WIDTH=60;
//小球的大小
private final int BALL_SIZE=16;
private Frame f =new Frame("弹球游戏");
Random rand=new Random();
//小球纵向的运行速度
private int ySpeed=10;
//返回一个-0.5-0.5的比率,用于控制小球的运行方向
private double xyRate=rand.nextDouble() - 0.5;
//小球横向的运行速度
private int xSpeed=(int)(ySpeed * xyRate * 2);
//ballx和bally代表小球的坐标
private int ballx=rand.nextInt(200)+20;
private int bally=rand.nextInt(10)+20;
    //racketX代表球拍的水平位置
private int racketX=rand.nextInt(200);
    private MyCanvas tableArea=new MyCanvas();
//用于保存需要绘制什么图形的字符串属性
private String shape="";
Timer timer;
//游戏是否结束的旗标
private boolean isLose=false;//963446064
public void init()
    {
    tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
    f.add(tableArea);
    //定义键盘监听器
    KeyAdapter keyProcessor=new KeyAdapter()
        {
        public void keyPressed(KeyEvent ke)
            {
            //按下向左、向右键时,球拍水平坐标分别减少、增加
            if (ke.getKeyCode()==KeyEvent.VK_LEFT)
            {
            if(racketX>0)
                racketX-=10;
            }
            if(ke.getKeyCode()==KeyEvent.VK_RIGHT)
                {
                if(racketX<TABLE_WIDTH-RACKET_WIDTH)
                    racketX+=10;
                }
            }
        };

        //为窗口和tableArea对象分别添加键盘监听器
        f.addKeyListener(keyProcessor);
        tableArea.addKeyListener(keyProcessor);
        //定义每0.1秒执行一次的时间监听器。
        ActionListener taskPerformer=new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //如果小球碰到左边边框
                if(ballx<0||ballx>=TABLE_WIDTH-BALL_SIZE)
                {
                    xSpeed=-xSpeed;
                }
                //如果小球高度超出了球拍位置,且横向不在球拍范围内,游戏结束。
                if(ballx>=RACKET_Y-BALL_SIZE&&
                    (ballx<racketX||ballx>racketX+RACKET_WIDTH))
                {
                    timer.stop();
                    //设置游戏是否结束的旗标为true.
                    isLose=true;
                    tableArea.repaint();
                }
                //如果小球位于球拍之内,且到达球拍位置,小球反弹
                else if (bally<=0||
                    (bally>=RACKET_Y-BALL_SIZE
                    &&ballx>racketX&&ballx<=racketX+RACKET_WIDTH))
                {
                    ySpeed=-ySpeed;
                }
                //小球坐标增加
                bally+=ySpeed;
                ballx+=xSpeed;
                tableArea.repaint();
            }
        };
        timer=new Timer(100,taskPerformer);
        timer.start();
        f.setVisible(true);

    }
    public static void main(String[]args)
    {
        new PinBall().init();
    }
    class MyCanvas extends Canvas
    {
        //重写Canvas的paint方法,实现绘画
        public void paint(Graphics g)
        {
            //如果游戏已经结束
            if(isLose)
            {
                g.setColor(new Color(255,0,0));
                g.setFont(new Font("Times",Font.BOLD,30));
                g.drawString("游戏已结束!",50,200);
                    }
                //如果游戏还未结束
                else
            {
                    //设置颜色,并绘制小球
                    g.setColor(new Color(240,240,80));
                    g.fillOval(ballx,bally,BALL_SIZE,BALL_SIZE);
                    //设置颜色,并绘制球拍
                    g.setColor(new Color(80,80,200));
                    g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);
            }
        }
    }
}

----------------解决方案--------------------------------------------------------


----------------解决方案--------------------------------------------------------
回复 4楼 czsbc
谢谢啊!
----------------解决方案--------------------------------------------------------
  相关解决方案