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

请教接苹果小游戏代码怎么编?(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 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题