forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyWeightTest.java
More file actions
124 lines (74 loc) · 3.06 KB
/
FlyWeightTest.java
File metadata and controls
124 lines (74 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// The Flyweight design pattern is used when you need to
// create a large number of similar objects
// To reduce memory this pattern shares Objects that are
// the same rather than creating new ones
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FlyWeightTest extends JFrame{
private static final long serialVersionUID = 1L;
JButton startDrawing;
int windowWidth = 1750;
int windowHeight = 1000;
// A new rectangle is created only if a new color is needed
Color[] shapeColor = {Color.orange, Color.red, Color.yellow,
Color.blue, Color.pink, Color.cyan, Color.magenta,
Color.black, Color.gray};
public static void main(String[] args){
new FlyWeightTest();
}
public FlyWeightTest(){
// Create the frame, position it and handle closing it
this.setSize(windowWidth,windowHeight);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Flyweight Test");
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
final JPanel drawingPanel = new JPanel();
startDrawing = new JButton("Button 1");
contentPane.add(drawingPanel, BorderLayout.CENTER);
contentPane.add(startDrawing, BorderLayout.SOUTH);
startDrawing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Graphics g = drawingPanel.getGraphics();
long startTime = System.currentTimeMillis();
for(int i=0; i < 100000; ++i) {
//
// Uses rectangles stored in the HashMap to
// speed up the program
MyRect rect = RectFactory.getRect(getRandColor());
rect.draw(g, getRandX(), getRandY(),
getRandX(), getRandY());
//
/*
MyRect rect = new MyRect(getRandColor(), getRandX(), getRandY(), getRandX(), getRandY());
rect.draw(g);
*/
//
/*
g.setColor(getRandColor());
g.fillRect(getRandX(), getRandY(), getRandX(), getRandY());
*/
}
long endTime = System.currentTimeMillis();
System.out.println("That took " + (endTime - startTime) + " milliseconds");
}
});
this.add(contentPane);
this.setVisible(true);
}
// Picks random x & y coordinates
private int getRandX(){ return (int)(Math.random()*windowWidth); }
private int getRandY(){ return (int)(Math.random()*windowHeight); }
// Picks a random Color from the 9 available
private Color getRandColor(){
Random randomGenerator = new Random();
int randInt = randomGenerator.nextInt(9);
return shapeColor[randInt];
}
}