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

📄 bubblethreaddemo.java

📁 Java程序设计大学教程程序源代码
💻 JAVA
字号:
/*
 *BubbleThreadDemo.java
 * 参见教程289页 示例程序10-4
 * Created on 2005年8月6日, 下午4:59
 */

package jbookch10;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

/**
 * 主程序
 */
public class BubbleThreadDemo {
    public static void main(String[] args) {
        JFrame frame = new MainFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
    }
}

/**
 * 主窗体界面
 */
class MainFrame extends JFrame {
    public MainFrame() {
        setSize(WIDTH, HEIGHT);
        setTitle("泡泡动画");
        Container contentPane = getContentPane();
        canvas = new BubbleCanvas();
        contentPane.add(canvas, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        addButton(buttonPanel, "产生泡泡",
                new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addBubble();
            }
        });
        addButton(buttonPanel, "退出程序",
                new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
    }
    
    public void addButton(Container c, String title,
            ActionListener listener) {
        JButton button = new JButton(title);
        c.add(button);
        button.addActionListener(listener);
    }
    
    public void addBubble() {
        Bubble b = new Bubble(canvas);//创建泡泡对象
        canvas.add(b);//将泡泡对象注册到画布上
        BubbleThread thread = new BubbleThread(b);//创建泡泡动画线程
        thread.start();//启动泡泡动画线程
    }
    
    private BubbleCanvas canvas;
    public static final int WIDTH = 450;
    public static final int HEIGHT = 350;
}

/**
 * 泡泡动画线程
 */
class BubbleThread extends Thread {
    public BubbleThread(Bubble aBall) {
        b = aBall;
    }
    
    public void run() {
        try {
            for (int i = 1; i <= 200; i++) {
                b.enlarge();//泡泡变大
                sleep(30);//利用线程休眠延时
            }
        } catch (InterruptedException exception) {
        }
    }
    
    private Bubble b;
}

/**
 * 绘制泡泡的画布
 */
class BubbleCanvas extends JPanel {
    public void add(Bubble b) {
        bubbles.add(b);//注册各个线程创建的泡泡
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        //重新把画布上的所有泡泡重绘一遍,以便保持显示同步
        for (int i = 0; i < bubbles.size(); i++) {
            Bubble b = (Bubble)bubbles.get(i);
            b.draw(g2);
        }
    }
    
    private ArrayList bubbles = new ArrayList();
    private Bubble lnkBubble;
}

class Bubble {
    public Bubble(Component c) {
        canvas = c;
        bubbleColor=getColor();
        x = (int)(Math.random()* canvas.getWidth());
        y =(int)(Math.random()*canvas.getHeight());
    }
    
    private Color getColor(){
        //产生随机颜色
        int r=(int)(Math.random()*255);
        int g=(int)(Math.random()*255);
        int b=(int)(Math.random()*255);
        Color newColor=new Color(r,g,b);
        return newColor;
    }

    public void draw(Graphics2D g2) {
        if (xSize>(200)) return;
        g2.setColor(bubbleColor);
        g2.draw(new Ellipse2D.Double(x, y, xSize, ySize));
    }

    public void enlarge() {
        x-= dx;
        y-= dy;
        xSize+= 2*dx;
        ySize += 2*dy;
        canvas.repaint();//刷新画布
    }
    
    private Component canvas;
    private int xSize =1;
    private int ySize= 1;
    private int x = 100;
    private int y = 100;
    private int dx = 1;
    private int dy = 1;
    private Color bubbleColor;
}

















⌨️ 快捷键说明

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