package com.javapapers.designpattern.flyweight;
import java.awt.Color; import java.awt.Graphics;
public interface MyShape {
public void draw(Graphics g, int x, int y, int width, int height,
Color color, boolean fill, String font);
} package com.javapapers.designpattern.flyweight;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class MyOval implements MyShape {
private String label;
public MyOval(String label) {
this.label = label;
}
public void draw(Graphics oval, int x, int y, int width, int height,
Color color, boolean fill, String font) {
oval.setColor(color);
oval.drawOval(x, y, width, height);
oval.setFont(new Font(font, 12, 12));
oval.drawString(label, x + (width / 2), y);
if (fill)
oval.fillOval(x, y, width, height);
}
}
package com.javapapers.designpattern.flyweight;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class MyRectangle implements MyShape {
private String label;
public MyRectangle(String label) {
this.label = label;
}
public void draw(Graphics rectangle, int x, int y, int width, int height,
Color color, boolean fill, String font) {
rectangle.setColor(color);
rectangle.drawRect(x, y, width, height);
rectangle.setFont(new Font(font, 12, 12));
rectangle.drawString(label, x + (width / 2), y);
if (fill)
rectangle.fillRect(x, y, width, height);
}
}
package com.javapapers.designpattern.flyweight;
import java.util.HashMap;
public class ShapeFactory {
private static final HashMap shapes = new HashMap();
public static MyShape getShape(String label) {
MyShape concreteShape = (MyShape) shapes.get(label);
if (concreteShape == null) {
if (label.equals("R")) {
concreteShape = new MyRectangle(label);
} else if (label.equals("O")) {
concreteShape = new MyOval(label);
}
shapes.put(label, concreteShape);
}
return concreteShape;
}
}
package com.javapapers.designpattern.flyweight;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Client extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final String shapes[] = { "R", "O" };
private static final Color colors[] = { Color.red, Color.green, Color.blue };
private static final boolean fill[] = { true, false };
private static final String font[] = { "Arial", "Courier" };
public Client() {
Container contentPane = getContentPane();
JButton startButton = new JButton("Draw Shapes");
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(startButton, BorderLayout.SOUTH);
setSize(WIDTH, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Graphics g = panel.getGraphics();
for (int i = 0; i < 100; ++i) {
MyShape shape = ShapeFactory.getShape(getRandomShape());
shape.draw(g, getRandomX(), getRandomY(), getRandomWidth(),
getRandomHeight(), getRandomColor(),
getRandomFill(), getRandomFont());
}
}
});
}
private String getRandomShape() {
return shapes[(int) (Math.random() * shapes.length)];
}
private int getRandomX() {
return (int) (Math.random() * WIDTH);
}
private int getRandomY() {
return (int) (Math.random() * HEIGHT);
}
private int getRandomWidth() {
return (int) (Math.random() * (WIDTH / 7));
}
private int getRandomHeight() {
return (int) (Math.random() * (HEIGHT / 7));
}
private Color getRandomColor() {
return colors[(int) (Math.random() * colors.length)];
}
private boolean getRandomFill() {
return fill[(int) (Math.random() * fill.length)];
}
private String getRandomFont() {
return font[(int) (Math.random() * font.length)];
}
public static void main(String[] args) {
Client client = new Client();
}
} |