一个免提 2015-12-21 13:53 采纳率: 100%
浏览 2803
已采纳

请教接苹果小游戏代码怎么编?(Java)

我编写的是用篮子接小球,在一个面板里既可以控制篮子左右移动,又不妨碍小球随机掉落,小球可以用Timer动画隔一段时间就repaint,篮子通过按钮左右移动之后也要repaint,他们都是通过一个paintComponent画出来的,但是这样一来,小球本来在时间间隔里,但是每次一移动篮子,小球又要重画,这样造成小球移动得时快时慢,这样怎么办???
下面是我的Java代码:
public class FinalProject extends JFrame{
private JButton jbtLeft = new JButton("Left");
private JButton jbtRight = new JButton("Right");
private JTextField jtfTotal = new JTextField(4);
private JTextField jtfCount = new JTextField(4);

private AppleJPanel p2 = new AppleJPanel();
int count=0;
int type=0;
//int n = Integer.parseInt(jtfTotal.getText());
public FinalProject(){
    JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
    p1.add(new JLabel("Total"));
    p1.add(jtfTotal);
    p1.add(new JLabel("Count"));
    p1.add(jtfCount);
    p1.add(jbtLeft);
    p1.add(jbtRight);

    jbtLeft.setMnemonic('J');
    jbtRight.setMnemonic('K');

    this.add(p1,BorderLayout.SOUTH);
    this.add(p2,BorderLayout.CENTER);

    ButtonListener listener = new ButtonListener();

    jbtLeft.addActionListener(listener);
    jbtRight.addActionListener(listener);
}

private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
            if(e.getSource() == jbtLeft)
            {
                type=1;
                repaint();
            }

            else if (e.getSource() == jbtRight)
            {
                type=2;
                repaint();
            }
    }
}

public static void main(String[] args){
    FinalProject frame = new FinalProject();
    frame.setTitle("FinalProject");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,300);
    frame.setVisible(true);
    }

class AppleJPanel extends JPanel{
    private int delay =100;
    private Timer timer = new Timer(delay, new  TimerListener());

    private int x=0 ;
    private int x1=getWidth();
    private int xCoordinate = (int) (Math.random()*getWidth());
    private int yCoordinate = 0;

    public AppleJPanel(){
        timer.start();
    }

    class TimerListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            repaint();
        }
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        //int n = Integer.parseInt(jtfTotal.getText());
        g.setColor(Color.red);
        if(yCoordinate+15>getHeight()){
            yCoordinate=-20;
            xCoordinate = (int) (Math.random()*p2.getWidth());
        }
        yCoordinate +=5;
        g.fillOval(xCoordinate, yCoordinate, 15, 15);
        if(type==1)
        {
            x=x-10;
            type=0;
        }
        else if(type==2)
        {
            x=x+10;
            type=0;
        }
        g.fillRect(getWidth()/2+x ,getHeight()-26, 25, 25);
        int x1=getWidth()/2+x;
        int y1=getHeight()-28;
        if((xCoordinate>=x1 && xCoordinate+15<=x1+25) && (yCoordinate>=y1 && yCoordinate+15<=y1+25))
        {
            count++;
            jtfCount.setText(""+count);
        }
        /*
        if(x!=0)
        {
            g.drawRect(getWidth()/2+x ,getHeight()-20, 15, 15);
        }
        else
        {
            g.drawRect(getWidth()/2+x ,getHeight()-20, 15, 15);
        }*/
    }   
}

}图片

  • 写回答

2条回答 默认 最新

  • devmiao 2015-12-21 14:41
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记