pinballpanel1.java

来自「一些JAVA的小程序」· Java 代码 · 共 53 行

JAVA
53
字号
import java.awt.event.*;import java.util.Vector;import javax.swing.*;import java.awt.*;public class PinBallPanel extends JPanel {  private Vector balls;   //A vector to hold balls  private PinBallFire fireButton = new PinBallFire(new Point(PinBallGame.FrameWidth-40, PinBallGame.FrameHeight-70));  public PinBallPanel(){    balls = new Vector();    addMouseListener(new MouseKeeper());  }  private class MouseKeeper extends MouseAdapter{    public void mousePressed(MouseEvent e) {	//locate position of mouse cursor when moused clicked      int x = e.getX();      int y = e.getY();	//if mouse clicked within firing region, create new	//ball and thread, and start thread      if (fireButton.includes(x, y)) balls.addElement(fireButton.fire(x, y));    }  }  public void run() {    while (true) {      moveBalls();      repaint();      try {        Thread.sleep(10);      } catch(InterruptedException e) {System.exit(0);}    }  }  private void moveBalls() {    for (int i = 0; i < balls.size(); i++) {      Ball theBall = (Ball) balls.elementAt(i);      if (theBall.y() < PinBallGame.FrameHeight)        theBall.move();    }  }  public void paintComponent(Graphics g) {    super.paintComponent(g); //clear window    fireButton.paint(g);    for (int i = 0; i < balls.size(); i++) {      Ball aBall = (Ball) balls.elementAt(i);      aBall.paint(g);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?