java画笔画一个图形_java中利用线程使画笔画出的图形动起来

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

原始数据

按行查看

历史

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值