Flyweignt

package test0806;

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

public class FlyweightTest extends JFrame {
    
private static final Color colors[] = { Color.red, Color.blue,
            Color.yellow, Color.orange, Color.black, Color.white }
;

    
private static final int WINDOW_WIDTH = 400, WINDOW_HEIGHT = 400,
            NUMBER_OF_LINES 
= 500;

    
private ArrayList vLine = new ArrayList();

    JButton button 
= new JButton("draw lines");

    
final JPanel panel = new JPanel();

    
public static void main(String[] args) {
        FlyweightTest test 
= new FlyweightTest();
        test.show();
    }


    
public FlyweightTest() {
        
super("Flyweight Test");
        Container contentPane 
= getContentPane();
        contentPane.setLayout(
new BorderLayout());

        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.SOUTH);
        setBounds(
2020, WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent event) {
                vLine.clear();
                
for (int i = 0; i < NUMBER_OF_LINES; i++{
                    
int index = LineFlyweightFactory.getIndex(getRandomColor(),
                            getRandomWidth());
                    vLine.add(
new Line(new Point(getRandomX(), getRandomY()),
                            
new Point(getRandomX(), getRandomY()), index));
                }

                repaint();
            }

        }
);
    }


    
private int getRandomX() {
        
return (int) (Math.random() * WINDOW_WIDTH);
    }


    
private int getRandomY() {
        
return (int) (Math.random() * WINDOW_HEIGHT);
    }


    
private Color getRandomColor() {
        
return colors[(int) (Math.random() * colors.length)];
    }


    
private int getRandomWidth() {
        
return (int) (Math.random() * 5);
    }


    
public void paint(Graphics g) {
        
super.paint(g);
        Graphics gp 
= panel.getGraphics();
        Line line;
        
for (int i = 0; i < vLine.size(); i++{
            line 
= (Line) vLine.get(i);
            line.draw(gp);
        }

    }

}


// class which contains extrinsic state and reference to flyweight
class Line {
    
private Point start, end;

    
private int index; // reference to flyweight

    
public Line(Point start, Point end, int index) {
        
this.start = start;
        
this.end = end;
        
this.index = index;
    }


    
public void draw(Graphics g) {
        LineFlyweight line 
= LineFlyweightFactory.getLine(index);
        line.draw(g, start.x, start.y, end.x, end.y); 
// pass extrinsic state
        
// to flyweight
    }

}


// Flyweight
class LineFlyweight {
    
// intrinsic state
    private Color color;

    
private BasicStroke stroke;

    
public LineFlyweight(Color color, float lineWidth) {
        
this.color = color;
        stroke 
= new BasicStroke(lineWidth);
    }


    
public boolean equals(Color color, int lineWidth) {
        
if (this.color.equals(color) && (stroke.getLineWidth() == lineWidth))
            
return true;

        
return false;
    }


    
public void draw(Graphics g, int x, int y, int x2, int y2) {
        Graphics2D g2 
= (Graphics2D) g;
        g2.setColor(color);
        g2.setStroke(stroke);
        g2.drawLine(x, y, x2, y2);
    }

}


class LineFlyweightFactory {
    
private static final ArrayList vFlyweight = new ArrayList();

    
public static int getIndex(Color color, int lineWidth) {
        LineFlyweight line;
        
for (int i = 0; i < vFlyweight.size(); i++{
            line 
= (LineFlyweight) vFlyweight.get(i);
            
if (line.equals(color, lineWidth))
                
return i;
        }

        line 
= new LineFlyweight(color, lineWidth);
        vFlyweight.add(line);
        System.out.println(
"Creating " + color + " line with width = "
                
+ lineWidth);
        
return vFlyweight.size() - 1;
    }


    
public static LineFlyweight getLine(int index) {
        
if (index > vFlyweight.size())
            
return null;

        
return (LineFlyweight) vFlyweight.get(index);
    }

}
 
根据原作 https://pan.quark.cn/s/0ed355622f0f 的源码改编 野火IM解决方案 野火IM是专业级即时通讯和实时音视频整体解决方案,由北京野火无限网络科技有限公司维护和支持。 主要特性有:私有部署安全可靠,性能强大,功能齐全,全平台支持,开源率高,部署运维简单,二次开发友好,方便与第三方系统对接或者嵌入现有系统中。 详细情况请参考在线文档。 主要包括一下项目: 野火IM Vue Electron Demo,演示如何将野火IM的能力集成到Vue Electron项目。 前置说明 本项目所使用的是需要付费的,价格请参考费用详情 支持试用,具体请看试用说明 本项目默认只能连接到官方服务,购买或申请试用之后,替换,即可连到自行部署的服务 分支说明 :基于开发,是未来的开发重心 :基于开发,进入维护模式,不再开发新功能,鉴于已经终止支持且不再维护,建议客户升级到版本 环境依赖 mac系统 最新版本的Xcode nodejs v18.19.0 npm v10.2.3 python 2.7.x git npm install -g node-gyp@8.3.0 windows系统 nodejs v18.19.0 python 2.7.x git npm 6.14.15 npm install --global --vs2019 --production windows-build-tools 本步安装windows开发环境的安装内容较多,如果网络情况不好可能需要等较长时间,选择早上网络较好时安装是个好的选择 或参考手动安装 windows-build-tools进行安装 npm install -g node-gyp@8.3.0 linux系统 nodej...
标题Django基于大数据的大麦网演唱会数据系统研究AI更换标题第1章引言阐述基于Django和大数据的大麦网演唱会数据系统的研究背景、意义、现状及创新点。1.1研究背景与意义分析大麦网演唱会数据现状及大数据处理的必要性。1.2国内外研究现状综述国内外在演唱会数据系统方面的研究进展。1.3研究方法及创新点介绍本文采用的研究方法及系统设计的创新点。第2章相关理论介绍Django框架和大数据处理相关理论。2.1Django框架概述介绍Django框架的特点和优势。2.2大数据处理技术介绍大数据处理的关键技术,包括数据采集、存储、分析和可视化。2.3数据挖掘与机器学习阐述数据挖掘和机器学习在数据处理中的应用。第3章系统设计详细介绍基于Django和大数据的大麦网演唱会数据系统的设计方案。3.1系统架构设计给出系统的整体架构,包括前端、后端和数据库设计。3.2功能模块设计介绍系统的各个功能模块,如数据采集、处理、分析和展示。3.3数据库设计阐述数据库的设计思路和表结构。第4章数据收集与处理介绍系统数据收集和处理的方法。4.1数据收集方法介绍从大麦网获取演唱会数据的方法。4.2数据清洗与预处理阐述数据清洗和预处理的过程。4.3数据存储与管理介绍数据存储和管理的方法,包括数据库的选择和优化。第5章系统实现与测试介绍系统的实现过程和测试方法。5.1系统实现过程详细介绍系统的实现步骤和关键代码。5.2系统测试方法介绍系统测试的方法和测试用例设计。5.3测试结果与分析对测试结果进行分析,评估系统的性能和稳定性。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括本文的主要研究成果和系统实现效果。6.2展望指出系统存在的不足和未来改进的方向。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值