demo2.jpg
要求功能如圖所示,並沒有要求彈碰。
實際內容以程式碼為主,並沒有題目文件,屬於個人的作業要求。
由 BallFrame.java, BallPanel.java, Ball.java 構成
BallFrame.java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BallFrame {
private JFrame frame;
private JPanel upperPanel;
private BallPanel lowerPanel;
private JButton addBall;
private JButton delBall;
private JButton stopBall;
private JButton stopAllBall;
private JButton clearBall;
private ExecutorService threadPool;
public BallFrame() {
frame = new JFrame("JumpBall");
upperPanel = new JPanel();
lowerPanel = new BallPanel();
addBall = new JButton("Add 1 Ball"); // 1
delBall = new JButton("Del 1 Ball"); // 2
stopBall = new JButton("Stop 1 Ball"); // 3
stopAllBall = new JButton("Stop All"); // 4
clearBall = new JButton("Clear All"); // 5
addBall.setEnabled(false);
upperPanel.setLayout(new GridLayout(2, 3));
upperPanel.add(addBall);
upperPanel.add(delBall);
upperPanel.add(stopBall);
upperPanel.add(stopAllBall);
upperPanel.add(clearBall);
upperPanel.setBorder(new TitledBorder("Setting"));
frame.setLayout(new BorderLayout());
frame.add(upperPanel, BorderLayout.NORTH);
frame.add(lowerPanel, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lowerPanel.W = lowerPanel.getWidth();
lowerPanel.H = lowerPanel.getHeight();
lowerPanel.setBorder(new TitledBorder("Area"));
threadPool = Executors.newCachedThreadPool();
threadPool.execute(lowerPanel);
threadPool.shutdown();
addBall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lowerPanel.CH = 0;
addBall.setEnabled(false);
delBall.setEnabled(true);
stopBall.setEnabled(true);
stopAllBall.setEnabled(true);
clearBall.setEnabled(true);
}
});
delBall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lowerPanel.CH = 1;
addBall.setEnabled(true);
delBall.setEnabled(false);
stopBall.setEnabled(true);
stopAllBall.setEnabled(true);
clearBall.setEnabled(true);
}
});
stopBall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lowerPanel.CH = 2;
addBall.setEnabled(true);
delBall.setEnabled(true);
stopBall.setEnabled(false);
stopAllBall.setEnabled(true);
clearBall.setEnabled(true);
}
});
stopAllBall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lowerPanel.CH = 3;
addBall.setEnabled(true);
delBall.setEnabled(true);
stopBall.setEnabled(true);
stopAllBall.setEnabled(false);
clearBall.setEnabled(true);
}
});
clearBall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lowerPanel.CH = 4;
addBall.setEnabled(true);
delBall.setEnabled(true);
stopBall.setEnabled(true);
stopAllBall.setEnabled(true);
clearBall.setEnabled(false);
}
});
}
public static void main(String[] args) {
new BallFrame();
}
}
BallPanel.java
import javax.swing.JPanel;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
public class BallPanel extends JPanel implements Runnable {
Vector<Ball> vector = new Vector<Ball>();
public int CH = 0;
public int W, H;
private ExecutorService threadPool;
public BallPanel() {
threadPool = Executors.newCachedThreadPool();
//threadPool.shutdown();
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (CH == 0) {
Ball ball = new Ball(e.getX(), e.getY());
ball.setRunSize(W, H);
vector.add(ball);
threadPool.execute(ball);
} else if (CH == 1) {
int x, y;
x = e.getX();
y = e.getY();
for (int i = 0; i < vector.size(); i++) {
if ((vector.get(i).x - x) * (vector.get(i).x - x)
+ (vector.get(i).y - y) * (vector.get(i).y - y) <= (vector.get(i).r + 20)
* (vector.get(i).r + 20)) {
vector.get(i).isExist = false;
vector.remove(i);
break;
}
}
} else if(CH == 2) {
int x, y;
x = e.getX();
y = e.getY();
for (int i = 0; i < vector.size(); i++) {
if ((vector.get(i).x - x) * (vector.get(i).x - x)
+ (vector.get(i).y - y) * (vector.get(i).y - y) <= (vector.get(i).r + 20)
* (vector.get(i).r + 20)) {
vector.get(i).canMove = !vector.get(i).canMove;
}
}
} else if(CH == 3) {
for (int i = 0; i < vector.size(); i++) {
vector.get(i).canMove = !vector.get(i).canMove;
}
} else {
for (int i = 0; i < vector.size(); i++) {
vector.get(i).canMove = false;
vector.remove(i);
i--;
}
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < vector.size(); i++) {
g.setColor(vector.get(i).color);
g.fillOval(vector.get(i).x, vector.get(i).y, vector.get(i).r, vector.get(i).r);
}
}
public void run() {
while (true) {
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("I'm interrupted ...");
}
//System.out.println(threadPool.);
}
}
}
Ball.java
import java.awt.Color;
import javax.swing.JPanel;
public class Ball implements Runnable {
public int x, y;
public int r, vx, vy, dirx, diry;
public Color color;
public boolean canMove, isExist;
public int W, H;
public Ball(int x, int y) {
this.x = x;
this.y = y;
vx = (int) (Math.random() * 5) + 1;
vy = (int) (Math.random() * 5) + 1;
r = (int) (Math.random() * 30) + 30;
dirx = (int) (Math.random() * 2);
diry = (int) (Math.random() * 2);
color = new Color((int) (Math.random() * 255),
(int) (Math.random() * 255), (int) (Math.random() * 255));
canMove = true;
isExist = true;
}
public void setRunSize(int w, int h) {
W = w;
H = h;
}
public void move() {
if (x + r > W || x < 0)
dirx = 1 - dirx;
if (y + r > H || y < 10)
diry = 1 - diry;
if(dirx == 1) x += vx;
else x -= vx;
if(diry == 1) y += vy;
else y -= vy;
}
public void run() {
while (isExist) {
while (canMove) {
move();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
System.out.println("I'm Interrupted ...");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("I'm Interrupted ...");
}
}
}
}
文章定位: