import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.util.Random;
import javax.swing.*;
class jj extends JFrame {
}
public class jumpball extends JFrame {
protected long sleepAmount = 10;
public jumpball() {
setBackground(Color.CYAN);
Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
int screenWidth = screenSize.width; // 获取屏幕的宽
int screenHeight = screenSize.height; // 获取屏幕的高
setLocation(new Point(screenWidth / 2 - getWidth() / 2, screenHeight / 2 - getHeight() / 2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton mb = new JButton();
mb.setText("click");
JPanel pan = new JPanel();
pan.setBackground(Color.CYAN);
pan.setLayout(new FlowLayout());
pan.add(mb);
setSize(500, 500);
final Mypaint mp = new Mypaint();
// mp.setBackground(Color.BLACK);
add(mp);
getContentPane().add(pan, BorderLayout.NORTH);
mb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Thread tt = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
mp.repaint();
Thread.sleep(sleepAmount);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tt.start();
}
});
}
public static void main(String[] args) {
jumpball my = new jumpball();
my.setVisible(true);
}
}
class Mypaint extends JPanel {
int x = 15, y = (int) (Math.random() * 1000 % getHeight());
double radius = 15;
int step = 5;
double k = Math.random() * Math.PI * 2;
public Mypaint() {
setDoubleBuffered(true);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
if (x < 0 || x > getWidth() - radius * 2) {
// if (k <= Math.PI)
k = -k + Math.PI * 2;
// if (k < 0)
// k = 2 * Math.PI + k;
} else if (y < 0 || y > getHeight() - radius * 2) {
k = Math.PI - k;
}
x += Math.sin(k) * step;
y += Math.cos(k) * step;
Arc2D arc = new Arc2D.Float(1);
arc.setFrame(x, y, radius * 2, radius * 2);
arc.setAngleExtent(360);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(arc);
}
}