Blame view

JPAINT/jpaint/ControlClass.java 11.8 KB
933d00ad   rlentieu   add JPaint
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  import javax.swing.JFrame;

  import javax.swing.JTabbedPane;

  import javax.swing.JPanel;

  import javax.swing.JMenuItem;

  import javax.swing.JFileChooser;

  import java.awt.GridBagLayout;

  import java.awt.GridBagConstraints;

  import java.awt.Color;

  import java.awt.Point;

  import java.awt.event.ActionListener;

  import java.awt.event.ActionEvent;

  import java.awt.event.ComponentListener;

  import java.awt.event.ComponentEvent;

  import java.util.ArrayList;

  import java.awt.Robot;

  import java.awt.Rectangle;

  import java.awt.Toolkit;

  import java.awt.AWTException;

  import java.awt.image.BufferedImage;

  import java.io.File;

  import java.io.IOException;

  import javax.imageio.ImageIO;

  import java.awt.print.PrinterException;

  import java.awt.print.PrinterJob;

  import java.awt.print.PageFormat;

  

  /**

   * This is the control class where everything comes together. The menubar, 

   * tabbed pane, the color box and the color pallettes are all created here.

   * Special fields are kept here to keep track of whats going on.

   **/

  public class ControlClass extends JFrame implements ActionListener, ComponentListener

  {

  	public static int fileCount, currentFile, xCoord, yCoord, extraY, numFiles;

  	public static SubToolBox sBox;

  	public static ArrayList canvas;

  	private GridBagLayout layout;

  	private GridBagConstraints c;

  	private File file;

  	private ColorPanel colorPanel;

  	private ColorBox pallette;

  	private JPaintLayoutManager layoutManager;

  	private JPaintFileChooser fileChooser; 

  	private MainMenu mainMenu;

  	private TabbedPanel tabbedCanvasHolder;

  	private ToolBar tBar;

  	

  	/** 

  	 * Default ControlClass constructor.

  	 * Calls run(), which initializes all necessay objects in the ControlClass

  	 **/

  	public ControlClass()

  	{

  		setExtendedState(JFrame.MAXIMIZED_BOTH);

  		run();

  	}

  	

  	/**	

  	 * Calls initAll(), and creates a new BufferedStrategy for the DrawingCanvas 

  	 * with two buffers.

  	 **/

  	public void run()

  	{

  		initAll();

  		((DrawingCanvas)canvas.get(0)).createBufferStrategy(2);

  		extraY = 71;

  		((DrawingCanvas)canvas.get(0)).checkScreenSize();

  		setXY();

  	}

  	

  	/**	

  	 * Instantiates JPaints custom layout manager.

  	 * Places the toolBox on the left side of the window

  	 * and strecthes it vertically.

  	 * Places the colorPallete at the bottom of the window and 

  	 * stretches it horizontally.

  	 * Places the subToolBox inside the toolBox near the top.

  	 * Places the tabbedCanvasHolder in all the space left over

  	 * and places the DrawingCanvas inside the tabbedCanvasHolder, filling

  	 * all avaialbe space 

  	 **/

  	public void initLayout()

  	{

  		layoutManager = new JPaintLayoutManager();

  		layoutManager.setComponent(sBox, 0,1, 1, 0, 100, 0, 0,0, GridBagConstraints.VERTICAL);

  		getContentPane().add(sBox);

  		layoutManager.setComponent(tBar, 0,0, 1,0, 100,0, 0,0, GridBagConstraints.VERTICAL);

  		layoutManager.setAnchor(GridBagConstraints.LINE_START);

  		getContentPane().add(tBar);

  		layoutManager.setComponent(tabbedCanvasHolder, 1 ,0, GridBagConstraints.REMAINDER,GridBagConstraints.RELATIVE, 800,600, 1, 1, GridBagConstraints.HORIZONTAL);

  		getContentPane().add(tabbedCanvasHolder);

    		layoutManager.setComponent(pallette, 1, 1, GridBagConstraints.REMAINDER,1, 350, 100, 0,0,  GridBagConstraints.HORIZONTAL);

  		layoutManager.setAnchor(GridBagConstraints.LAST_LINE_START);

  		getContentPane().add(pallette);		

  	}	

  	

  	/** Instantiates the fileChooser **/

  	public void initFileChooser()

  	{

  		fileChooser = new JPaintFileChooser(this);

  	}

  	

  	/** Instantiates the tool bar **/

  	public void initToolBar()

  	{

  		tBar = new ToolBar();

  		tBar.setSize(200, 200);

  		tBar.setBackground(Color.WHITE);

  	}

  	

  	/** Instantiates the subToolBox **/

  	public void initSubToolBox()

  	{

  		sBox = new SubToolBox();

  		sBox.setBackground(Color.WHITE);

  	}

  	

  	/** Instantiates the layoutManager **/

  	public void initLayoutManager()

  	{

  		layout = new GridBagLayout();

  		c = new GridBagConstraints();

  	 

  	 	getContentPane().setLayout(layout);

  		getContentPane().add(tabbedCanvasHolder);

  	}

  	

  	/** 

  	 * Instantiates the tabbedCanvasHolder with one tab.

  	 * Tabs are positioned at the Top. Calls initFirstCanvas(). Then adds

  	 * the canvas to the tabbedCanvasHolder 

  	 **/

  	public void initTabbedCanvasHolder()

  	{

  		initFirstCanvas();

  

  		// instantiate the canvasHolder and add the initial canvas to it

  		tabbedCanvasHolder = new TabbedPanel(JTabbedPane.TOP);

  		tabbedCanvasHolder.add((DrawingCanvas)canvas.get(0));

  	}

  	

  	/** 

  	 * Instantiates the DrawingCanvas.

  	 * Sets its name to "untitled"+ fileCount, a static int which is also 

  	 * incremented by one in this method. 

  	 **/

  	public void initFirstCanvas()

  	{

  		canvas = new ArrayList();

  		DrawingCanvas tempCanvas = new DrawingCanvas();

  		tempCanvas.setName("untitled-"+fileCount);

  	  	tempCanvas.setBackground(Color.WHITE);

  		canvas.add(tempCanvas);

  	  	fileCount++;

  		numFiles++;

  	}

  	

  	/** Instantiates the colorBox **/

  	public void initPallette()

  	{

  		pallette = new ColorBox();

  	}	

  	

  	/** Instantiates the colorPanel **/

  	public void initColorPanel()

  	{

  		colorPanel = new ColorPanel();

  	}

  	

  	/** Instantiates the MainMenu, which is JPaints menu bar. **/

  	public void initMenuBar()

  	{

  		mainMenu = new MainMenu();

  		mainMenu.addActionListener(this);

  		setJMenuBar(mainMenu);

  	}

  	

  	/** 

  	 * Initializes the frame by setting its title, size, location, listeners, 

  	 * and other default attributes.

  	 **/

  	public void initFrame()

  	{

  		setTitle(".: JPaint :.");

  		getContentPane().setLayout(layoutManager);

  		setDefaultCloseOperation(EXIT_ON_CLOSE);

  		setSize(300, 600);

  		setLocation(0,0);

  		addComponentListener(this);

  		setVisible(true);

  		setXY();

  	}

  	

  	/**

  	 * This code checks sets the X and Y coords of where the drawing canvas

  	 * begins.

  	 **/

  	public void setXY()

  	{

  		xCoord = tBar.getWidth() + 6;

      	yCoord = extraY;

  	}

  	

  	/** Calls all instantiating methods thereby instantiating all objects. **/

  	public void initAll()

  	{

  		initTabbedCanvasHolder();

  		initToolBar();

  		initSubToolBox();

  		initColorPanel();

  		initPallette();

  		initLayout();

  		initFileChooser();

  		initMenuBar();

  		initFrame();

  	}

  		

  	/** 

  	 * Reveals the fileOpen dialog. 

  	 * Retrieves the pathname of the selected file and calls openOperation(path) 

  	 * to display the image to th current canvas.

  	 **/	

  	public void showFileOpen()

  	{

  		int returnVal = fileChooser.showOpenDialog(this);

  		if(returnVal == JFileChooser.APPROVE_OPTION)

  		{

  			String path = fileChooser.getSelectedFile().getPath();

  			((DrawingCanvas)canvas.get(currentFile)).openOperation(path);

  			}

  		}

  

  	/** Allows user to print the image on the screen through any available printer. **/

  	public void printImage()

  	{

  		((DrawingCanvas)canvas.get(currentFile)).saveImageForPrinting();

  		PrinterJob printJob = PrinterJob.getPrinterJob();

  		PageFormat pf = printJob.pageDialog(printJob.defaultPage());

  		printJob.setPrintable(((DrawingCanvas)canvas.get(currentFile)));

  		if(printJob.printDialog()) 

  			try{printJob.print();}catch(PrinterException pe){ pe.printStackTrace();}

  	}

  	

  	/** 

  	 * Saves the image on the current canvas to a user specified path with 

  	 * the .png extension if the image has not previously been saved.

  	 * Otherwise, updates the current file.

  	 **/

  	public void saveImage()

  	{

  		try

  		{

  			DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile));

  			BufferedImage screenCapture = new Robot().createScreenCapture(

  		    new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight()));

  		    

  		    String title = tabbedCanvasHolder.getTitleAt(currentFile);

  		    if(title.indexOf("untitled-") != -1)

  		    	saveImageAs();

  		    else

  		    	try{

  		    		ImageIO.write(screenCapture, "png", file);

  		    	}catch(IOException ioe){}

  	    }catch(AWTException awte){}	

  	}

  	

  	/**	

  	 * Saves the image on the current canvas to a user specified path with 

  	 * the .png extension

  	 **/

  	public void saveImageAs()

  	{

  		try{

  			DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile));

  			BufferedImage screenCapture = new Robot().createScreenCapture(

  	       	new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight()));

  	    	

  			int returnVal = fileChooser.showSaveDialog(this);

  	    	if(returnVal == JFileChooser.APPROVE_OPTION)

  	    	{

  	    		String path = fileChooser.getSelectedFile().getPath();

  				if(path.indexOf(".png") == -1)

  					path+=".png";

  				try

  				{

  					file = new File(path);

  					tabbedCanvasHolder.setTitleAt(currentFile,fileChooser.getName(file));

  					ImageIO.write(screenCapture, "png", file);

  				}catch(IOException ioe){}

  			}

  		}catch(AWTException awte){}	

  	}

  	

  	/** Clears the current canvas of any images **/

  	public void clearImage()

  	{

  		((DrawingCanvas)canvas.get(currentFile)).clearImage();

  	}

  	

  	/** Allows the user to set the visible state of the tool box. **/

  	public void viewToolBoxEvent()

  	{

  		tBar.setVisible(!tBar.isVisible());

  	}

  	

  	/** Allows the user to set the visible state **/ 

  	public void viewColorBoxEvent()

  	{

  		pallette.setVisible(!pallette.isVisible());

  	}

  	

  	/** Displays the help dialog. **/

  	public void helpAboutEvent()

  	{

  		HelpDialog help = new HelpDialog();

  	}

  	

  	/** Exits JPaint. **/

  	public void fileExitEvent()

  	{

  		System.exit(0);

  	}

  	

  	/** 

  	 * Creates a new DrawingCanvas and names it "untitled" + fileCount, a 

  	 * static variable which is incremented in this method.

  	 **/

  	public void fileNewEvent()

  	{

  		DrawingCanvas nextCanvas = new DrawingCanvas();

  		nextCanvas.setBackground(Color.WHITE);

  		nextCanvas.setName("untitled-"+fileCount);

  		tabbedCanvasHolder.add(nextCanvas);

  		canvas.add(nextCanvas);

  		nextCanvas.createBufferStrategy(2);

  		fileCount++;

  		numFiles++;

  	}

  	

  	/** Handles all MenuBar events. **/

  	public void actionPerformed(ActionEvent e)

  	{		

  		JMenuItem hit = (JMenuItem)e.getSource();

  		if(hit.equals(mainMenu.getJMenuItem("fileNew")))

  			fileNewEvent();

  		else if(hit.equals(mainMenu.getJMenuItem("fileExit")))

  			fileExitEvent();

  		else if(hit.equals(mainMenu.getJMenuItem("helpAbout")))

  			helpAboutEvent();

  		else if(hit.equals(mainMenu.getJMenuItem("viewColorBox")))

  			viewColorBoxEvent();

  		else if(hit.equals(mainMenu.getJMenuItem("viewToolBox")))

  			viewToolBoxEvent();

  		else if(hit.equals(mainMenu.getJMenuItem("fileOpen")))

  			showFileOpen();

  		else if(hit.equals(mainMenu.getJMenuItem("fileSave")))

  			saveImage();

  		else if(hit.equals(mainMenu.getJMenuItem("fileSaveAs")))

  			saveImageAs();

  		else if(hit.equals(mainMenu.getJMenuItem("fileClose")))

          	closeProject();

  		else if(hit.equals(mainMenu.getJMenuItem("filePrint")))

  			printImage();

  		else if(hit.equals(mainMenu.getJMenuItem("imageClear")))

  			clearImage();

  		else if(hit.equals(mainMenu.getJMenuItem("imageRotate")))

  			((DrawingCanvas)canvas.get(currentFile)).rotateOperation();

  		else if(hit.equals(mainMenu.getJMenuItem("editCut")))

  			((DrawingCanvas)canvas.get(currentFile)).cutOperation();

  		else if(hit.equals(mainMenu.getJMenuItem("editPaste")))

  			((DrawingCanvas)canvas.get(currentFile)).pasteOperation();

  		else if(hit.equals(mainMenu.getJMenuItem("editCopy")))

  			((DrawingCanvas)canvas.get(currentFile)).copyOperation();

  	}	

  

  	/** Closes an open project. **/

  	public void closeProject()

  	{

          if(numFiles>1)

  		{

              tabbedCanvasHolder.remove(currentFile);

              tabbedCanvasHolder.setSelectedIndex(0);

          	numFiles--;

      	}

  	}

     

     	/** Handles a componentMoved event **/

  	public void componentMoved(ComponentEvent e)

  	{

  	    Point p = getLocation(); 

  	    xCoord = (int)p.getX() + tBar.getWidth() + 6;

  	    yCoord = (int)p.getY() + extraY;

  	}

  	    

  	/** Handles a componentHidden event **/

  	public void componentHidden(ComponentEvent e){}

  	/** Handles a componentResized event **/

  	public void componentResized(ComponentEvent e){} 

  	/** Handles a componentShown event **/

  	public void componentShown(ComponentEvent e){}

  }