⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lotsofcircles.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import javax.swing.*;

public class LotsOfCircles
        extends JPanel //这个类可以正常工作
{
    private Image offImage = null;
    private Graphics offGraphics = null;
    private int width = 0, height = 0;
    public LotsOfCircles()
    {
        super();
        setDoubleBuffered(false);
    }

    public void offPaint(Graphics g)
    {
        for (int i = 0; i < 10000; i++)
        {
            Color color = new Color( (int) (255 * Math.random()),
                                    (int) (255 * Math.random()),
                                    (int) (255 * Math.random()));
            int x = (int) (getSize().width * Math.random());
            int y = (int) (getSize().height * Math.random());
            int r = 10;
            g.setColor(color);
            g.fillOval(x - r, y - r, 2 * r, 2 * r);
        }
    }

    public void initOffGraphics()
    {
        width = getSize().width;
        height = getSize().height;
        offImage = createImage(width, height);
        offGraphics = offImage.getGraphics();
        offPaint(offGraphics);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if ( (width != getSize().width) || (height != getSize().height))
        {
            initOffGraphics();
        }
        if (offImage != null)
        {
            g.drawImage(offImage, 0, 0, this);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -