import javax.swing.*;
public class SeaFish {
public static void main(String[] args) {
//创建窗口对象
JFrame jf=new JFrame();
jf.setTitle("鱼");
jf.setSize(1920,1080);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FishJPanel jp=new FishJPanel();
Thread thread=new Thread(jp);
thread.start();
jf.add(jp);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FishJPanel extends JPanel implements Runnable {
private int[] aa = new int[10];//设置一个数组获得图形x,y轴坐标
private int[] bb = new int[10];
public FishJPanel() {
for (int i = 0; i < aa.length; i++) {
aa[i] = new Random().nextInt(1920);
bb[i] = new Random().nextInt(1080);
}
}
public void paint(Graphics g) {
super.paint(g);
//设置背景颜色
this.setBackground(Color.CYAN);
for (int i = 0; i < 10; i++) {
int a = (int) (Math.random() * 1020);
int b = (int) (Math.random() * 768);
g.setColor(Color.red);
g.fillOval(aa[i] + 500, bb[i] + 300, 7, 8);
g.setColor(Color.yellow);
g.fillOval(aa[i] + 430, bb[i] + 275, 70, 60);
g.setColor(Color.black);
g.fillOval(aa[i] + 480, bb[i] + 295, 8, 12);
g.setColor(Color.black);
g.fillOval(aa[i] + 410, bb[i] + 295, 20, 20);
g.setColor(Color.cyan);
g.fillOval(aa[i] + 400, bb[i] + 295, 20, 20);
}
}
@Override
public void run() {
while (true) {
for (int i = 0; i
aa[i]++;
if(aa[i]>1920){
aa[i]=0;
bb[i]=new Random().nextInt(1080);
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史