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);
    }

}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章