java设置一个窗体_java程序设计一个窗体实现每隔1s在窗体上随机显示1个红心园点...

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

udwzm

2015.12.04

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:48%    等级:9

已帮助:463人

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.util.Random; import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer; public class TestDemo extends JPanel implements ActionListener{     final Dimension dim = new Dimension(500, 500);    Graphics bg;    BufferedImage sc;    Timer timer = new Timer(1000, this);         public TestDemo() {        this.setPreferredSize(dim);        sc = new BufferedImage(dim.width, dim.height, 1);        bg = sc.getGraphics();        bg.setColor(Color.WHITE);        bg.fillRect(0, 0, dim.width - 1, dim.height - 1);        timer.start();    }         @Override    protected void paintComponent(Graphics g) {        super.paintComponent(g);        g.drawImage(sc, 0, 0, this);    }         @Override    public void actionPerformed(ActionEvent e) {        // TODO Auto-generated method stub        Random rnd = new Random();        int x = rnd.nextInt(dim.width),            y = rnd.nextInt(dim.height),            r = rnd.nextInt(10) + 10;        new Circle(x, y, r).draw(bg);        repaint();    }         public static void main(String[] args) {        JFrame frame = new JFrame("Demo");        frame.add(new TestDemo());        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);    } } class Circle {    int x, y, r;    public Circle(int x, int y, int r) {        this.x = x;        this.y = y;        this.r = r;    }    public void draw(Graphics g) {        g.setColor(Color.RED);        g.fillOval(x - r, y - r, 2 * r, 2 * r);    }}

00分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值