程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 接蘋果游戲-請教接蘋果小游戲代碼怎麼編?(Java)

接蘋果游戲-請教接蘋果小游戲代碼怎麼編?(Java)

編輯:編程綜合問答
請教接蘋果小游戲代碼怎麼編?(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);
        }*/
    }   
}

}圖片

最佳回答:


http://download.csdn.net/download/bing621/1433797

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved