问题描述
- 请教接苹果小游戏代码怎么编?(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
解决方案二:
改成了键盘敲击事件了,其他的都一样。还是老问题。
class AppleJPanel extends JPanel{
private final KeyListener KeyAdapter = null;
private int delay =100;
private Timer timer = new Timer(delay, new TimerListener());
int d=15;
int side=25;
private int x=0 ;
private int x1=getWidth();
private int xCoordinate = (int) (Math.random()*getWidth());
private int yCoordinate = 0;
private char keyChar='A';
//int n=Integer.parseInt(jtfTotal.getText());
public AppleJPanel(){
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:x-=10;break;
case KeyEvent.VK_RIGHT:x+=10;break;
default:keyChar=e.getKeyChar();
}
repaint();
}
});
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()*p.getWidth());
}
yCoordinate +=5;
g.fillOval(xCoordinate, yCoordinate, d, d);
g.fillRect(getWidth()/2+x ,getHeight()-25, side, side);
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);
}
}
}
}
时间: 2024-09-22 21:05:46