View Javadoc
1   package auxtestlib;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Dimension;
6   import java.awt.Graphics;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.awt.event.WindowAdapter;
10  import java.awt.event.WindowEvent;
11  import java.awt.image.BufferedImage;
12  
13  import javax.swing.JButton;
14  import javax.swing.JComponent;
15  import javax.swing.JFrame;
16  import javax.swing.JPanel;
17  import javax.swing.JToolBar;
18  
19  /**
20   * Class that can be used to generate random images.
21   */
22  public final class PictureBuilder {
23  	/**
24  	 * Utility class, no constructor.
25  	 */
26  	private PictureBuilder() {
27  		/*
28  		 * Utility class, no constructor.
29  		 */
30  	}
31  
32  	/**
33  	 * Generates a totally random picture with the given width and height.
34  	 * 
35  	 * @param width the width
36  	 * @param height the height
37  	 * 
38  	 * @return a random picture
39  	 */
40  	public static BufferedImage generateFullRandomPicture(int width, int height) {
41  		if (width <= 0) {
42  			throw new IllegalArgumentException("width <= 0");
43  		}
44  
45  		if (height <= 0) {
46  			throw new IllegalArgumentException("height <= 0");
47  		}
48  
49  		BufferedImage bimg = new BufferedImage(width, height,
50  				BufferedImage.TYPE_INT_RGB);
51  		for (int x = 0; x < width; x++) {
52  			for (int y = 0; y < height; y++) {
53  				Color c = pickRandomColor();
54  				bimg.setRGB(x, y, c.getRGB());
55  			}
56  		}
57  
58  		return bimg;
59  	}
60  
61  	/**
62  	 * Generates a random picture by random drawing figures (rectangles and
63  	 * circles) on a white canvas.
64  	 * 
65  	 * @param width the width of the canvas
66  	 * @param height the height of the canvas
67  	 * @param figures the number of figures to draw
68  	 * 
69  	 * @return the generated picture
70  	 */
71  	public static BufferedImage generateSemiRandomPicture(int width,
72  			int height, int figures) {
73  		if (width <= 0) {
74  			throw new IllegalArgumentException("width <= 0");
75  		}
76  
77  		if (height <= 0) {
78  			throw new IllegalArgumentException("height <= 0");
79  		}
80  
81  		BufferedImage bimg = new BufferedImage(width, height,
82  				BufferedImage.TYPE_INT_RGB);
83  		for (int x = 0; x < width; x++) {
84  			for (int y = 0; y < height; y++) {
85  				bimg.setRGB(x, y, Color.WHITE.getRGB());
86  			}
87  		}
88  
89  		Graphics g = bimg.createGraphics();
90  		for (int i = 0; i < figures; i++) {
91  			int ftype = RandomGenerator.rand_int(2);
92  
93  			int x = RandomGenerator.rand_int(width - 1);
94  			int y = RandomGenerator.rand_int(height - 1);
95  			int w = RandomGenerator.rand_int(width - x);
96  			int h = RandomGenerator.rand_int(height);
97  			Color c = pickRandomColor();
98  			g.setColor(c);
99  
100 			switch (ftype) {
101 			case 0:
102 				/*
103 				 * Rectangle.
104 				 */
105 				g.fillRect(x, y, w, h);
106 				break;
107 			case 1:
108 				/*
109 				 * Circle.
110 				 */
111 				g.fillOval(x, y, w, h);
112 				break;
113 			default:
114 				assert false;
115 			}
116 		}
117 
118 		return bimg;
119 	}
120 
121 	/**
122 	 * Generates a random color.
123 	 * 
124 	 * @return a random color
125 	 */
126 	public static Color pickRandomColor() {
127 		int r = RandomGenerator.rand_int(256);
128 		int g = RandomGenerator.rand_int(256);
129 		int b = RandomGenerator.rand_int(256);
130 
131 		Color rgbColor = new Color(r, g, b);
132 		return rgbColor;
133 	}
134 
135 	/**
136 	 * Main method that can be used to try the random picture generator.
137 	 * 
138 	 * @param args not used
139 	 */
140 	public static void main(String[] args) {
141 		JFrame f = new JFrame("Picture generator test");
142 		final int w = 300;
143 		final int h = 200;
144 		final BufferedImage[] img = new BufferedImage[1];
145 		img[0] = generateFullRandomPicture(w, h);
146 		final JComponent image = new JComponent() {
147 			/**
148 			 * Version for serialization.
149 			 */
150 			private static final long serialVersionUID = 1L;
151 
152 			@Override
153 			public void paint(Graphics g) {
154 				g.drawImage(img[0], 0, 0, w, h, null);
155 			}
156 		};
157 
158 		image.setMinimumSize(new Dimension(w, h));
159 		image.setMaximumSize(new Dimension(w, h));
160 		image.setPreferredSize(new Dimension(w, h));
161 
162 		JButton newFullRandom = new JButton("Full Random");
163 		newFullRandom.addActionListener(new ActionListener() {
164 			@Override
165 			public void actionPerformed(ActionEvent e) {
166 				img[0] = generateFullRandomPicture(w, h);
167 				image.repaint();
168 			}
169 		});
170 
171 		JButton newSemi3Random = new JButton("Semi 3 Random");
172 		newSemi3Random.addActionListener(new ActionListener() {
173 			@Override
174 			public void actionPerformed(ActionEvent e) {
175 				img[0] = generateSemiRandomPicture(w, h, 3);
176 				image.repaint();
177 			}
178 		});
179 
180 		JButton newSemi10Random = new JButton("Semi 10 Random");
181 		newSemi10Random.addActionListener(new ActionListener() {
182 			@Override
183 			public void actionPerformed(ActionEvent e) {
184 				img[0] = generateSemiRandomPicture(w, h, 10);
185 				image.repaint();
186 			}
187 		});
188 
189 		JToolBar tb = new JToolBar();
190 		tb.add(newFullRandom);
191 		tb.add(newSemi3Random);
192 		tb.add(newSemi10Random);
193 
194 		JPanel panel = new JPanel();
195 		panel.setLayout(new BorderLayout());
196 		panel.add(image, BorderLayout.CENTER);
197 		panel.add(tb, BorderLayout.NORTH);
198 
199 		f.getContentPane().setLayout(new BorderLayout());
200 		f.getContentPane().add(panel, BorderLayout.CENTER);
201 		f.pack();
202 		f.addWindowListener(new WindowAdapter() {
203 			@Override
204 			public void windowClosing(WindowEvent e) {
205 				System.exit(0);
206 			}
207 		});
208 
209 		f.setVisible(true);
210 	}
211 }