Commit 933d00adefe6ec66fc3d19719fb8bee5151a553c
1 parent
593e0a0e
add JPaint
Showing
110 changed files
with
5569 additions
and
0 deletions
Show diff stats
Too many changes.
To preserve performance only 100 of 110 files are displayed.
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,112 @@ |
1 | +import java.awt.Label; | |
2 | +import java.awt.Color; | |
3 | +import java.awt.event.ActionListener; | |
4 | +import java.awt.event.ActionEvent; | |
5 | +import javax.swing.JPanel; | |
6 | +import javax.swing.JButton; | |
7 | + | |
8 | +/** | |
9 | + * The ColorBox class is a color pallette. It stores the color the user clicked | |
10 | + * with the right and left mouse button. It also contains a set of preset colors | |
11 | + * which can be changed with the advanced feature. | |
12 | + **/ | |
13 | +public class ColorBox extends JPanel | |
14 | +{ | |
15 | + public static Label foreGround, backGround; | |
16 | + public static int selectedSwatchX, selectedSwatchY; | |
17 | + public static ColorLabel[][] colorPal; | |
18 | + private JButton clrPnlBtn; | |
19 | + private ColorPanel clrPnl; | |
20 | + | |
21 | + /** Creates a default ColorBox. **/ | |
22 | + public ColorBox() | |
23 | + { | |
24 | + // sets the layout to null and sets a size to the color box | |
25 | + setLayout(null); | |
26 | + setSize(350,120); | |
27 | + // instantiates the Mouse Left and Mouse Right color labels | |
28 | + setForeBack(); | |
29 | + | |
30 | + // instantiates the Color Panel | |
31 | + clrPnl = new ColorPanel(); | |
32 | + | |
33 | + // instantiates a button that calls for advanced settings, sets the size | |
34 | + // and the x,y coordinates and adds it to screen | |
35 | + clrPnlBtn = new JButton("Advanced"); | |
36 | + clrPnlBtn.setBounds(100,65,100,20); | |
37 | + add(clrPnlBtn); | |
38 | + clrPnlBtn.addActionListener(new AdvancedListener()); | |
39 | + | |
40 | + // instantiates the color label and it finds its corresponding color | |
41 | + // depending on its x and y coordinates | |
42 | + colorPal = new ColorLabel[2][14]; | |
43 | + for(int i=0;i<colorPal.length;i++) | |
44 | + for(int j=0;j<colorPal[i].length;j++) | |
45 | + { | |
46 | + Color c = setDefaultColors(i,j); | |
47 | + colorPal[i][j] = new ColorLabel(c,j*20+40,i*20+20,i,j); | |
48 | + add(colorPal[i][j]); | |
49 | + } | |
50 | + | |
51 | + setVisible(true); | |
52 | + } | |
53 | + | |
54 | + /** Sets the ColorLabels with its corresponding default colors **/ | |
55 | + public Color setDefaultColors(int x, int y) | |
56 | + { | |
57 | + if(x == 0 && y == 0) return Color.BLACK; | |
58 | + if(x == 0 && y == 1) return new Color(128,128,128); | |
59 | + if(x == 0 && y == 2) return new Color(128,0,0); | |
60 | + if(x == 0 && y == 3) return new Color(128,128,0); | |
61 | + if(x == 0 && y == 4) return new Color(0,128,0); | |
62 | + if(x == 0 && y == 5) return new Color(0,128,128); | |
63 | + if(x == 0 && y == 6) return new Color(0,0,128); | |
64 | + if(x == 0 && y == 7) return new Color(128,0,128); | |
65 | + if(x == 0 && y == 8) return new Color(128,128,64); | |
66 | + if(x == 0 && y == 9) return new Color(0,64,64); | |
67 | + if(x == 0 && y == 10) return new Color(0,128,255); | |
68 | + if(x == 0 && y == 11) return new Color(0,64,128); | |
69 | + if(x == 0 && y == 12) return new Color(128,0,255); | |
70 | + if(x == 0 && y == 13) return new Color(128,64,0); | |
71 | + | |
72 | + if(x == 1 && y == 0) return Color.WHITE; | |
73 | + if(x == 1 && y == 1) return new Color(192,192,192); | |
74 | + if(x == 1 && y == 2) return Color.RED; | |
75 | + if(x == 1 && y == 3) return Color.YELLOW; | |
76 | + if(x == 1 && y == 4) return Color.GREEN; | |
77 | + if(x == 1 && y == 5) return Color.CYAN; | |
78 | + if(x == 1 && y == 6) return Color.BLUE; | |
79 | + if(x == 1 && y == 7) return Color.MAGENTA; | |
80 | + if(x == 1 && y == 8) return new Color(255,255,128); | |
81 | + if(x == 1 && y == 9) return new Color(0,255,128); | |
82 | + if(x == 1 && y == 10) return new Color(128,255,255); | |
83 | + if(x == 1 && y == 11) return new Color(128,128,255); | |
84 | + if(x == 1 && y == 12) return new Color(255,0,128); | |
85 | + if(x == 1 && y == 13) return new Color(255,128,64); | |
86 | + | |
87 | + return Color.WHITE; | |
88 | + } | |
89 | + | |
90 | + /** Instantiates the mouse right and left click colors **/ | |
91 | + public void setForeBack() | |
92 | + { | |
93 | + // creates the foreground and background label and then sets the size | |
94 | + // and its x,y coordinates and adds it to the Color Box | |
95 | + foreGround = new Label(); | |
96 | + backGround = new Label(); | |
97 | + foreGround.setBounds(10,10,15,15); | |
98 | + foreGround.setBackground(new Color(0,0,0)); | |
99 | + backGround.setBounds(17,17,15,15); | |
100 | + backGround.setBackground(new Color(255,255,255)); | |
101 | + add(foreGround); | |
102 | + add(backGround); | |
103 | + } | |
104 | + | |
105 | + class AdvancedListener implements ActionListener | |
106 | + { | |
107 | + public void actionPerformed(ActionEvent e) | |
108 | + { | |
109 | + clrPnl.setVisible(true); | |
110 | + } | |
111 | + } | |
112 | +} | |
0 | 113 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,112 @@ |
1 | +import java.awt.Label; | |
2 | +import java.awt.Color; | |
3 | +import java.awt.event.ActionListener; | |
4 | +import java.awt.event.ActionEvent; | |
5 | +import javax.swing.JPanel; | |
6 | +import javax.swing.JButton; | |
7 | + | |
8 | +/** | |
9 | + * The ColorBox class is a color pallette. It stores the color the user clicked | |
10 | + * with the right and left mouse button. It also contains a set of preset colors | |
11 | + * which can be changed with the advanced feature. | |
12 | + **/ | |
13 | +public class ColorBox extends JPanel | |
14 | +{ | |
15 | + public static Label foreGround, backGround; | |
16 | + public static int selectedSwatchX, selectedSwatchY; | |
17 | + public static ColorLabel[][] colorPal; | |
18 | + private JButton clrPnlBtn; | |
19 | + private ColorPanel clrPnl; | |
20 | + | |
21 | + /** Creates a default ColorBox. **/ | |
22 | + public ColorBox() | |
23 | + { | |
24 | + // sets the layout to null and sets a size to the color box | |
25 | + setLayout(null); | |
26 | + setSize(350,120); | |
27 | + // instantiates the Mouse Left and Mouse Right color labels | |
28 | + setForeBack(); | |
29 | + | |
30 | + // instantiates the Color Panel | |
31 | + clrPnl = new ColorPanel(); | |
32 | + | |
33 | + // instantiates a button that calls for advanced settings, sets the size | |
34 | + // and the x,y coordinates and adds it to screen | |
35 | + clrPnlBtn = new JButton("Advanced"); | |
36 | + clrPnlBtn.setBounds(100,65,100,20); | |
37 | + add(clrPnlBtn); | |
38 | + clrPnlBtn.addActionListener(new AdvancedListener()); | |
39 | + | |
40 | + // instantiates the color label and it finds its corresponding color | |
41 | + // depending on its x and y coordinates | |
42 | + colorPal = new ColorLabel[2][14]; | |
43 | + for(int i=0;i<colorPal.length;i++) | |
44 | + for(int j=0;j<colorPal[i].length;j++) | |
45 | + { | |
46 | + Color c = setDefaultColors(i,j); | |
47 | + colorPal[i][j] = new ColorLabel(c,j*20+40,i*20+20,i,j); | |
48 | + add(colorPal[i][j]); | |
49 | + } | |
50 | + | |
51 | + setVisible(true); | |
52 | + } | |
53 | + | |
54 | + /** Sets the ColorLabels with its corresponding default colors **/ | |
55 | + public Color setDefaultColors(int x, int y) | |
56 | + { | |
57 | + if(x == 0 && y == 0) return Color.BLACK; | |
58 | + if(x == 0 && y == 1) return new Color(128,128,128); | |
59 | + if(x == 0 && y == 2) return new Color(128,0,0); | |
60 | + if(x == 0 && y == 3) return new Color(128,128,0); | |
61 | + if(x == 0 && y == 4) return new Color(0,128,0); | |
62 | + if(x == 0 && y == 5) return new Color(0,128,128); | |
63 | + if(x == 0 && y == 6) return new Color(0,0,128); | |
64 | + if(x == 0 && y == 7) return new Color(128,0,128); | |
65 | + if(x == 0 && y == 8) return new Color(128,128,64); | |
66 | + if(x == 0 && y == 9) return new Color(0,64,64); | |
67 | + if(x == 0 && y == 10) return new Color(0,128,255); | |
68 | + if(x == 0 && y == 11) return new Color(0,64,128); | |
69 | + if(x == 0 && y == 12) return new Color(128,0,255); | |
70 | + if(x == 0 && y == 13) return new Color(128,64,0); | |
71 | + | |
72 | + if(x == 1 && y == 0) return Color.WHITE; | |
73 | + if(x == 1 && y == 1) return new Color(192,192,192); | |
74 | + if(x == 1 && y == 2) return Color.RED; | |
75 | + if(x == 1 && y == 3) return Color.YELLOW; | |
76 | + if(x == 1 && y == 4) return Color.GREEN; | |
77 | + if(x == 1 && y == 5) return Color.CYAN; | |
78 | + if(x == 1 && y == 6) return Color.BLUE; | |
79 | + if(x == 1 && y == 7) return Color.MAGENTA; | |
80 | + if(x == 1 && y == 8) return new Color(255,255,128); | |
81 | + if(x == 1 && y == 9) return new Color(0,255,128); | |
82 | + if(x == 1 && y == 10) return new Color(128,255,255); | |
83 | + if(x == 1 && y == 11) return new Color(128,128,255); | |
84 | + if(x == 1 && y == 12) return new Color(255,0,128); | |
85 | + if(x == 1 && y == 13) return new Color(255,128,64); | |
86 | + | |
87 | + return Color.WHITE; | |
88 | + } | |
89 | + | |
90 | + /** Instantiates the mouse right and left click colors **/ | |
91 | + public void setForeBack() | |
92 | + { | |
93 | + // creates the foreground and background label and then sets the size | |
94 | + // and its x,y coordinates and adds it to the Color Box | |
95 | + foreGround = new Label(); | |
96 | + backGround = new Label(); | |
97 | + foreGround.setBounds(10,10,15,15); | |
98 | + foreGround.setBackground(new Color(255,255,255)); | |
99 | + backGround.setBounds(17,17,15,15); | |
100 | + backGround.setBackground(new Color(255,255,255)); | |
101 | + add(foreGround); | |
102 | + add(backGround); | |
103 | + } | |
104 | + | |
105 | + class AdvancedListener implements ActionListener | |
106 | + { | |
107 | + public void actionPerformed(ActionEvent e) | |
108 | + { | |
109 | + clrPnl.setVisible(true); | |
110 | + } | |
111 | + } | |
112 | +} | |
0 | 113 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,77 @@ |
1 | +import java.awt.Color; | |
2 | +import java.awt.Label; | |
3 | +import java.awt.event.MouseListener; | |
4 | +import java.awt.event.MouseEvent; | |
5 | + | |
6 | +/** | |
7 | + * ColorLabel is an extension of a label with an id field and coordinates. It | |
8 | + * has a listener on it to set the Mouse Left and Right click with the corresponding | |
9 | + * background color. | |
10 | + **/ | |
11 | +public class ColorLabel extends Label implements MouseListener | |
12 | +{ | |
13 | + private int x, y, idX, idY; | |
14 | + private Color background; | |
15 | + | |
16 | + /** Creates a default ColorLabel. **/ | |
17 | + public ColorLabel(){} | |
18 | + | |
19 | + /** | |
20 | + * Creates a ColorLabel which takes in a color, coordinates and a special id. | |
21 | + * | |
22 | + * Example: | |
23 | + * new ColorLabel(Color.Blue,5,5,5,5); | |
24 | + **/ | |
25 | + public ColorLabel(Color background, int x, int y, int idX, int idY) | |
26 | + { | |
27 | + // sets a listener, sets x and y coordinates and the background and | |
28 | + // sets the fields with the corresponding values | |
29 | + addMouseListener(this); | |
30 | + setBounds(x,y,15,15); | |
31 | + setBackground(background); | |
32 | + this.background = background; | |
33 | + this.x = x; | |
34 | + this.y = y; | |
35 | + this.idX = idX; | |
36 | + this.idY = idY; | |
37 | + } | |
38 | + | |
39 | + /** | |
40 | + * Overwrites the setBackground so it sets the background and stores it in | |
41 | + * a field of its own. | |
42 | + **/ | |
43 | + public void setBackground(Color background) | |
44 | + { | |
45 | + // sets the background and sets the field to the value passed through | |
46 | + // the parameter | |
47 | + this.background = background; | |
48 | + super.setBackground(background); | |
49 | + } | |
50 | + | |
51 | + /** | |
52 | + * Invoked when the mouse button has been clicked (pressed and released) on a component. | |
53 | + * Sets the corresponding color to the Mouse Left and Mouse Right fields and | |
54 | + * save the ID of the last color picked. | |
55 | + **/ | |
56 | + public void mouseClicked(MouseEvent e) | |
57 | + { | |
58 | + if(e.getButton() == MouseEvent.BUTTON1) | |
59 | + ColorBox.foreGround.setBackground(background); | |
60 | + | |
61 | + if(e.getButton() == MouseEvent.BUTTON3) | |
62 | + ColorBox.backGround.setBackground(background); | |
63 | + | |
64 | + // lets the program know which is the current swatch selected | |
65 | + ColorBox.selectedSwatchX = idX; | |
66 | + ColorBox.selectedSwatchY = idY; | |
67 | + } | |
68 | + | |
69 | + /** Invoked when a mouse button has been released on a component.**/ | |
70 | + public void mouseReleased(MouseEvent e){} | |
71 | + /** Invoked when a mouse button has been pressed on a component. **/ | |
72 | + public void mousePressed(MouseEvent e){} | |
73 | + /** Invoked when the mouse enters a component.**/ | |
74 | + public void mouseEntered(MouseEvent e){} | |
75 | + /** Invoked when the mouse exits a component. **/ | |
76 | + public void mouseExited(MouseEvent e){} | |
77 | +} | |
0 | 78 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,47 @@ |
1 | +import java.awt.HeadlessException; | |
2 | +import java.awt.event.ActionListener; | |
3 | +import java.awt.event.ActionEvent; | |
4 | +import javax.swing.JColorChooser; | |
5 | +import javax.swing.JDialog; | |
6 | +import javax.swing.JPanel; | |
7 | +import javax.swing.JButton; | |
8 | + | |
9 | +/** | |
10 | + * ColorPanel creates a JColorChooser and allows the user to choose a new color | |
11 | + * to change with the selected swatch. | |
12 | + **/ | |
13 | +public class ColorPanel extends JPanel implements ActionListener | |
14 | +{ | |
15 | + private JColorChooser jcc; | |
16 | + private JDialog jccHolder; | |
17 | + | |
18 | + /** Creates a default ColorBox. **/ | |
19 | + public ColorPanel() | |
20 | + { | |
21 | + // instantiates the JColorChooser and sets it visible. | |
22 | + jcc = new JColorChooser(); | |
23 | + try{ | |
24 | + jccHolder = jcc.createDialog(this, "Color Panel", false, jcc, this, this); | |
25 | + } | |
26 | + catch(HeadlessException he){} | |
27 | + } | |
28 | + | |
29 | + /** Sets the JDialog's visible attribute true or false. **/ | |
30 | + public void setVisible(boolean visible) | |
31 | + { | |
32 | + jccHolder.setVisible(visible); | |
33 | + } | |
34 | + | |
35 | + /** Changes the color you selected with the last selected swatch. **/ | |
36 | + public void actionPerformed(ActionEvent e) | |
37 | + { | |
38 | + // if the okay button is clicked then change the selected swatch and | |
39 | + // the left mouse click color | |
40 | + JButton button = (JButton)e.getSource(); | |
41 | + if(button.getText() == "OK") | |
42 | + { | |
43 | + ColorBox.colorPal[ColorBox.selectedSwatchX][ColorBox.selectedSwatchY].setBackground(jcc.getColor()); | |
44 | + ColorBox.foreGround.setBackground(jcc.getColor()); | |
45 | + } | |
46 | + } | |
47 | +} | |
0 | 48 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,401 @@ |
1 | +import javax.swing.JFrame; | |
2 | +import javax.swing.JTabbedPane; | |
3 | +import javax.swing.JPanel; | |
4 | +import javax.swing.JMenuItem; | |
5 | +import javax.swing.JFileChooser; | |
6 | +import java.awt.GridBagLayout; | |
7 | +import java.awt.GridBagConstraints; | |
8 | +import java.awt.Color; | |
9 | +import java.awt.Point; | |
10 | +import java.awt.event.ActionListener; | |
11 | +import java.awt.event.ActionEvent; | |
12 | +import java.awt.event.ComponentListener; | |
13 | +import java.awt.event.ComponentEvent; | |
14 | +import java.util.ArrayList; | |
15 | +import java.awt.Robot; | |
16 | +import java.awt.Rectangle; | |
17 | +import java.awt.Toolkit; | |
18 | +import java.awt.AWTException; | |
19 | +import java.awt.image.BufferedImage; | |
20 | +import java.io.File; | |
21 | +import java.io.IOException; | |
22 | +import javax.imageio.ImageIO; | |
23 | +import java.awt.print.PrinterException; | |
24 | +import java.awt.print.PrinterJob; | |
25 | +import java.awt.print.PageFormat; | |
26 | + | |
27 | +/** | |
28 | + * This is the control class where everything comes together. The menubar, | |
29 | + * tabbed pane, the color box and the color pallettes are all created here. | |
30 | + * Special fields are kept here to keep track of whats going on. | |
31 | + **/ | |
32 | +public class ControlClass extends JFrame implements ActionListener, ComponentListener | |
33 | +{ | |
34 | + public static int fileCount, currentFile, xCoord, yCoord, extraY, numFiles; | |
35 | + public static SubToolBox sBox; | |
36 | + public static ArrayList canvas; | |
37 | + private GridBagLayout layout; | |
38 | + private GridBagConstraints c; | |
39 | + private File file; | |
40 | + private ColorPanel colorPanel; | |
41 | + private ColorBox pallette; | |
42 | + private JPaintLayoutManager layoutManager; | |
43 | + private JPaintFileChooser fileChooser; | |
44 | + private MainMenu mainMenu; | |
45 | + private TabbedPanel tabbedCanvasHolder; | |
46 | + private ToolBar tBar; | |
47 | + | |
48 | + /** | |
49 | + * Default ControlClass constructor. | |
50 | + * Calls run(), which initializes all necessay objects in the ControlClass | |
51 | + **/ | |
52 | + public ControlClass() | |
53 | + { | |
54 | + setExtendedState(JFrame.MAXIMIZED_BOTH); | |
55 | + run(); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * Calls initAll(), and creates a new BufferedStrategy for the DrawingCanvas | |
60 | + * with two buffers. | |
61 | + **/ | |
62 | + public void run() | |
63 | + { | |
64 | + initAll(); | |
65 | + ((DrawingCanvas)canvas.get(0)).createBufferStrategy(2); | |
66 | + extraY = 71; | |
67 | + ((DrawingCanvas)canvas.get(0)).checkScreenSize(); | |
68 | + setXY(); | |
69 | + } | |
70 | + | |
71 | + /** | |
72 | + * Instantiates JPaints custom layout manager. | |
73 | + * Places the toolBox on the left side of the window | |
74 | + * and strecthes it vertically. | |
75 | + * Places the colorPallete at the bottom of the window and | |
76 | + * stretches it horizontally. | |
77 | + * Places the subToolBox inside the toolBox near the top. | |
78 | + * Places the tabbedCanvasHolder in all the space left over | |
79 | + * and places the DrawingCanvas inside the tabbedCanvasHolder, filling | |
80 | + * all avaialbe space | |
81 | + **/ | |
82 | + public void initLayout() | |
83 | + { | |
84 | + layoutManager = new JPaintLayoutManager(); | |
85 | + layoutManager.setComponent(sBox, 0,1, 1, 0, 100, 0, 0,0, GridBagConstraints.VERTICAL); | |
86 | + getContentPane().add(sBox); | |
87 | + layoutManager.setComponent(tBar, 0,0, 1,0, 100,0, 0,0, GridBagConstraints.VERTICAL); | |
88 | + layoutManager.setAnchor(GridBagConstraints.LINE_START); | |
89 | + getContentPane().add(tBar); | |
90 | + layoutManager.setComponent(tabbedCanvasHolder, 1 ,0, GridBagConstraints.REMAINDER,GridBagConstraints.RELATIVE, 800,600, 1, 1, GridBagConstraints.HORIZONTAL); | |
91 | + getContentPane().add(tabbedCanvasHolder); | |
92 | + layoutManager.setComponent(pallette, 1, 1, GridBagConstraints.REMAINDER,1, 350, 100, 0,0, GridBagConstraints.HORIZONTAL); | |
93 | + layoutManager.setAnchor(GridBagConstraints.LAST_LINE_START); | |
94 | + getContentPane().add(pallette); | |
95 | + } | |
96 | + | |
97 | + /** Instantiates the fileChooser **/ | |
98 | + public void initFileChooser() | |
99 | + { | |
100 | + fileChooser = new JPaintFileChooser(this); | |
101 | + } | |
102 | + | |
103 | + /** Instantiates the tool bar **/ | |
104 | + public void initToolBar() | |
105 | + { | |
106 | + tBar = new ToolBar(); | |
107 | + tBar.setSize(200, 200); | |
108 | + tBar.setBackground(Color.WHITE); | |
109 | + } | |
110 | + | |
111 | + /** Instantiates the subToolBox **/ | |
112 | + public void initSubToolBox() | |
113 | + { | |
114 | + sBox = new SubToolBox(); | |
115 | + sBox.setBackground(Color.WHITE); | |
116 | + } | |
117 | + | |
118 | + /** Instantiates the layoutManager **/ | |
119 | + public void initLayoutManager() | |
120 | + { | |
121 | + layout = new GridBagLayout(); | |
122 | + c = new GridBagConstraints(); | |
123 | + | |
124 | + getContentPane().setLayout(layout); | |
125 | + getContentPane().add(tabbedCanvasHolder); | |
126 | + } | |
127 | + | |
128 | + /** | |
129 | + * Instantiates the tabbedCanvasHolder with one tab. | |
130 | + * Tabs are positioned at the Top. Calls initFirstCanvas(). Then adds | |
131 | + * the canvas to the tabbedCanvasHolder | |
132 | + **/ | |
133 | + public void initTabbedCanvasHolder() | |
134 | + { | |
135 | + initFirstCanvas(); | |
136 | + | |
137 | + // instantiate the canvasHolder and add the initial canvas to it | |
138 | + tabbedCanvasHolder = new TabbedPanel(JTabbedPane.TOP); | |
139 | + tabbedCanvasHolder.add((DrawingCanvas)canvas.get(0)); | |
140 | + } | |
141 | + | |
142 | + /** | |
143 | + * Instantiates the DrawingCanvas. | |
144 | + * Sets its name to "untitled"+ fileCount, a static int which is also | |
145 | + * incremented by one in this method. | |
146 | + **/ | |
147 | + public void initFirstCanvas() | |
148 | + { | |
149 | + canvas = new ArrayList(); | |
150 | + DrawingCanvas tempCanvas = new DrawingCanvas(); | |
151 | + tempCanvas.setName("untitled-"+fileCount); | |
152 | + tempCanvas.setBackground(Color.WHITE); | |
153 | + canvas.add(tempCanvas); | |
154 | + fileCount++; | |
155 | + numFiles++; | |
156 | + } | |
157 | + | |
158 | + /** Instantiates the colorBox **/ | |
159 | + public void initPallette() | |
160 | + { | |
161 | + pallette = new ColorBox(); | |
162 | + } | |
163 | + | |
164 | + /** Instantiates the colorPanel **/ | |
165 | + public void initColorPanel() | |
166 | + { | |
167 | + colorPanel = new ColorPanel(); | |
168 | + } | |
169 | + | |
170 | + /** Instantiates the MainMenu, which is JPaints menu bar. **/ | |
171 | + public void initMenuBar() | |
172 | + { | |
173 | + mainMenu = new MainMenu(); | |
174 | + mainMenu.addActionListener(this); | |
175 | + setJMenuBar(mainMenu); | |
176 | + } | |
177 | + | |
178 | + /** | |
179 | + * Initializes the frame by setting its title, size, location, listeners, | |
180 | + * and other default attributes. | |
181 | + **/ | |
182 | + public void initFrame() | |
183 | + { | |
184 | + setTitle(".: JPaint :."); | |
185 | + getContentPane().setLayout(layoutManager); | |
186 | + setDefaultCloseOperation(EXIT_ON_CLOSE); | |
187 | + setSize(300, 600); | |
188 | + setLocation(0,0); | |
189 | + addComponentListener(this); | |
190 | + setVisible(true); | |
191 | + setXY(); | |
192 | + } | |
193 | + | |
194 | + /** | |
195 | + * This code checks sets the X and Y coords of where the drawing canvas | |
196 | + * begins. | |
197 | + **/ | |
198 | + public void setXY() | |
199 | + { | |
200 | + xCoord = tBar.getWidth() + 6; | |
201 | + yCoord = extraY; | |
202 | + } | |
203 | + | |
204 | + /** Calls all instantiating methods thereby instantiating all objects. **/ | |
205 | + public void initAll() | |
206 | + { | |
207 | + initTabbedCanvasHolder(); | |
208 | + initToolBar(); | |
209 | + initSubToolBox(); | |
210 | + initColorPanel(); | |
211 | + initPallette(); | |
212 | + initLayout(); | |
213 | + initFileChooser(); | |
214 | + initMenuBar(); | |
215 | + initFrame(); | |
216 | + } | |
217 | + | |
218 | + /** | |
219 | + * Reveals the fileOpen dialog. | |
220 | + * Retrieves the pathname of the selected file and calls openOperation(path) | |
221 | + * to display the image to th current canvas. | |
222 | + **/ | |
223 | + public void showFileOpen() | |
224 | + { | |
225 | + int returnVal = fileChooser.showOpenDialog(this); | |
226 | + if(returnVal == JFileChooser.APPROVE_OPTION) | |
227 | + { | |
228 | + String path = fileChooser.getSelectedFile().getPath(); | |
229 | + ((DrawingCanvas)canvas.get(currentFile)).openOperation(path); | |
230 | + } | |
231 | + } | |
232 | + | |
233 | + /** Allows user to print the image on the screen through any available printer. **/ | |
234 | + public void printImage() | |
235 | + { | |
236 | + ((DrawingCanvas)canvas.get(currentFile)).saveImageForPrinting(); | |
237 | + PrinterJob printJob = PrinterJob.getPrinterJob(); | |
238 | + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); | |
239 | + printJob.setPrintable(((DrawingCanvas)canvas.get(currentFile))); | |
240 | + if(printJob.printDialog()) | |
241 | + try{printJob.print();}catch(PrinterException pe){ pe.printStackTrace();} | |
242 | + } | |
243 | + | |
244 | + /** | |
245 | + * Saves the image on the current canvas to a user specified path with | |
246 | + * the .png extension if the image has not previously been saved. | |
247 | + * Otherwise, updates the current file. | |
248 | + **/ | |
249 | + public void saveImage() | |
250 | + { | |
251 | + try | |
252 | + { | |
253 | + DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile)); | |
254 | + BufferedImage screenCapture = new Robot().createScreenCapture( | |
255 | + new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight())); | |
256 | + | |
257 | + String title = tabbedCanvasHolder.getTitleAt(currentFile); | |
258 | + if(title.indexOf("untitled-") != -1) | |
259 | + saveImageAs(); | |
260 | + else | |
261 | + try{ | |
262 | + ImageIO.write(screenCapture, "png", file); | |
263 | + }catch(IOException ioe){} | |
264 | + }catch(AWTException awte){} | |
265 | + } | |
266 | + | |
267 | + /** | |
268 | + * Saves the image on the current canvas to a user specified path with | |
269 | + * the .png extension | |
270 | + **/ | |
271 | + public void saveImageAs() | |
272 | + { | |
273 | + try{ | |
274 | + DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile)); | |
275 | + BufferedImage screenCapture = new Robot().createScreenCapture( | |
276 | + new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight())); | |
277 | + | |
278 | + int returnVal = fileChooser.showSaveDialog(this); | |
279 | + if(returnVal == JFileChooser.APPROVE_OPTION) | |
280 | + { | |
281 | + String path = fileChooser.getSelectedFile().getPath(); | |
282 | + if(path.indexOf(".png") == -1) | |
283 | + path+=".png"; | |
284 | + try | |
285 | + { | |
286 | + file = new File(path); | |
287 | + tabbedCanvasHolder.setTitleAt(currentFile,fileChooser.getName(file)); | |
288 | + ImageIO.write(screenCapture, "png", file); | |
289 | + }catch(IOException ioe){} | |
290 | + } | |
291 | + }catch(AWTException awte){} | |
292 | + } | |
293 | + | |
294 | + /** Clears the current canvas of any images **/ | |
295 | + public void clearImage() | |
296 | + { | |
297 | + ((DrawingCanvas)canvas.get(currentFile)).clearImage(); | |
298 | + } | |
299 | + | |
300 | + /** Allows the user to set the visible state of the tool box. **/ | |
301 | + public void viewToolBoxEvent() | |
302 | + { | |
303 | + tBar.setVisible(!tBar.isVisible()); | |
304 | + } | |
305 | + | |
306 | + /** Allows the user to set the visible state **/ | |
307 | + public void viewColorBoxEvent() | |
308 | + { | |
309 | + pallette.setVisible(!pallette.isVisible()); | |
310 | + } | |
311 | + | |
312 | + /** Displays the help dialog. **/ | |
313 | + public void helpAboutEvent() | |
314 | + { | |
315 | + HelpDialog help = new HelpDialog(); | |
316 | + } | |
317 | + | |
318 | + /** Exits JPaint. **/ | |
319 | + public void fileExitEvent() | |
320 | + { | |
321 | + System.exit(0); | |
322 | + } | |
323 | + | |
324 | + /** | |
325 | + * Creates a new DrawingCanvas and names it "untitled" + fileCount, a | |
326 | + * static variable which is incremented in this method. | |
327 | + **/ | |
328 | + public void fileNewEvent() | |
329 | + { | |
330 | + DrawingCanvas nextCanvas = new DrawingCanvas(); | |
331 | + nextCanvas.setBackground(Color.WHITE); | |
332 | + nextCanvas.setName("untitled-"+fileCount); | |
333 | + tabbedCanvasHolder.add(nextCanvas); | |
334 | + canvas.add(nextCanvas); | |
335 | + nextCanvas.createBufferStrategy(2); | |
336 | + fileCount++; | |
337 | + numFiles++; | |
338 | + } | |
339 | + | |
340 | + /** Handles all MenuBar events. **/ | |
341 | + public void actionPerformed(ActionEvent e) | |
342 | + { | |
343 | + JMenuItem hit = (JMenuItem)e.getSource(); | |
344 | + if(hit.equals(mainMenu.getJMenuItem("fileNew"))) | |
345 | + fileNewEvent(); | |
346 | + else if(hit.equals(mainMenu.getJMenuItem("fileExit"))) | |
347 | + fileExitEvent(); | |
348 | + else if(hit.equals(mainMenu.getJMenuItem("helpAbout"))) | |
349 | + helpAboutEvent(); | |
350 | + else if(hit.equals(mainMenu.getJMenuItem("viewColorBox"))) | |
351 | + viewColorBoxEvent(); | |
352 | + else if(hit.equals(mainMenu.getJMenuItem("viewToolBox"))) | |
353 | + viewToolBoxEvent(); | |
354 | + else if(hit.equals(mainMenu.getJMenuItem("fileOpen"))) | |
355 | + showFileOpen(); | |
356 | + else if(hit.equals(mainMenu.getJMenuItem("fileSave"))) | |
357 | + saveImage(); | |
358 | + else if(hit.equals(mainMenu.getJMenuItem("fileSaveAs"))) | |
359 | + saveImageAs(); | |
360 | + else if(hit.equals(mainMenu.getJMenuItem("fileClose"))) | |
361 | + closeProject(); | |
362 | + else if(hit.equals(mainMenu.getJMenuItem("filePrint"))) | |
363 | + printImage(); | |
364 | + else if(hit.equals(mainMenu.getJMenuItem("imageClear"))) | |
365 | + clearImage(); | |
366 | + else if(hit.equals(mainMenu.getJMenuItem("imageRotate"))) | |
367 | + ((DrawingCanvas)canvas.get(currentFile)).rotateOperation(); | |
368 | + else if(hit.equals(mainMenu.getJMenuItem("editCut"))) | |
369 | + ((DrawingCanvas)canvas.get(currentFile)).cutOperation(); | |
370 | + else if(hit.equals(mainMenu.getJMenuItem("editPaste"))) | |
371 | + ((DrawingCanvas)canvas.get(currentFile)).pasteOperation(); | |
372 | + else if(hit.equals(mainMenu.getJMenuItem("editCopy"))) | |
373 | + ((DrawingCanvas)canvas.get(currentFile)).copyOperation(); | |
374 | + } | |
375 | + | |
376 | + /** Closes an open project. **/ | |
377 | + public void closeProject() | |
378 | + { | |
379 | + if(numFiles>1) | |
380 | + { | |
381 | + tabbedCanvasHolder.remove(currentFile); | |
382 | + tabbedCanvasHolder.setSelectedIndex(0); | |
383 | + numFiles--; | |
384 | + } | |
385 | + } | |
386 | + | |
387 | + /** Handles a componentMoved event **/ | |
388 | + public void componentMoved(ComponentEvent e) | |
389 | + { | |
390 | + Point p = getLocation(); | |
391 | + xCoord = (int)p.getX() + tBar.getWidth() + 6; | |
392 | + yCoord = (int)p.getY() + extraY; | |
393 | + } | |
394 | + | |
395 | + /** Handles a componentHidden event **/ | |
396 | + public void componentHidden(ComponentEvent e){} | |
397 | + /** Handles a componentResized event **/ | |
398 | + public void componentResized(ComponentEvent e){} | |
399 | + /** Handles a componentShown event **/ | |
400 | + public void componentShown(ComponentEvent e){} | |
401 | +} | |
0 | 402 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,401 @@ |
1 | +import javax.swing.JFrame; | |
2 | +import javax.swing.JTabbedPane; | |
3 | +import javax.swing.JPanel; | |
4 | +import javax.swing.JMenuItem; | |
5 | +import javax.swing.JFileChooser; | |
6 | +import java.awt.GridBagLayout; | |
7 | +import java.awt.GridBagConstraints; | |
8 | +import java.awt.Color; | |
9 | +import java.awt.Point; | |
10 | +import java.awt.event.ActionListener; | |
11 | +import java.awt.event.ActionEvent; | |
12 | +import java.awt.event.ComponentListener; | |
13 | +import java.awt.event.ComponentEvent; | |
14 | +import java.util.ArrayList; | |
15 | +import java.awt.Robot; | |
16 | +import java.awt.Rectangle; | |
17 | +import java.awt.Toolkit; | |
18 | +import java.awt.AWTException; | |
19 | +import java.awt.image.BufferedImage; | |
20 | +import java.io.File; | |
21 | +import java.io.IOException; | |
22 | +import javax.imageio.ImageIO; | |
23 | +import java.awt.print.PrinterException; | |
24 | +import java.awt.print.PrinterJob; | |
25 | +import java.awt.print.PageFormat; | |
26 | + | |
27 | +/** | |
28 | + * This is the control class where everything comes together. The menubar, | |
29 | + * tabbed pane, the color box and the color pallettes are all created here. | |
30 | + * Special fields are kept here to keep track of whats going on. | |
31 | + **/ | |
32 | +public class ControlClass extends JFrame implements ActionListener, ComponentListener | |
33 | +{ | |
34 | + public static int fileCount, currentFile, xCoord, yCoord, extraY, numFiles; | |
35 | + public static SubToolBox sBox; | |
36 | + public static ArrayList canvas; | |
37 | + private GridBagLayout layout; | |
38 | + private GridBagConstraints c; | |
39 | + private File file; | |
40 | + private ColorPanel colorPanel; | |
41 | + private ColorBox pallette; | |
42 | + private JPaintLayoutManager layoutManager; | |
43 | + private JPaintFileChooser fileChooser; | |
44 | + private MainMenu mainMenu; | |
45 | + private TabbedPanel tabbedCanvasHolder; | |
46 | + private ToolBar tBar; | |
47 | + | |
48 | + /** | |
49 | + * Default ControlClass constructor. | |
50 | + * Calls run(), which initializes all necessay objects in the ControlClass | |
51 | + **/ | |
52 | + public ControlClass() | |
53 | + { | |
54 | + setExtendedState(JFrame.MAXIMIZED_BOTH); | |
55 | + run(); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * Calls initAll(), and creates a new BufferedStrategy for the DrawingCanvas | |
60 | + * with two buffers. | |
61 | + **/ | |
62 | + public void run() | |
63 | + { | |
64 | + initAll(); | |
65 | + ((DrawingCanvas)canvas.get(0)).createBufferStrategy(2); | |
66 | + extraY = 71; | |
67 | + ((DrawingCanvas)canvas.get(0)).checkScreenSize(); | |
68 | + setXY(); | |
69 | + } | |
70 | + | |
71 | + /** | |
72 | + * Instantiates JPaints custom layout manager. | |
73 | + * Places the toolBox on the left side of the window | |
74 | + * and strecthes it vertically. | |
75 | + * Places the colorPallete at the bottom of the window and | |
76 | + * stretches it horizontally. | |
77 | + * Places the subToolBox inside the toolBox near the top. | |
78 | + * Places the tabbedCanvasHolder in all the space left over | |
79 | + * and places the DrawingCanvas inside the tabbedCanvasHolder, filling | |
80 | + * all avaialbe space | |
81 | + **/ | |
82 | + public void initLayout() | |
83 | + { | |
84 | + layoutManager = new JPaintLayoutManager(); | |
85 | + layoutManager.setComponent(sBox, 0,1, 1, 0, 100, 0, 0,0, GridBagConstraints.VERTICAL); | |
86 | + getContentPane().add(sBox); | |
87 | + layoutManager.setComponent(tBar, 0,0, 1,0, 100,0, 0,0, GridBagConstraints.VERTICAL); | |
88 | + layoutManager.setAnchor(GridBagConstraints.LINE_START); | |
89 | + getContentPane().add(tBar); | |
90 | + layoutManager.setComponent(tabbedCanvasHolder, 1 ,0, GridBagConstraints.REMAINDER,GridBagConstraints.RELATIVE, 800,600, 1, 1, GridBagConstraints.HORIZONTAL); | |
91 | + getContentPane().add(tabbedCanvasHolder); | |
92 | + layoutManager.setComponent(pallette, 1, 1, GridBagConstraints.REMAINDER,1, 350, 100, 0,0, GridBagConstraints.HORIZONTAL); | |
93 | + layoutManager.setAnchor(GridBagConstraints.LAST_LINE_START); | |
94 | + getContentPane().add(pallette); | |
95 | + } | |
96 | + | |
97 | + /** Instantiates the fileChooser **/ | |
98 | + public void initFileChooser() | |
99 | + { | |
100 | + fileChooser = new JPaintFileChooser(this); | |
101 | + } | |
102 | + | |
103 | + /** Instantiates the tool bar **/ | |
104 | + public void initToolBar() | |
105 | + { | |
106 | + tBar = new ToolBar(); | |
107 | + tBar.setSize(200, 200); | |
108 | + tBar.setBackground(Color.WHITE); | |
109 | + } | |
110 | + | |
111 | + /** Instantiates the subToolBox **/ | |
112 | + public void initSubToolBox() | |
113 | + { | |
114 | + sBox = new SubToolBox(); | |
115 | + sBox.setBackground(Color.WHITE); | |
116 | + } | |
117 | + | |
118 | + /** Instantiates the layoutManager **/ | |
119 | + public void initLayoutManager() | |
120 | + { | |
121 | + layout = new GridBagLayout(); | |
122 | + c = new GridBagConstraints(); | |
123 | + | |
124 | + getContentPane().setLayout(layout); | |
125 | + getContentPane().add(tabbedCanvasHolder); | |
126 | + } | |
127 | + | |
128 | + /** | |
129 | + * Instantiates the tabbedCanvasHolder with one tab. | |
130 | + * Tabs are positioned at the Top. Calls initFirstCanvas(). Then adds | |
131 | + * the canvas to the tabbedCanvasHolder | |
132 | + **/ | |
133 | + public void initTabbedCanvasHolder() | |
134 | + { | |
135 | + initFirstCanvas(); | |
136 | + | |
137 | + // instantiate the canvasHolder and add the initial canvas to it | |
138 | + tabbedCanvasHolder = new TabbedPanel(JTabbedPane.TOP); | |
139 | + tabbedCanvasHolder.add((DrawingCanvas)canvas.get(0)); | |
140 | + } | |
141 | + | |
142 | + /** | |
143 | + * Instantiates the DrawingCanvas. | |
144 | + * Sets its name to "untitled"+ fileCount, a static int which is also | |
145 | + * incremented by one in this method. | |
146 | + **/ | |
147 | + public void initFirstCanvas() | |
148 | + { | |
149 | + canvas = new ArrayList(); | |
150 | + DrawingCanvas tempCanvas = new DrawingCanvas(); | |
151 | + tempCanvas.setName("untitled-"+fileCount); | |
152 | + tempCanvas.setBackground(Color.WHITE); | |
153 | + canvas.add(tempCanvas); | |
154 | + fileCount++; | |
155 | + numFiles++; | |
156 | + } | |
157 | + | |
158 | + /** Instantiates the colorBox **/ | |
159 | + public void initPallette() | |
160 | + { | |
161 | + pallette = new ColorBox(); | |
162 | + } | |
163 | + | |
164 | + /** Instantiates the colorPanel **/ | |
165 | + public void initColorPanel() | |
166 | + { | |
167 | + colorPanel = new ColorPanel(); | |
168 | + } | |
169 | + | |
170 | + /** Instantiates the MainMenu, which is JPaints menu bar. **/ | |
171 | + public void initMenuBar() | |
172 | + { | |
173 | + mainMenu = new MainMenu(); | |
174 | + mainMenu.addActionListener(this); | |
175 | + setJMenuBar(mainMenu); | |
176 | + } | |
177 | + | |
178 | + /** | |
179 | + * Initializes the frame by setting its title, size, location, listeners, | |
180 | + * and other default attributes. | |
181 | + **/ | |
182 | + public void initFrame() | |
183 | + { | |
184 | + setTitle(".: JPaint :."); | |
185 | + getContentPane().setLayout(layoutManager); | |
186 | + setDefaultCloseOperation(EXIT_ON_CLOSE); | |
187 | + setSize(300, 600); | |
188 | + setLocation(0,0); | |
189 | + addComponentListener(this); | |
190 | + setVisible(true); | |
191 | + setXY(); | |
192 | + } | |
193 | + | |
194 | + /** | |
195 | + * This code checks sets the X and Y coords of where the drawing canvas | |
196 | + * begins. | |
197 | + **/ | |
198 | + public void setXY() | |
199 | + { | |
200 | + xCoord = tBar.getWidth() + 6; | |
201 | + yCoord = extraY; | |
202 | + } | |
203 | + | |
204 | + /** Calls all instantiating methods thereby instantiating all objects. **/ | |
205 | + public void initAll() | |
206 | + { | |
207 | + initTabbedCanvasHolder(); | |
208 | + initToolBar(); | |
209 | + initSubToolBox(); | |
210 | + initColorPanel(); | |
211 | + initPallette(); | |
212 | + initLayout(); | |
213 | + initFileChooser(); | |
214 | + initMenuBar(); | |
215 | + initFrame(); | |
216 | + } | |
217 | + | |
218 | + /** | |
219 | + * Reveals the fileOpen dialog. | |
220 | + * Retrieves the pathname of the selected file and calls openOperation(path) | |
221 | + * to display the image to th current canvas. | |
222 | + **/ | |
223 | + public void showFileOpen() | |
224 | + { | |
225 | + int returnVal = fileChooser.showOpenDialog(this); | |
226 | + if(returnVal == JFileChooser.APPROVE_OPTION) | |
227 | + { | |
228 | + String path = fileChooser.getSelectedFile().getPath(); | |
229 | + ((DrawingCanvas)canvas.get(currentFile)).openOperation(path); | |
230 | + } | |
231 | + } | |
232 | + | |
233 | + /** Allows user to print the image on the screen through any available printer. **/ | |
234 | + public void printImage() | |
235 | + { | |
236 | + ((DrawingCanvas)canvas.get(currentFile)).saveImageForPrinting(); | |
237 | + PrinterJob printJob = PrinterJob.getPrinterJob(); | |
238 | + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); | |
239 | + printJob.setPrintable(((DrawingCanvas)canvas.get(currentFile))); | |
240 | + if(printJob.printDialog()) | |
241 | + try{printJob.print();}catch(PrinterException pe){ pe.printStackTrace();} | |
242 | + } | |
243 | + | |
244 | + /** | |
245 | + * Saves the image on the current canvas to a user specified path with | |
246 | + * the .png extension if the image has not previously been saved. | |
247 | + * Otherwise, updates the current file. | |
248 | + **/ | |
249 | + public void saveImage() | |
250 | + { | |
251 | + try | |
252 | + { | |
253 | + DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile)); | |
254 | + BufferedImage screenCapture = new Robot().createScreenCapture( | |
255 | + new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight())); | |
256 | + | |
257 | + String title = tabbedCanvasHolder.getTitleAt(currentFile); | |
258 | + if(title.indexOf("untitled-") != -1) | |
259 | + saveImageAs(); | |
260 | + else | |
261 | + try{ | |
262 | + ImageIO.write(screenCapture, "png", file); | |
263 | + }catch(IOException ioe){} | |
264 | + }catch(AWTException awte){} | |
265 | + } | |
266 | + | |
267 | + /** | |
268 | + * Saves the image on the current canvas to a user specified path with | |
269 | + * the .png extension | |
270 | + **/ | |
271 | + public void saveImageAs() | |
272 | + { | |
273 | + try{ | |
274 | + DrawingCanvas dc = ((DrawingCanvas)canvas.get(currentFile)); | |
275 | + BufferedImage screenCapture = new Robot().createScreenCapture( | |
276 | + new Rectangle(xCoord,yCoord,dc.getWidth(),dc.getHeight())); | |
277 | + | |
278 | + int returnVal = fileChooser.showSaveDialog(this); | |
279 | + if(returnVal == JFileChooser.APPROVE_OPTION) | |
280 | + { | |
281 | + String path = fileChooser.getSelectedFile().getPath(); | |
282 | + if(path.indexOf(".png") == -1) | |
283 | + path+=".png"; | |
284 | + try | |
285 | + { | |
286 | + file = new File(path); | |
287 | + tabbedCanvasHolder.setTitleAt(currentFile,fileChooser.getName(file)); | |
288 | + ImageIO.write(screenCapture, "png", file); | |
289 | + }catch(IOException ioe){} | |
290 | + } | |
291 | + }catch(AWTException awte){} | |
292 | + } | |
293 | + | |
294 | + /** Clears the current canvas of any images **/ | |
295 | + public void clearImage() | |
296 | + { | |
297 | + ((DrawingCanvas)canvas.get(currentFile)).clearImage(); | |
298 | + } | |
299 | + | |
300 | + /** Allows the user to set the visible state of the tool box. **/ | |
301 | + public void viewToolBoxEvent() | |
302 | + { | |
303 | + tBar.setVisible(!tBar.isVisible()); | |
304 | + } | |
305 | + | |
306 | + /** Allows the user to set the visible state **/ | |
307 | + public void viewColorBoxEvent() | |
308 | + { | |
309 | + pallette.setVisible(!pallette.isVisible()); | |
310 | + } | |
311 | + | |
312 | + /** Displays the help dialog. **/ | |
313 | + public void helpAboutEvent() | |
314 | + { | |
315 | + HelpDialog help = new HelpDialog(); | |
316 | + } | |
317 | + | |
318 | + /** Exits JPaint. **/ | |
319 | + public void fileExitEvent() | |
320 | + { | |
321 | + System.exit(0); | |
322 | + } | |
323 | + | |
324 | + /** | |
325 | + * Creates a new DrawingCanvas and names it "untitled" + fileCount, a | |
326 | + * static variable which is incremented in this method. | |
327 | + **/ | |
328 | + public void fileNewEvent() | |
329 | + { | |
330 | + DrawingCanvas nextCanvas = new DrawingCanvas(); | |
331 | + nextCanvas.setBackground(Color.BLACK); | |
332 | + nextCanvas.setName("untitled-"+fileCount); | |
333 | + tabbedCanvasHolder.add(nextCanvas); | |
334 | + canvas.add(nextCanvas); | |
335 | + nextCanvas.createBufferStrategy(2); | |
336 | + fileCount++; | |
337 | + numFiles++; | |
338 | + } | |
339 | + | |
340 | + /** Handles all MenuBar events. **/ | |
341 | + public void actionPerformed(ActionEvent e) | |
342 | + { | |
343 | + JMenuItem hit = (JMenuItem)e.getSource(); | |
344 | + if(hit.equals(mainMenu.getJMenuItem("fileNew"))) | |
345 | + fileNewEvent(); | |
346 | + else if(hit.equals(mainMenu.getJMenuItem("fileExit"))) | |
347 | + fileExitEvent(); | |
348 | + else if(hit.equals(mainMenu.getJMenuItem("helpAbout"))) | |
349 | + helpAboutEvent(); | |
350 | + else if(hit.equals(mainMenu.getJMenuItem("viewColorBox"))) | |
351 | + viewColorBoxEvent(); | |
352 | + else if(hit.equals(mainMenu.getJMenuItem("viewToolBox"))) | |
353 | + viewToolBoxEvent(); | |
354 | + else if(hit.equals(mainMenu.getJMenuItem("fileOpen"))) | |
355 | + showFileOpen(); | |
356 | + else if(hit.equals(mainMenu.getJMenuItem("fileSave"))) | |
357 | + saveImage(); | |
358 | + else if(hit.equals(mainMenu.getJMenuItem("fileSaveAs"))) | |
359 | + saveImageAs(); | |
360 | + else if(hit.equals(mainMenu.getJMenuItem("fileClose"))) | |
361 | + closeProject(); | |
362 | + else if(hit.equals(mainMenu.getJMenuItem("filePrint"))) | |
363 | + printImage(); | |
364 | + else if(hit.equals(mainMenu.getJMenuItem("imageClear"))) | |
365 | + clearImage(); | |
366 | + else if(hit.equals(mainMenu.getJMenuItem("imageRotate"))) | |
367 | + ((DrawingCanvas)canvas.get(currentFile)).rotateOperation(); | |
368 | + else if(hit.equals(mainMenu.getJMenuItem("editCut"))) | |
369 | + ((DrawingCanvas)canvas.get(currentFile)).cutOperation(); | |
370 | + else if(hit.equals(mainMenu.getJMenuItem("editPaste"))) | |
371 | + ((DrawingCanvas)canvas.get(currentFile)).pasteOperation(); | |
372 | + else if(hit.equals(mainMenu.getJMenuItem("editCopy"))) | |
373 | + ((DrawingCanvas)canvas.get(currentFile)).copyOperation(); | |
374 | + } | |
375 | + | |
376 | + /** Closes an open project. **/ | |
377 | + public void closeProject() | |
378 | + { | |
379 | + if(numFiles>1) | |
380 | + { | |
381 | + tabbedCanvasHolder.remove(currentFile); | |
382 | + tabbedCanvasHolder.setSelectedIndex(0); | |
383 | + numFiles--; | |
384 | + } | |
385 | + } | |
386 | + | |
387 | + /** Handles a componentMoved event **/ | |
388 | + public void componentMoved(ComponentEvent e) | |
389 | + { | |
390 | + Point p = getLocation(); | |
391 | + xCoord = (int)p.getX() + tBar.getWidth() + 6; | |
392 | + yCoord = (int)p.getY() + extraY; | |
393 | + } | |
394 | + | |
395 | + /** Handles a componentHidden event **/ | |
396 | + public void componentHidden(ComponentEvent e){} | |
397 | + /** Handles a componentResized event **/ | |
398 | + public void componentResized(ComponentEvent e){} | |
399 | + /** Handles a componentShown event **/ | |
400 | + public void componentShown(ComponentEvent e){} | |
401 | +} | |
0 | 402 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,994 @@ |
1 | +import java.awt.Color; | |
2 | +import java.awt.Canvas; | |
3 | +import java.awt.Graphics; | |
4 | +import java.awt.event.MouseListener; | |
5 | +import java.awt.event.MouseMotionListener; | |
6 | +import java.awt.event.MouseEvent; | |
7 | +import java.io.File; | |
8 | +import java.awt.Image; | |
9 | +import javax.swing.ImageIcon; | |
10 | +import java.awt.Cursor; | |
11 | +import java.awt.Robot; | |
12 | +import java.awt.AWTException; | |
13 | +import java.awt.image.BufferedImage; | |
14 | +import java.awt.print.Printable; | |
15 | +import java.awt.print.PageFormat; | |
16 | +import java.awt.print.PrinterException; | |
17 | +import java.awt.Graphics2D; | |
18 | +import java.awt.Rectangle; | |
19 | +import java.awt.AWTException; | |
20 | +import java.awt.Font; | |
21 | +import java.awt.image.PixelGrabber; | |
22 | +import java.awt.image.MemoryImageSource; | |
23 | +import java.awt.geom.CubicCurve2D; | |
24 | +import java.awt.geom.CubicCurve2D.Float; | |
25 | +import java.awt.geom.Point2D; | |
26 | +import java.awt.event.FocusListener; | |
27 | +import java.awt.event.FocusEvent; | |
28 | +import java.awt.geom.AffineTransform; | |
29 | + | |
30 | +public class DrawingCanvas extends Canvas implements MouseListener, | |
31 | +MouseMotionListener, Printable, FocusListener | |
32 | +{ | |
33 | + public static final int FILL = 1, NORMAL = 2, OUTLINE = 3, RECT = 4, OVAL = 5, LINE = 6; | |
34 | + public static BufferedImage selectImage; | |
35 | + public static int extraOperations, lineWidth, radius, amtLines; | |
36 | + private Color drawColor, oppositeColor; | |
37 | + private BufferedImage printImage, resizeImage; | |
38 | + private CubicCurve2D c; | |
39 | + private Point2D p1, p2, p3, p4; | |
40 | + private boolean polyStart, arcStart, curveMade, curveMade2, save, dragSave; | |
41 | + private int mouseX, mouseY, prevX, prevY, originX, originY, width, height; | |
42 | + private int[] pixels; | |
43 | + | |
44 | + /** Creates a default DrawingCanvas. **/ | |
45 | + public DrawingCanvas() | |
46 | + { | |
47 | + // save is set to true unless the focus is lost | |
48 | + save = true; | |
49 | + // sets the default linewidth and the spray radius | |
50 | + lineWidth = 1; | |
51 | + radius = 5; | |
52 | + amtLines = 10; | |
53 | + // adds listeners and sets settings | |
54 | + setSize(400,400); | |
55 | + setBackground(Color.WHITE); | |
56 | + this.addMouseListener(this); | |
57 | + this.addMouseMotionListener(this); | |
58 | + this.addFocusListener(this); | |
59 | + } | |
60 | + | |
61 | + /** | |
62 | + * This methods adjusts the screen size of the user by taking a snapshot of | |
63 | + * the canvas and seeing if it shifts up or down and sets the pixels | |
64 | + * accordingly. | |
65 | + **/ | |
66 | + public void checkScreenSize() | |
67 | + { | |
68 | + saveFromResize(); | |
69 | + pixels = new int[getWidth()*getHeight()]; | |
70 | + PixelGrabber pg = new PixelGrabber(resizeImage, 0, 0, getWidth(), getHeight(), pixels, 0, getWidth()); | |
71 | + | |
72 | + try { | |
73 | + pg.grabPixels(); | |
74 | + } catch (InterruptedException ie) {} | |
75 | + | |
76 | + | |
77 | + if(pixels[0] != (Color.WHITE).getRGB()) | |
78 | + ControlClass.extraY = 78; | |
79 | + else | |
80 | + ControlClass.extraY = 71; | |
81 | + } | |
82 | + | |
83 | + /** Saves an image of the current screen so if the canvas resizes the image doesnt get deleted. **/ | |
84 | + public void saveFromResize() | |
85 | + { | |
86 | + try{ | |
87 | + resizeImage = new Robot().createScreenCapture( | |
88 | + new Rectangle(ControlClass.xCoord,ControlClass.yCoord,getWidth(),getHeight())); | |
89 | + }catch(AWTException awte){} | |
90 | + } | |
91 | + | |
92 | + /** Draws the image back to the screen after the canvas is resized **/ | |
93 | + public void pasteFromResize() | |
94 | + { | |
95 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
96 | + g.drawImage(resizeImage,0,0,null); | |
97 | + repaint(); | |
98 | + } | |
99 | + | |
100 | + /** Fills anything of the same color the first pixel selected. **/ | |
101 | + public void fillOperation(MouseEvent e) | |
102 | + { | |
103 | + // saves the image and then creates a pixel array | |
104 | + saveImageForPrinting(); | |
105 | + pixels = new int[getWidth()*getHeight()]; | |
106 | + | |
107 | + // grabs all the pixels from the image | |
108 | + PixelGrabber pg = new PixelGrabber(printImage, 0, 0, getWidth(), getHeight(), pixels, 0, getWidth()); | |
109 | + try { | |
110 | + pg.grabPixels(); | |
111 | + } catch (InterruptedException ie) {} | |
112 | + | |
113 | + // calls the start fill method, rgb is the color clicked on | |
114 | + int rgb = pixels[e.getY()*getWidth()+e.getX()]; | |
115 | + startFill(e.getX(),e.getY(),rgb); | |
116 | + | |
117 | + // creates an image from the pixel array and draws it back to the screen | |
118 | + Image img = createImage(new MemoryImageSource(getWidth(), getHeight(), pixels, 0, getWidth())); | |
119 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
120 | + g.drawImage(img,0,0,null); | |
121 | + repaint(); | |
122 | + saveFromResize(); | |
123 | + } | |
124 | + | |
125 | + /** Changes the color of the pixel at the x and y coordinate. **/ | |
126 | + private void changePixel(int x, int y, int rgb) | |
127 | + { | |
128 | + pixels[y*getWidth()+x] = rgb; | |
129 | + } | |
130 | + | |
131 | + /** Gets the pixel at the x and y point **/ | |
132 | + private int getPixel(int x, int y) | |
133 | + { | |
134 | + return pixels[y*getWidth()+x]; | |
135 | + } | |
136 | + | |
137 | + /** Helper method for fillOperation. This method does the actually filling. **/ | |
138 | + public void startFill(int x, int y, int original) | |
139 | + { | |
140 | + int i = y*getWidth()+x; | |
141 | + if(pixels[i] != original) | |
142 | + return; | |
143 | + | |
144 | + // sets the pixel quete to the whole canvas | |
145 | + int pixelQueue[] = new int[getWidth() * getHeight()]; | |
146 | + int pixelQueueSize = 0; | |
147 | + | |
148 | + // sets the first pixel | |
149 | + pixelQueue[0] = (y << 16) + x; | |
150 | + pixelQueueSize = 1; | |
151 | + | |
152 | + changePixel(x,y,original); | |
153 | + | |
154 | + // loop to check and does a flood fill to the original color | |
155 | + while (pixelQueueSize > 0) | |
156 | + { | |
157 | + x = pixelQueue[0] & 0xffff; | |
158 | + y = (pixelQueue[0] >> 16) & 0xffff; | |
159 | + pixelQueueSize--; | |
160 | + pixelQueue[0] = pixelQueue[pixelQueueSize]; | |
161 | + | |
162 | + if (x > 0) | |
163 | + { | |
164 | + if (getPixel(x-1, y) == original) | |
165 | + { | |
166 | + changePixel(x-1, y, drawColor.getRGB()); | |
167 | + pixelQueue[pixelQueueSize] = (y << 16) + x-1; | |
168 | + pixelQueueSize++; | |
169 | + } | |
170 | + } | |
171 | + | |
172 | + if (y > 0) | |
173 | + { | |
174 | + if (getPixel(x, y-1) == original) | |
175 | + { | |
176 | + changePixel(x, y-1, drawColor.getRGB()); | |
177 | + pixelQueue[pixelQueueSize] = ((y-1) << 16) + x; | |
178 | + pixelQueueSize++; | |
179 | + } | |
180 | + } | |
181 | + if (x < getWidth()-1) | |
182 | + { | |
183 | + if (getPixel(x+1, y) == original) | |
184 | + { | |
185 | + changePixel(x+1, y, drawColor.getRGB()); | |
186 | + pixelQueue[pixelQueueSize] = (y << 16) + x+1; | |
187 | + pixelQueueSize++; | |
188 | + } | |
189 | + } | |
190 | + if (y < getHeight()-1) | |
191 | + { | |
192 | + if (getPixel(x, y+1) == original) | |
193 | + { | |
194 | + changePixel(x, y+1, drawColor.getRGB()); | |
195 | + pixelQueue[pixelQueueSize] = ((y+1) << 16) + x; | |
196 | + pixelQueueSize++; | |
197 | + } | |
198 | + } | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + /** Helper method for the rotation. Sets the amount of rotation 90 degrees. **/ | |
203 | + private AffineTransform setRotation() | |
204 | + { | |
205 | + AffineTransform atright = AffineTransform.getRotateInstance(Math.PI / 2, getWidth() / 2, getHeight() / 2); | |
206 | + return atright; | |
207 | + } | |
208 | + | |
209 | + /** This method actually rotates the image 90 degrees. **/ | |
210 | + public void rotateOperation() | |
211 | + { | |
212 | + // saves the image and then clears it and draws it rotated 90 degrees. | |
213 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
214 | + Graphics2D g2 = (Graphics2D) g; | |
215 | + saveFromResize(); | |
216 | + clearImage(); | |
217 | + g2.drawImage(resizeImage, setRotation(), this); | |
218 | + repaint(); | |
219 | + } | |
220 | + | |
221 | + /** Saves the image so that it can be printed. **/ | |
222 | + public void saveImageForPrinting() | |
223 | + { | |
224 | + // saves the image of the screen | |
225 | + try{ | |
226 | + printImage = new Robot().createScreenCapture( | |
227 | + new Rectangle(ControlClass.xCoord,ControlClass.yCoord,getWidth(),getHeight())); | |
228 | + }catch(AWTException awte){} | |
229 | + } | |
230 | + | |
231 | + /** This is a helper method for print. Draws the image to be printed. **/ | |
232 | + public void drawImage(Graphics2D g2D) | |
233 | + { | |
234 | + // draws the print image out on paper | |
235 | + g2D.drawImage(printImage,0,0,null); | |
236 | + } | |
237 | + | |
238 | + /** Printing operation, it prints the image saved. **/ | |
239 | + public int print(Graphics g, PageFormat pf, int pIndex) throws PrinterException | |
240 | + { | |
241 | + if (pIndex > 0) | |
242 | + return Printable.NO_SUCH_PAGE; | |
243 | + | |
244 | + Graphics2D g2D = (Graphics2D)g; | |
245 | + drawImage(g2D); | |
246 | + return Printable.PAGE_EXISTS; | |
247 | + } | |
248 | + | |
249 | + /** Updates the screen and flips the buffers around. **/ | |
250 | + public void update(Graphics g) | |
251 | + { | |
252 | + getBufferStrategy().show(); | |
253 | + } | |
254 | + | |
255 | + /** Paints the image onto the screen. **/ | |
256 | + public void paint(Graphics g) | |
257 | + { | |
258 | + try{update(g);}catch(NullPointerException e){} | |
259 | + } | |
260 | + | |
261 | + /** Opens a selected image and draws it onto the screen. **/ | |
262 | + public void openOperation(String path) | |
263 | + { | |
264 | + // creates an image and draws it to the screen | |
265 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
266 | + ImageIcon ic = new ImageIcon(path); | |
267 | + g.drawImage((Image)ic.getImage(),0,0,null); | |
268 | + } | |
269 | + | |
270 | + /** Selects the image from the top left corner to the bottom right corner. **/ | |
271 | + public void selectOperation(MouseEvent e) | |
272 | + { | |
273 | + if(mouseHasMoved(e)) | |
274 | + { | |
275 | + // this checks if you select off screen, if your off screen then it will | |
276 | + // set the boundaries to the edge of the drawing canvas | |
277 | + int tOriginX = ControlClass.xCoord+e.getX(); | |
278 | + int tOriginY = ControlClass.yCoord+e.getY(); | |
279 | + | |
280 | + if(tOriginX > getWidth()+ControlClass.xCoord) | |
281 | + tOriginX = getWidth()+ControlClass.xCoord; | |
282 | + if(tOriginX < ControlClass.xCoord) | |
283 | + tOriginX = ControlClass.xCoord; | |
284 | + if(tOriginY > getHeight()+ControlClass.yCoord) | |
285 | + tOriginY = getHeight()+ControlClass.yCoord; | |
286 | + if(tOriginY < ControlClass.yCoord) | |
287 | + tOriginY = ControlClass.yCoord; | |
288 | + | |
289 | + // calculates the width and height of the area | |
290 | + width = tOriginX-(ControlClass.xCoord+originX); | |
291 | + height = tOriginY-(ControlClass.yCoord+originY); | |
292 | + | |
293 | + if(width < 0 && height < 0) | |
294 | + { | |
295 | + originX+=width; | |
296 | + originY+=height; | |
297 | + } | |
298 | + else if(width < 0 && height > 0) | |
299 | + originX+=width; | |
300 | + else if(width > 0 && height < 0) | |
301 | + originY+=height; | |
302 | + | |
303 | + width = Math.abs(width); | |
304 | + height = Math.abs(height); | |
305 | + } | |
306 | + } | |
307 | + | |
308 | + /** Copies the selected image **/ | |
309 | + public void copyOperation() | |
310 | + { | |
311 | + // copies the boundaries set by the mouse | |
312 | + try{ | |
313 | + DrawingCanvas.selectImage = new Robot().createScreenCapture( | |
314 | + new Rectangle(ControlClass.xCoord+originX,ControlClass.yCoord+originY, | |
315 | + width,height)); | |
316 | + }catch(AWTException awte){} | |
317 | + } | |
318 | + /** Copies the selected image and deletes the selected portion **/ | |
319 | + public void cutOperation() | |
320 | + { | |
321 | + // copies the image and then draws a rectangle over the selected area | |
322 | + copyOperation(); | |
323 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
324 | + g.setColor(oppositeColor); | |
325 | + g.fillRect(originX,originY,width,height); | |
326 | + width = height = 0; | |
327 | + repaint(); | |
328 | + } | |
329 | + | |
330 | + /** Draws the image with regards to the mouse location. **/ | |
331 | + public void pasteOperation() | |
332 | + { | |
333 | + // draws the image of the selected image onto the screen | |
334 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
335 | + g.drawImage(DrawingCanvas.selectImage,originX,originY,null); | |
336 | + repaint(); | |
337 | + } | |
338 | + | |
339 | + /** Draws random lines of random colors on the screen. **/ | |
340 | + public void randomDraw() | |
341 | + { | |
342 | + RandomPoint rPt = new RandomPoint(getWidth(),getHeight()); | |
343 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
344 | + int i=0; | |
345 | + while(i< amtLines) | |
346 | + { | |
347 | + g.setColor(rPt.getColor()); | |
348 | + g.drawLine((int)rPt.getX(),(int)rPt.getY(),rPt.nextX(),rPt.nextY()); | |
349 | + i++; | |
350 | + } | |
351 | + repaint(); | |
352 | + } | |
353 | + | |
354 | + /** Opens up a text dialog in the selected area. **/ | |
355 | + public void textOperation(MouseEvent e) | |
356 | + { | |
357 | + // opens up a text dialog | |
358 | + setCursor(new Cursor(Cursor.TEXT_CURSOR)); | |
359 | + TextDialog tDialog = new TextDialog(e.getX(),e.getY()); | |
360 | + } | |
361 | + | |
362 | + /** Prints the text onto the screen **/ | |
363 | + public void textOperation2(String txt, int x, int y, Font f) | |
364 | + { | |
365 | + // draws the text onto the screen | |
366 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
367 | + g.setFont(f); | |
368 | + g.setColor(drawColor); | |
369 | + g.drawString(txt,x,y); | |
370 | + g.setFont(new Font("Times New Roman", Font.PLAIN, 12)); | |
371 | + repaint(); | |
372 | + } | |
373 | + | |
374 | + /** An eraser that erases when the mouse is dragged. **/ | |
375 | + public void eraserOperation(MouseEvent e) | |
376 | + { | |
377 | + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); | |
378 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
379 | + g.setColor(ColorBox.backGround.getBackground()); | |
380 | + | |
381 | + if (mouseHasMoved(e)) | |
382 | + g.fillRect(e.getX(),e.getY(),15,15); | |
383 | + | |
384 | + repaint(); | |
385 | + } | |
386 | + | |
387 | + /** Paint brush with different brushes like a square and a circle. **/ | |
388 | + public void brushOperation(MouseEvent e) | |
389 | + { | |
390 | + setCursor(new Cursor(Cursor.HAND_CURSOR)); | |
391 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
392 | + g.setColor(drawColor); | |
393 | + | |
394 | + // depending on which brush is selected it will change the brush to that shape | |
395 | + if (mouseHasMoved(e)) | |
396 | + if(extraOperations == OVAL) | |
397 | + g.fillOval(e.getX(),e.getY(),radius,radius); | |
398 | + else if(extraOperations == RECT) | |
399 | + g.fillRect(e.getX(),e.getY(),radius,radius); | |
400 | + repaint(); | |
401 | + } | |
402 | + | |
403 | + /** Creates a pen when can be operated when mouse is dragged. **/ | |
404 | + public void penOperation(MouseEvent e) | |
405 | + { | |
406 | + setCursor(new Cursor(Cursor.HAND_CURSOR)); | |
407 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
408 | + g.setColor(drawColor); | |
409 | + | |
410 | + // draws lines while your mouse is dragged | |
411 | + if (mouseHasMoved(e)) | |
412 | + { | |
413 | + mouseX = e.getX(); | |
414 | + mouseY = e.getY(); | |
415 | + | |
416 | + g.drawLine(prevX,prevY,mouseX,mouseY); | |
417 | + | |
418 | + prevX = mouseX; | |
419 | + prevY = mouseY; | |
420 | + } | |
421 | + repaint(); | |
422 | + } | |
423 | + | |
424 | + /** Calls the line operation first and then starts the polygon operation **/ | |
425 | + public void prePolyOperation(MouseEvent e) | |
426 | + { | |
427 | + if(!polyStart) | |
428 | + { | |
429 | + lineOperation(e); | |
430 | + polyStart = true; | |
431 | + } | |
432 | + polyOperation(e); | |
433 | + } | |
434 | + | |
435 | + /** Starts a polygon shape by drawing many line. **/ | |
436 | + public void polyOperation(MouseEvent e) | |
437 | + { | |
438 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
439 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
440 | + g.setColor(drawColor); | |
441 | + | |
442 | + // draws a line on the screen | |
443 | + if(mouseHasMoved(e)) | |
444 | + { | |
445 | + mouseX = e.getX(); | |
446 | + mouseY = e.getY(); | |
447 | + | |
448 | + g.drawLine(prevX, prevY, mouseX, mouseY); | |
449 | + | |
450 | + prevX = mouseX; | |
451 | + prevY = mouseY; | |
452 | + } | |
453 | + | |
454 | + // sets the polygon operation to false if the line is back at the origin | |
455 | + if(isBeginning(e)) | |
456 | + polyStart = false; | |
457 | + | |
458 | + repaint(); | |
459 | + } | |
460 | + | |
461 | + /** Helper method for the polygon to check if the line drawn is back at the origin. **/ | |
462 | + public boolean isBeginning(MouseEvent e) | |
463 | + { | |
464 | + // returns true or false if the range is five away from the beginning | |
465 | + return (Math.abs(originX-e.getX()) <= 5 && Math.abs(originY-e.getY()) <= 5); | |
466 | + } | |
467 | + | |
468 | + /** Checks if the polygon operation is deselected and stops it. **/ | |
469 | + public void checkPolyOff() | |
470 | + { | |
471 | + // if the polygon operation is on and the polygon tool is not selected then | |
472 | + // finish the line if it wasnt completed and reset the polygon operation | |
473 | + if(polyStart && ToolBox.toolSelected != ToolBox.POLYGON) | |
474 | + { | |
475 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
476 | + g.setColor(drawColor); | |
477 | + g.drawLine(prevX,prevY,originX,originY); | |
478 | + polyStart = false; | |
479 | + } | |
480 | + } | |
481 | + | |
482 | + /** Checks if the curve line tool is deselected. **/ | |
483 | + public void checkCurveOff() | |
484 | + { | |
485 | + // if the tool is deselected then reset variables to default | |
486 | + if(ToolBox.toolSelected != ToolBox.CURVELINE) | |
487 | + curveMade = curveMade2 = false; | |
488 | + } | |
489 | + | |
490 | + /** Starts the curve line tool. **/ | |
491 | + public void curveLineOperation(MouseEvent e) | |
492 | + { | |
493 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
494 | + g.setColor(drawColor); | |
495 | + | |
496 | + if(mouseHasMoved(e)) | |
497 | + { | |
498 | + mouseX = e.getX(); | |
499 | + mouseY = e.getY(); | |
500 | + | |
501 | + Graphics2D g2 = (Graphics2D)g; | |
502 | + | |
503 | + // if the first and second point are not choosen then draw a straight line | |
504 | + if(!curveMade && !curveMade2) | |
505 | + { | |
506 | + c = new CubicCurve2D.Float(); | |
507 | + p1 = new Point2D.Float(); | |
508 | + p2 = new Point2D.Float(); | |
509 | + p3 = new Point2D.Float(); | |
510 | + p4 = new Point2D.Float(); | |
511 | + p1.setLocation(originX, originY); | |
512 | + p4.setLocation(e.getX(), e.getY()); | |
513 | + curveMade = true; | |
514 | + g.drawLine(originX,originY,e.getX(),e.getY()); | |
515 | + repaint(); | |
516 | + } | |
517 | + // once one of the curves are made then you select a point | |
518 | + else if(curveMade && !curveMade2) | |
519 | + { | |
520 | + pasteFromResize(); | |
521 | + p2.setLocation(e.getX(), e.getY()); | |
522 | + p3.setLocation(e.getX(), e.getY()); | |
523 | + curveMade2 = true; | |
524 | + c.setCurve(p1, p2, p3, p4); | |
525 | + g2.draw(c); | |
526 | + repaint(); | |
527 | + } | |
528 | + // once both points are choosen then the curve is drawn onto the screen | |
529 | + else if(curveMade2 && curveMade) | |
530 | + { | |
531 | + pasteFromResize(); | |
532 | + p3.setLocation(e.getX(), e.getY()); | |
533 | + c.setCurve(p1, p2, p3, p4); | |
534 | + g2.draw(c); | |
535 | + curveMade = false; | |
536 | + curveMade2 = false; | |
537 | + } | |
538 | + } | |
539 | + repaint(); | |
540 | + } | |
541 | + | |
542 | + /** Changes the foreground color to the selected color. **/ | |
543 | + public void eyeDropperOperation(MouseEvent e) | |
544 | + { | |
545 | + // creates a screen shot and takes the pixel color where the mouse is situated | |
546 | + try{ | |
547 | + Robot robot = new Robot(); | |
548 | + ColorBox.foreGround.setBackground(robot.getPixelColor(ControlClass.xCoord+e.getX(),ControlClass.yCoord+e.getY())); | |
549 | + }catch(AWTException awt){} | |
550 | + } | |
551 | + | |
552 | + /** Creates a line from two points. **/ | |
553 | + public void lineOperation(MouseEvent e) | |
554 | + { | |
555 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
556 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
557 | + g.setColor(drawColor); | |
558 | + | |
559 | + if (mouseHasMoved(e)) | |
560 | + { | |
561 | + mouseX = e.getX(); | |
562 | + mouseY = e.getY(); | |
563 | + | |
564 | + g.drawLine(originX, originY, mouseX, mouseY); | |
565 | + | |
566 | + prevX = mouseX; | |
567 | + prevY = mouseY; | |
568 | + } | |
569 | + pasteFromResize(); | |
570 | + g.drawLine(originX, originY, mouseX, mouseY); | |
571 | + repaint(); | |
572 | + } | |
573 | + | |
574 | + /** | |
575 | + * Draw a rectangle from two given points. Draws a fill operation, a outlined | |
576 | + * rectangle or a outlined with fill operation. | |
577 | + **/ | |
578 | + public void drawRectangle(MouseEvent e) | |
579 | + { | |
580 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
581 | + g.setColor(drawColor); | |
582 | + | |
583 | + // gets the mouse coordinate and gets the width and height | |
584 | + mouseX = e.getX(); | |
585 | + mouseY = e.getY(); | |
586 | + int width = mouseX-originX; | |
587 | + int height = mouseY-originY; | |
588 | + | |
589 | + // draws a filled rectangle | |
590 | + if(extraOperations == FILL) | |
591 | + { | |
592 | + if(width > 0 && height > 0) | |
593 | + g.fillRect(originX,originY,width,height); | |
594 | + else if(width < 0 && height < 0) | |
595 | + g.fillRect(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
596 | + else if(width < 0 && height > 0) | |
597 | + g.fillRect(originX+width,originY,Math.abs(width),height); | |
598 | + else if(width > 0 && height < 0) | |
599 | + g.fillRect(originX,originY+height,width,Math.abs(height)); | |
600 | + } | |
601 | + // draws a normal rectangle | |
602 | + else if(extraOperations == NORMAL) | |
603 | + { | |
604 | + if(width > 0 && height > 0) | |
605 | + for(int i=0;i<lineWidth;i++) | |
606 | + g.drawRect(originX+i,originY+i,width-(i*2),height-(i*2)); | |
607 | + else if(width < 0 && height < 0) | |
608 | + for(int i=0;i<lineWidth;i++) | |
609 | + g.drawRect(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2)); | |
610 | + else if(width < 0 && height > 0) | |
611 | + for(int i=0;i<lineWidth;i++) | |
612 | + g.drawRect(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2)); | |
613 | + else if(width > 0 && height < 0) | |
614 | + for(int i=0;i<lineWidth;i++) | |
615 | + g.drawRect(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2)); | |
616 | + } | |
617 | + // draws a filled rectangle with an outline | |
618 | + else if(extraOperations == OUTLINE) | |
619 | + { | |
620 | + if(width > 0 && height > 0) | |
621 | + { | |
622 | + g.fillRect(originX,originY,width,height); | |
623 | + g.setColor(oppositeColor); | |
624 | + g.fillRect(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2)); | |
625 | + } | |
626 | + else if(width < 0 && height < 0) | |
627 | + { | |
628 | + g.fillRect(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
629 | + g.setColor(oppositeColor); | |
630 | + g.fillRect(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
631 | + } | |
632 | + else if(width < 0 && height > 0) | |
633 | + { | |
634 | + g.fillRect(originX+width,originY,Math.abs(width),height); | |
635 | + g.setColor(oppositeColor); | |
636 | + g.fillRect(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2)); | |
637 | + } | |
638 | + else if(width > 0 && height < 0) | |
639 | + { | |
640 | + g.fillRect(originX,originY+height,width,Math.abs(height)); | |
641 | + g.setColor(oppositeColor); | |
642 | + g.fillRect(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
643 | + } | |
644 | + } | |
645 | + } | |
646 | + | |
647 | + /** Calls the drawRectangle operation. **/ | |
648 | + public void rectOperation(MouseEvent e) | |
649 | + { | |
650 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
651 | + | |
652 | + if (mouseHasMoved(e)) | |
653 | + drawRectangle(e); | |
654 | + | |
655 | + pasteFromResize(); | |
656 | + drawRectangle(e); | |
657 | + repaint(); | |
658 | + } | |
659 | + | |
660 | + /** This method draws a rectangle with rounded edges. **/ | |
661 | + public void drawRoundRectangle(MouseEvent e) | |
662 | + { | |
663 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
664 | + g.setColor(drawColor); | |
665 | + | |
666 | + // gets the mouse coordinate and gets the width and height | |
667 | + mouseX = e.getX(); | |
668 | + mouseY = e.getY(); | |
669 | + int width = mouseX-originX; | |
670 | + int height = mouseY-originY; | |
671 | + | |
672 | + // draws a filled rounded rectangle | |
673 | + if(extraOperations == FILL) | |
674 | + { | |
675 | + if(width > 0 && height > 0) | |
676 | + g.fillRoundRect(originX,originY,width,height,25,25); | |
677 | + else if(width < 0 && height < 0) | |
678 | + g.fillRoundRect(originX+width,originY+height,Math.abs(width),Math.abs(height),25,25); | |
679 | + else if(width < 0 && height > 0) | |
680 | + g.fillRoundRect(originX+width,originY,Math.abs(width),height,25,25); | |
681 | + else if(width > 0 && height < 0) | |
682 | + g.fillRoundRect(originX,originY+height,width,Math.abs(height),25,25); | |
683 | + } | |
684 | + // draws a normal rounded rectangle | |
685 | + else if(extraOperations == NORMAL) | |
686 | + { | |
687 | + if(width > 0 && height > 0) | |
688 | + for(int i=0;i<lineWidth;i++) | |
689 | + g.drawRoundRect(originX+i,originY+i,width-(i*2),height-(i*2),25,25); | |
690 | + else if(width < 0 && height < 0) | |
691 | + for(int i=0;i<lineWidth;i++) | |
692 | + g.drawRoundRect(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2),25,25); | |
693 | + else if(width < 0 && height > 0) | |
694 | + for(int i=0;i<lineWidth;i++) | |
695 | + g.drawRoundRect(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2),25,25); | |
696 | + else if(width > 0 && height < 0) | |
697 | + for(int i=0;i<lineWidth;i++) | |
698 | + g.drawRoundRect(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2),25,25); | |
699 | + } | |
700 | + // draws a filled rounded rectangle with an outline | |
701 | + else if(extraOperations == OUTLINE) | |
702 | + { | |
703 | + if(width > 0 && height > 0) | |
704 | + { | |
705 | + g.fillRoundRect(originX,originY,width,height,25,25); | |
706 | + g.setColor(oppositeColor); | |
707 | + g.fillRoundRect(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2),25,25); | |
708 | + } | |
709 | + else if(width < 0 && height < 0) | |
710 | + { | |
711 | + g.fillRoundRect(originX+width,originY+height,Math.abs(width),Math.abs(height),25,25); | |
712 | + g.setColor(oppositeColor); | |
713 | + g.fillRoundRect(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2),25,25); | |
714 | + } | |
715 | + else if(width < 0 && height > 0) | |
716 | + { | |
717 | + g.fillRoundRect(originX+width,originY,Math.abs(width),height,25,25); | |
718 | + g.setColor(oppositeColor); | |
719 | + g.fillRoundRect(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2),25,25); | |
720 | + } | |
721 | + else if(width > 0 && height < 0) | |
722 | + { | |
723 | + g.fillRoundRect(originX,originY+height,width,Math.abs(height),25,25); | |
724 | + g.setColor(oppositeColor); | |
725 | + g.fillRoundRect(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2),25,25); | |
726 | + } | |
727 | + } | |
728 | + } | |
729 | + | |
730 | + /** Calls the drawRoundRectangle operation. **/ | |
731 | + public void roundOperation(MouseEvent e) | |
732 | + { | |
733 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
734 | + | |
735 | + if (mouseHasMoved(e)) | |
736 | + drawRoundRectangle(e); | |
737 | + | |
738 | + pasteFromResize(); | |
739 | + drawRoundRectangle(e); | |
740 | + repaint(); | |
741 | + } | |
742 | + | |
743 | + /** Draws an oval onto the screen. **/ | |
744 | + public void drawOval(MouseEvent e) | |
745 | + { | |
746 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
747 | + g.setColor(drawColor); | |
748 | + | |
749 | + // gets the mouse coordinate and gets the width and height | |
750 | + mouseX = e.getX(); | |
751 | + mouseY = e.getY(); | |
752 | + int width = mouseX-originX; | |
753 | + int height = mouseY-originY; | |
754 | + | |
755 | + // draws a filled oval | |
756 | + if(extraOperations == FILL) | |
757 | + { | |
758 | + if(width > 0 && height > 0) | |
759 | + g.fillOval(originX,originY,width,height); | |
760 | + else if(width < 0 && height < 0) | |
761 | + g.fillOval(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
762 | + else if(width < 0 && height > 0) | |
763 | + g.fillOval(originX+width,originY,Math.abs(width),height); | |
764 | + else if(width > 0 && height < 0) | |
765 | + g.fillOval(originX,originY+height,width,Math.abs(height)); | |
766 | + } | |
767 | + // draws a normal oval | |
768 | + else if(extraOperations == NORMAL) | |
769 | + { | |
770 | + if(width > 0 && height > 0) | |
771 | + for(int i=0;i<lineWidth;i++) | |
772 | + g.drawOval(originX+i,originY+i,width-(i*2),height-(i*2)); | |
773 | + else if(width < 0 && height < 0) | |
774 | + for(int i=0;i<lineWidth;i++) | |
775 | + g.drawOval(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2)); | |
776 | + else if(width < 0 && height > 0) | |
777 | + for(int i=0;i<lineWidth;i++) | |
778 | + g.drawOval(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2)); | |
779 | + else if(width > 0 && height < 0) | |
780 | + for(int i=0;i<lineWidth;i++) | |
781 | + g.drawOval(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2)); | |
782 | + } | |
783 | + // draws a filled oval with an outline | |
784 | + else if(extraOperations == OUTLINE) | |
785 | + { | |
786 | + if(width > 0 && height > 0) | |
787 | + { | |
788 | + g.fillOval(originX,originY,width,height); | |
789 | + g.setColor(oppositeColor); | |
790 | + g.fillOval(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2)); | |
791 | + } | |
792 | + else if(width < 0 && height < 0) | |
793 | + { | |
794 | + g.fillOval(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
795 | + g.setColor(oppositeColor); | |
796 | + g.fillOval(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
797 | + } | |
798 | + else if(width < 0 && height > 0) | |
799 | + { | |
800 | + g.fillOval(originX+width,originY,Math.abs(width),height); | |
801 | + g.setColor(oppositeColor); | |
802 | + g.fillOval(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2)); | |
803 | + } | |
804 | + else if(width > 0 && height < 0) | |
805 | + { | |
806 | + g.fillOval(originX,originY+height,width,Math.abs(height)); | |
807 | + g.setColor(oppositeColor); | |
808 | + g.fillOval(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
809 | + } | |
810 | + } | |
811 | + } | |
812 | + | |
813 | + /** Calls the drawOval operation. **/ | |
814 | + public void ovalOperation(MouseEvent e) | |
815 | + { | |
816 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
817 | + | |
818 | + if (mouseHasMoved(e)) | |
819 | + drawOval(e); | |
820 | + | |
821 | + pasteFromResize(); | |
822 | + drawOval(e); | |
823 | + repaint(); | |
824 | + } | |
825 | + | |
826 | + /** Drag Brush Operation. You draw a shape around as your brush. **/ | |
827 | + public void dragOperation(MouseEvent e) | |
828 | + { | |
829 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
830 | + g.setColor(drawColor); | |
831 | + | |
832 | + if (mouseHasMoved(e)) | |
833 | + if(extraOperations == LINE) | |
834 | + g.drawLine(e.getX(),e.getY(),e.getX()+radius,e.getY()-radius); | |
835 | + else if(extraOperations == RECT) | |
836 | + g.drawRect(e.getX(),e.getY(),radius,radius); | |
837 | + else if(extraOperations == OVAL) | |
838 | + g.drawOval(e.getX(),e.getY(),radius,radius); | |
839 | + repaint(); | |
840 | + } | |
841 | + | |
842 | + /** Spray brush tool. Draws many tiny dots within a set radius. **/ | |
843 | + public void sprayOperation(MouseEvent e) | |
844 | + { | |
845 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
846 | + g.setColor(drawColor); | |
847 | + // sets random points to the origin and draws them onto the screen | |
848 | + for(int i=0;i<radius;i++) | |
849 | + { | |
850 | + setRandomPts(e); | |
851 | + g.fillOval(originX,originY,1,1); | |
852 | + repaint(); | |
853 | + } | |
854 | + } | |
855 | + | |
856 | + /** Gets random points that are within a circle with a certain radius. **/ | |
857 | + public void setRandomPts(MouseEvent e) | |
858 | + { | |
859 | + boolean valid = false; | |
860 | + do{ | |
861 | + originX = randInt(e.getX()-radius,e.getX()+radius); | |
862 | + originY = randInt(e.getY()-radius,e.getX()+radius); | |
863 | + if(Math.pow(e.getX()-originX,2)+Math.pow(e.getY()-originY,2) <= Math.pow(radius,2)) | |
864 | + valid = true; | |
865 | + }while(!valid); | |
866 | + } | |
867 | + | |
868 | + /** Gets a random integer within a set bound **/ | |
869 | + public int randInt(int start, int end) | |
870 | + { | |
871 | + return (int)(Math.random()*Math.abs(end-start)+start); | |
872 | + } | |
873 | + | |
874 | + /** Draws a clear white rectangle onto the screen. **/ | |
875 | + public void clearImage() | |
876 | + { | |
877 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
878 | + g.clearRect(0,0,getWidth(),getHeight()); | |
879 | + repaint(); | |
880 | + } | |
881 | + | |
882 | + /** Set these variables with the values of the mouse coordinates. **/ | |
883 | + public void setFieldDefaults(MouseEvent e) | |
884 | + { | |
885 | + prevX = e.getX(); | |
886 | + prevY = e.getY(); | |
887 | + originX = e.getX(); | |
888 | + originY = e.getY(); | |
889 | + } | |
890 | + | |
891 | + /** Checks if the mouse has moved. **/ | |
892 | + public boolean mouseHasMoved(MouseEvent e) | |
893 | + { | |
894 | + return (mouseX != e.getX() || mouseY != e.getY()); | |
895 | + } | |
896 | + | |
897 | + /** | |
898 | + * If the curve is created and there is a focus then save and paste the image. | |
899 | + * When the mouse is clicked set the drawColor and the oppositeColor. Check | |
900 | + * if the polygon operation and the curve tool is interrupted. | |
901 | + **/ | |
902 | + public void mousePressed(MouseEvent e) | |
903 | + { | |
904 | + | |
905 | + if(e.getButton() == e.BUTTON1) | |
906 | + { | |
907 | + drawColor = ColorBox.foreGround.getBackground(); | |
908 | + oppositeColor = ColorBox.backGround.getBackground(); | |
909 | + } | |
910 | + if(e.getButton() == e.BUTTON3) | |
911 | + { | |
912 | + drawColor = ColorBox.backGround.getBackground(); | |
913 | + oppositeColor = ColorBox.foreGround.getBackground(); | |
914 | + } | |
915 | + checkPolyOff(); | |
916 | + checkCurveOff(); | |
917 | + } | |
918 | + | |
919 | + /** | |
920 | + * This method controls which operation is being performed when the mouse | |
921 | + * is released. | |
922 | + **/ | |
923 | + public void mouseReleased(MouseEvent e) | |
924 | + { | |
925 | + int currentTool = ToolBox.toolSelected; | |
926 | + | |
927 | + if(currentTool == ToolBox.POLYGON) | |
928 | + prePolyOperation(e); | |
929 | + else if(currentTool == ToolBox.CURVELINE) | |
930 | + curveLineOperation(e); | |
931 | + else if(currentTool == ToolBox.TEXT) | |
932 | + textOperation(e); | |
933 | + else if(currentTool == ToolBox.SELECT) | |
934 | + selectOperation(e); | |
935 | + else if(currentTool == ToolBox.FILL) | |
936 | + fillOperation(e); | |
937 | + else if(currentTool == ToolBox.RANDOMDRAW) | |
938 | + randomDraw(); | |
939 | + } | |
940 | + | |
941 | + /** | |
942 | + * This method controls which oepration is being performed when the mouse is | |
943 | + * being dragged. | |
944 | + **/ | |
945 | + public void mouseDragged(MouseEvent e) | |
946 | + { | |
947 | + dragSave = true; | |
948 | + | |
949 | + int currentTool = ToolBox.toolSelected; | |
950 | + | |
951 | + if(currentTool == ToolBox.ERASER) | |
952 | + eraserOperation(e); | |
953 | + else if(currentTool == ToolBox.PENCIL) | |
954 | + penOperation(e); | |
955 | + else if(currentTool == ToolBox.BRUSH) | |
956 | + brushOperation(e); | |
957 | + else if(currentTool == ToolBox.EYEDROPPER) | |
958 | + eyeDropperOperation(e); | |
959 | + else if(currentTool == ToolBox.SPRAY) | |
960 | + sprayOperation(e); | |
961 | + else if(currentTool == ToolBox.LINE) | |
962 | + lineOperation(e); | |
963 | + else if(currentTool == ToolBox.RECTANGLE) | |
964 | + rectOperation(e); | |
965 | + else if(currentTool == ToolBox.OVAL) | |
966 | + ovalOperation(e); | |
967 | + else if(currentTool == ToolBox.ROUNDRECTANGLE) | |
968 | + roundOperation(e); | |
969 | + else if(currentTool == ToolBox.DRAG) | |
970 | + dragOperation(e); | |
971 | + } | |
972 | + | |
973 | + /** Invoked when the mouse button has been clicked (pressed and released) on a component. **/ | |
974 | + public void mouseClicked(MouseEvent e) | |
975 | + { | |
976 | + // when the drag is released then undo the save. | |
977 | + dragSave = false; | |
978 | + } | |
979 | + | |
980 | + /** Invoked when the mouse enters a component.**/ | |
981 | + public void mouseEntered(MouseEvent e){} | |
982 | + /** Invoked when the mouse exits a component. **/ | |
983 | + public void mouseExited(MouseEvent e){} | |
984 | + /** If it is not the polygon operation then call setFieldDefaults. **/ | |
985 | + public void mouseMoved(MouseEvent e) | |
986 | + { | |
987 | + if(!polyStart) | |
988 | + setFieldDefaults(e); | |
989 | + } | |
990 | + /** If the focus is lost then do not save the screen. **/ | |
991 | + public void focusLost(FocusEvent e){save = false;} | |
992 | + /** If the focus is found then save the screen. **/ | |
993 | + public void focusGained(FocusEvent e){save = true;} | |
994 | + } | |
0 | 995 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,994 @@ |
1 | +import java.awt.Color; | |
2 | +import java.awt.Canvas; | |
3 | +import java.awt.Graphics; | |
4 | +import java.awt.event.MouseListener; | |
5 | +import java.awt.event.MouseMotionListener; | |
6 | +import java.awt.event.MouseEvent; | |
7 | +import java.io.File; | |
8 | +import java.awt.Image; | |
9 | +import javax.swing.ImageIcon; | |
10 | +import java.awt.Cursor; | |
11 | +import java.awt.Robot; | |
12 | +import java.awt.AWTException; | |
13 | +import java.awt.image.BufferedImage; | |
14 | +import java.awt.print.Printable; | |
15 | +import java.awt.print.PageFormat; | |
16 | +import java.awt.print.PrinterException; | |
17 | +import java.awt.Graphics2D; | |
18 | +import java.awt.Rectangle; | |
19 | +import java.awt.AWTException; | |
20 | +import java.awt.Font; | |
21 | +import java.awt.image.PixelGrabber; | |
22 | +import java.awt.image.MemoryImageSource; | |
23 | +import java.awt.geom.CubicCurve2D; | |
24 | +import java.awt.geom.CubicCurve2D.Float; | |
25 | +import java.awt.geom.Point2D; | |
26 | +import java.awt.event.FocusListener; | |
27 | +import java.awt.event.FocusEvent; | |
28 | +import java.awt.geom.AffineTransform; | |
29 | + | |
30 | +public class DrawingCanvas extends Canvas implements MouseListener, | |
31 | +MouseMotionListener, Printable, FocusListener | |
32 | +{ | |
33 | + public static final int FILL = 1, NORMAL = 2, OUTLINE = 3, RECT = 4, OVAL = 5, LINE = 6; | |
34 | + public static BufferedImage selectImage; | |
35 | + public static int extraOperations, lineWidth, radius, amtLines; | |
36 | + private Color drawColor, oppositeColor; | |
37 | + private BufferedImage printImage, resizeImage; | |
38 | + private CubicCurve2D c; | |
39 | + private Point2D p1, p2, p3, p4; | |
40 | + private boolean polyStart, arcStart, curveMade, curveMade2, save, dragSave; | |
41 | + private int mouseX, mouseY, prevX, prevY, originX, originY, width, height; | |
42 | + private int[] pixels; | |
43 | + | |
44 | + /** Creates a default DrawingCanvas. **/ | |
45 | + public DrawingCanvas() | |
46 | + { | |
47 | + // save is set to true unless the focus is lost | |
48 | + save = true; | |
49 | + // sets the default linewidth and the spray radius | |
50 | + lineWidth = 1; | |
51 | + radius = 5; | |
52 | + amtLines = 10; | |
53 | + // adds listeners and sets settings | |
54 | + setSize(400,400); | |
55 | + setBackground(Color.BLACK); | |
56 | + this.addMouseListener(this); | |
57 | + this.addMouseMotionListener(this); | |
58 | + this.addFocusListener(this); | |
59 | + } | |
60 | + | |
61 | + /** | |
62 | + * This methods adjusts the screen size of the user by taking a snapshot of | |
63 | + * the canvas and seeing if it shifts up or down and sets the pixels | |
64 | + * accordingly. | |
65 | + **/ | |
66 | + public void checkScreenSize() | |
67 | + { | |
68 | + saveFromResize(); | |
69 | + pixels = new int[getWidth()*getHeight()]; | |
70 | + PixelGrabber pg = new PixelGrabber(resizeImage, 0, 0, getWidth(), getHeight(), pixels, 0, getWidth()); | |
71 | + | |
72 | + try { | |
73 | + pg.grabPixels(); | |
74 | + } catch (InterruptedException ie) {} | |
75 | + | |
76 | + | |
77 | + if(pixels[0] != (Color.WHITE).getRGB()) | |
78 | + ControlClass.extraY = 78; | |
79 | + else | |
80 | + ControlClass.extraY = 71; | |
81 | + } | |
82 | + | |
83 | + /** Saves an image of the current screen so if the canvas resizes the image doesnt get deleted. **/ | |
84 | + public void saveFromResize() | |
85 | + { | |
86 | + try{ | |
87 | + resizeImage = new Robot().createScreenCapture( | |
88 | + new Rectangle(ControlClass.xCoord,ControlClass.yCoord,getWidth(),getHeight())); | |
89 | + }catch(AWTException awte){} | |
90 | + } | |
91 | + | |
92 | + /** Draws the image back to the screen after the canvas is resized **/ | |
93 | + public void pasteFromResize() | |
94 | + { | |
95 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
96 | + g.drawImage(resizeImage,0,0,null); | |
97 | + repaint(); | |
98 | + } | |
99 | + | |
100 | + /** Fills anything of the same color the first pixel selected. **/ | |
101 | + public void fillOperation(MouseEvent e) | |
102 | + { | |
103 | + // saves the image and then creates a pixel array | |
104 | + saveImageForPrinting(); | |
105 | + pixels = new int[getWidth()*getHeight()]; | |
106 | + | |
107 | + // grabs all the pixels from the image | |
108 | + PixelGrabber pg = new PixelGrabber(printImage, 0, 0, getWidth(), getHeight(), pixels, 0, getWidth()); | |
109 | + try { | |
110 | + pg.grabPixels(); | |
111 | + } catch (InterruptedException ie) {} | |
112 | + | |
113 | + // calls the start fill method, rgb is the color clicked on | |
114 | + int rgb = pixels[e.getY()*getWidth()+e.getX()]; | |
115 | + startFill(e.getX(),e.getY(),rgb); | |
116 | + | |
117 | + // creates an image from the pixel array and draws it back to the screen | |
118 | + Image img = createImage(new MemoryImageSource(getWidth(), getHeight(), pixels, 0, getWidth())); | |
119 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
120 | + g.drawImage(img,0,0,null); | |
121 | + repaint(); | |
122 | + saveFromResize(); | |
123 | + } | |
124 | + | |
125 | + /** Changes the color of the pixel at the x and y coordinate. **/ | |
126 | + private void changePixel(int x, int y, int rgb) | |
127 | + { | |
128 | + pixels[y*getWidth()+x] = rgb; | |
129 | + } | |
130 | + | |
131 | + /** Gets the pixel at the x and y point **/ | |
132 | + private int getPixel(int x, int y) | |
133 | + { | |
134 | + return pixels[y*getWidth()+x]; | |
135 | + } | |
136 | + | |
137 | + /** Helper method for fillOperation. This method does the actually filling. **/ | |
138 | + public void startFill(int x, int y, int original) | |
139 | + { | |
140 | + int i = y*getWidth()+x; | |
141 | + if(pixels[i] != original) | |
142 | + return; | |
143 | + | |
144 | + // sets the pixel quete to the whole canvas | |
145 | + int pixelQueue[] = new int[getWidth() * getHeight()]; | |
146 | + int pixelQueueSize = 0; | |
147 | + | |
148 | + // sets the first pixel | |
149 | + pixelQueue[0] = (y << 16) + x; | |
150 | + pixelQueueSize = 1; | |
151 | + | |
152 | + changePixel(x,y,original); | |
153 | + | |
154 | + // loop to check and does a flood fill to the original color | |
155 | + while (pixelQueueSize > 0) | |
156 | + { | |
157 | + x = pixelQueue[0] & 0xffff; | |
158 | + y = (pixelQueue[0] >> 16) & 0xffff; | |
159 | + pixelQueueSize--; | |
160 | + pixelQueue[0] = pixelQueue[pixelQueueSize]; | |
161 | + | |
162 | + if (x > 0) | |
163 | + { | |
164 | + if (getPixel(x-1, y) == original) | |
165 | + { | |
166 | + changePixel(x-1, y, drawColor.getRGB()); | |
167 | + pixelQueue[pixelQueueSize] = (y << 16) + x-1; | |
168 | + pixelQueueSize++; | |
169 | + } | |
170 | + } | |
171 | + | |
172 | + if (y > 0) | |
173 | + { | |
174 | + if (getPixel(x, y-1) == original) | |
175 | + { | |
176 | + changePixel(x, y-1, drawColor.getRGB()); | |
177 | + pixelQueue[pixelQueueSize] = ((y-1) << 16) + x; | |
178 | + pixelQueueSize++; | |
179 | + } | |
180 | + } | |
181 | + if (x < getWidth()-1) | |
182 | + { | |
183 | + if (getPixel(x+1, y) == original) | |
184 | + { | |
185 | + changePixel(x+1, y, drawColor.getRGB()); | |
186 | + pixelQueue[pixelQueueSize] = (y << 16) + x+1; | |
187 | + pixelQueueSize++; | |
188 | + } | |
189 | + } | |
190 | + if (y < getHeight()-1) | |
191 | + { | |
192 | + if (getPixel(x, y+1) == original) | |
193 | + { | |
194 | + changePixel(x, y+1, drawColor.getRGB()); | |
195 | + pixelQueue[pixelQueueSize] = ((y+1) << 16) + x; | |
196 | + pixelQueueSize++; | |
197 | + } | |
198 | + } | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + /** Helper method for the rotation. Sets the amount of rotation 90 degrees. **/ | |
203 | + private AffineTransform setRotation() | |
204 | + { | |
205 | + AffineTransform atright = AffineTransform.getRotateInstance(Math.PI / 2, getWidth() / 2, getHeight() / 2); | |
206 | + return atright; | |
207 | + } | |
208 | + | |
209 | + /** This method actually rotates the image 90 degrees. **/ | |
210 | + public void rotateOperation() | |
211 | + { | |
212 | + // saves the image and then clears it and draws it rotated 90 degrees. | |
213 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
214 | + Graphics2D g2 = (Graphics2D) g; | |
215 | + saveFromResize(); | |
216 | + clearImage(); | |
217 | + g2.drawImage(resizeImage, setRotation(), this); | |
218 | + repaint(); | |
219 | + } | |
220 | + | |
221 | + /** Saves the image so that it can be printed. **/ | |
222 | + public void saveImageForPrinting() | |
223 | + { | |
224 | + // saves the image of the screen | |
225 | + try{ | |
226 | + printImage = new Robot().createScreenCapture( | |
227 | + new Rectangle(ControlClass.xCoord,ControlClass.yCoord,getWidth(),getHeight())); | |
228 | + }catch(AWTException awte){} | |
229 | + } | |
230 | + | |
231 | + /** This is a helper method for print. Draws the image to be printed. **/ | |
232 | + public void drawImage(Graphics2D g2D) | |
233 | + { | |
234 | + // draws the print image out on paper | |
235 | + g2D.drawImage(printImage,0,0,null); | |
236 | + } | |
237 | + | |
238 | + /** Printing operation, it prints the image saved. **/ | |
239 | + public int print(Graphics g, PageFormat pf, int pIndex) throws PrinterException | |
240 | + { | |
241 | + if (pIndex > 0) | |
242 | + return Printable.NO_SUCH_PAGE; | |
243 | + | |
244 | + Graphics2D g2D = (Graphics2D)g; | |
245 | + drawImage(g2D); | |
246 | + return Printable.PAGE_EXISTS; | |
247 | + } | |
248 | + | |
249 | + /** Updates the screen and flips the buffers around. **/ | |
250 | + public void update(Graphics g) | |
251 | + { | |
252 | + getBufferStrategy().show(); | |
253 | + } | |
254 | + | |
255 | + /** Paints the image onto the screen. **/ | |
256 | + public void paint(Graphics g) | |
257 | + { | |
258 | + try{update(g);}catch(NullPointerException e){} | |
259 | + } | |
260 | + | |
261 | + /** Opens a selected image and draws it onto the screen. **/ | |
262 | + public void openOperation(String path) | |
263 | + { | |
264 | + // creates an image and draws it to the screen | |
265 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
266 | + ImageIcon ic = new ImageIcon(path); | |
267 | + g.drawImage((Image)ic.getImage(),0,0,null); | |
268 | + } | |
269 | + | |
270 | + /** Selects the image from the top left corner to the bottom right corner. **/ | |
271 | + public void selectOperation(MouseEvent e) | |
272 | + { | |
273 | + if(mouseHasMoved(e)) | |
274 | + { | |
275 | + // this checks if you select off screen, if your off screen then it will | |
276 | + // set the boundaries to the edge of the drawing canvas | |
277 | + int tOriginX = ControlClass.xCoord+e.getX(); | |
278 | + int tOriginY = ControlClass.yCoord+e.getY(); | |
279 | + | |
280 | + if(tOriginX > getWidth()+ControlClass.xCoord) | |
281 | + tOriginX = getWidth()+ControlClass.xCoord; | |
282 | + if(tOriginX < ControlClass.xCoord) | |
283 | + tOriginX = ControlClass.xCoord; | |
284 | + if(tOriginY > getHeight()+ControlClass.yCoord) | |
285 | + tOriginY = getHeight()+ControlClass.yCoord; | |
286 | + if(tOriginY < ControlClass.yCoord) | |
287 | + tOriginY = ControlClass.yCoord; | |
288 | + | |
289 | + // calculates the width and height of the area | |
290 | + width = tOriginX-(ControlClass.xCoord+originX); | |
291 | + height = tOriginY-(ControlClass.yCoord+originY); | |
292 | + | |
293 | + if(width < 0 && height < 0) | |
294 | + { | |
295 | + originX+=width; | |
296 | + originY+=height; | |
297 | + } | |
298 | + else if(width < 0 && height > 0) | |
299 | + originX+=width; | |
300 | + else if(width > 0 && height < 0) | |
301 | + originY+=height; | |
302 | + | |
303 | + width = Math.abs(width); | |
304 | + height = Math.abs(height); | |
305 | + } | |
306 | + } | |
307 | + | |
308 | + /** Copies the selected image **/ | |
309 | + public void copyOperation() | |
310 | + { | |
311 | + // copies the boundaries set by the mouse | |
312 | + try{ | |
313 | + DrawingCanvas.selectImage = new Robot().createScreenCapture( | |
314 | + new Rectangle(ControlClass.xCoord+originX,ControlClass.yCoord+originY, | |
315 | + width,height)); | |
316 | + }catch(AWTException awte){} | |
317 | + } | |
318 | + /** Copies the selected image and deletes the selected portion **/ | |
319 | + public void cutOperation() | |
320 | + { | |
321 | + // copies the image and then draws a rectangle over the selected area | |
322 | + copyOperation(); | |
323 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
324 | + g.setColor(oppositeColor); | |
325 | + g.fillRect(originX,originY,width,height); | |
326 | + width = height = 0; | |
327 | + repaint(); | |
328 | + } | |
329 | + | |
330 | + /** Draws the image with regards to the mouse location. **/ | |
331 | + public void pasteOperation() | |
332 | + { | |
333 | + // draws the image of the selected image onto the screen | |
334 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
335 | + g.drawImage(DrawingCanvas.selectImage,originX,originY,null); | |
336 | + repaint(); | |
337 | + } | |
338 | + | |
339 | + /** Draws random lines of random colors on the screen. **/ | |
340 | + public void randomDraw() | |
341 | + { | |
342 | + RandomPoint rPt = new RandomPoint(getWidth(),getHeight()); | |
343 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
344 | + int i=0; | |
345 | + while(i< amtLines) | |
346 | + { | |
347 | + g.setColor(rPt.getColor()); | |
348 | + g.drawLine((int)rPt.getX(),(int)rPt.getY(),rPt.nextX(),rPt.nextY()); | |
349 | + i++; | |
350 | + } | |
351 | + repaint(); | |
352 | + } | |
353 | + | |
354 | + /** Opens up a text dialog in the selected area. **/ | |
355 | + public void textOperation(MouseEvent e) | |
356 | + { | |
357 | + // opens up a text dialog | |
358 | + setCursor(new Cursor(Cursor.TEXT_CURSOR)); | |
359 | + TextDialog tDialog = new TextDialog(e.getX(),e.getY()); | |
360 | + } | |
361 | + | |
362 | + /** Prints the text onto the screen **/ | |
363 | + public void textOperation2(String txt, int x, int y, Font f) | |
364 | + { | |
365 | + // draws the text onto the screen | |
366 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
367 | + g.setFont(f); | |
368 | + g.setColor(drawColor); | |
369 | + g.drawString(txt,x,y); | |
370 | + g.setFont(new Font("Times New Roman", Font.PLAIN, 12)); | |
371 | + repaint(); | |
372 | + } | |
373 | + | |
374 | + /** An eraser that erases when the mouse is dragged. **/ | |
375 | + public void eraserOperation(MouseEvent e) | |
376 | + { | |
377 | + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); | |
378 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
379 | + g.setColor(ColorBox.backGround.getBackground()); | |
380 | + | |
381 | + if (mouseHasMoved(e)) | |
382 | + g.fillRect(e.getX(),e.getY(),15,15); | |
383 | + | |
384 | + repaint(); | |
385 | + } | |
386 | + | |
387 | + /** Paint brush with different brushes like a square and a circle. **/ | |
388 | + public void brushOperation(MouseEvent e) | |
389 | + { | |
390 | + setCursor(new Cursor(Cursor.HAND_CURSOR)); | |
391 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
392 | + g.setColor(drawColor); | |
393 | + | |
394 | + // depending on which brush is selected it will change the brush to that shape | |
395 | + if (mouseHasMoved(e)) | |
396 | + if(extraOperations == OVAL) | |
397 | + g.fillOval(e.getX(),e.getY(),radius,radius); | |
398 | + else if(extraOperations == RECT) | |
399 | + g.fillRect(e.getX(),e.getY(),radius,radius); | |
400 | + repaint(); | |
401 | + } | |
402 | + | |
403 | + /** Creates a pen when can be operated when mouse is dragged. **/ | |
404 | + public void penOperation(MouseEvent e) | |
405 | + { | |
406 | + setCursor(new Cursor(Cursor.HAND_CURSOR)); | |
407 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
408 | + g.setColor(drawColor); | |
409 | + | |
410 | + // draws lines while your mouse is dragged | |
411 | + if (mouseHasMoved(e)) | |
412 | + { | |
413 | + mouseX = e.getX(); | |
414 | + mouseY = e.getY(); | |
415 | + | |
416 | + g.drawLine(prevX,prevY,mouseX,mouseY); | |
417 | + | |
418 | + prevX = mouseX; | |
419 | + prevY = mouseY; | |
420 | + } | |
421 | + repaint(); | |
422 | + } | |
423 | + | |
424 | + /** Calls the line operation first and then starts the polygon operation **/ | |
425 | + public void prePolyOperation(MouseEvent e) | |
426 | + { | |
427 | + if(!polyStart) | |
428 | + { | |
429 | + lineOperation(e); | |
430 | + polyStart = true; | |
431 | + } | |
432 | + polyOperation(e); | |
433 | + } | |
434 | + | |
435 | + /** Starts a polygon shape by drawing many line. **/ | |
436 | + public void polyOperation(MouseEvent e) | |
437 | + { | |
438 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
439 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
440 | + g.setColor(drawColor); | |
441 | + | |
442 | + // draws a line on the screen | |
443 | + if(mouseHasMoved(e)) | |
444 | + { | |
445 | + mouseX = e.getX(); | |
446 | + mouseY = e.getY(); | |
447 | + | |
448 | + g.drawLine(prevX, prevY, mouseX, mouseY); | |
449 | + | |
450 | + prevX = mouseX; | |
451 | + prevY = mouseY; | |
452 | + } | |
453 | + | |
454 | + // sets the polygon operation to false if the line is back at the origin | |
455 | + if(isBeginning(e)) | |
456 | + polyStart = false; | |
457 | + | |
458 | + repaint(); | |
459 | + } | |
460 | + | |
461 | + /** Helper method for the polygon to check if the line drawn is back at the origin. **/ | |
462 | + public boolean isBeginning(MouseEvent e) | |
463 | + { | |
464 | + // returns true or false if the range is five away from the beginning | |
465 | + return (Math.abs(originX-e.getX()) <= 5 && Math.abs(originY-e.getY()) <= 5); | |
466 | + } | |
467 | + | |
468 | + /** Checks if the polygon operation is deselected and stops it. **/ | |
469 | + public void checkPolyOff() | |
470 | + { | |
471 | + // if the polygon operation is on and the polygon tool is not selected then | |
472 | + // finish the line if it wasnt completed and reset the polygon operation | |
473 | + if(polyStart && ToolBox.toolSelected != ToolBox.POLYGON) | |
474 | + { | |
475 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
476 | + g.setColor(drawColor); | |
477 | + g.drawLine(prevX,prevY,originX,originY); | |
478 | + polyStart = false; | |
479 | + } | |
480 | + } | |
481 | + | |
482 | + /** Checks if the curve line tool is deselected. **/ | |
483 | + public void checkCurveOff() | |
484 | + { | |
485 | + // if the tool is deselected then reset variables to default | |
486 | + if(ToolBox.toolSelected != ToolBox.CURVELINE) | |
487 | + curveMade = curveMade2 = false; | |
488 | + } | |
489 | + | |
490 | + /** Starts the curve line tool. **/ | |
491 | + public void curveLineOperation(MouseEvent e) | |
492 | + { | |
493 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
494 | + g.setColor(drawColor); | |
495 | + | |
496 | + if(mouseHasMoved(e)) | |
497 | + { | |
498 | + mouseX = e.getX(); | |
499 | + mouseY = e.getY(); | |
500 | + | |
501 | + Graphics2D g2 = (Graphics2D)g; | |
502 | + | |
503 | + // if the first and second point are not choosen then draw a straight line | |
504 | + if(!curveMade && !curveMade2) | |
505 | + { | |
506 | + c = new CubicCurve2D.Float(); | |
507 | + p1 = new Point2D.Float(); | |
508 | + p2 = new Point2D.Float(); | |
509 | + p3 = new Point2D.Float(); | |
510 | + p4 = new Point2D.Float(); | |
511 | + p1.setLocation(originX, originY); | |
512 | + p4.setLocation(e.getX(), e.getY()); | |
513 | + curveMade = true; | |
514 | + g.drawLine(originX,originY,e.getX(),e.getY()); | |
515 | + repaint(); | |
516 | + } | |
517 | + // once one of the curves are made then you select a point | |
518 | + else if(curveMade && !curveMade2) | |
519 | + { | |
520 | + pasteFromResize(); | |
521 | + p2.setLocation(e.getX(), e.getY()); | |
522 | + p3.setLocation(e.getX(), e.getY()); | |
523 | + curveMade2 = true; | |
524 | + c.setCurve(p1, p2, p3, p4); | |
525 | + g2.draw(c); | |
526 | + repaint(); | |
527 | + } | |
528 | + // once both points are choosen then the curve is drawn onto the screen | |
529 | + else if(curveMade2 && curveMade) | |
530 | + { | |
531 | + pasteFromResize(); | |
532 | + p3.setLocation(e.getX(), e.getY()); | |
533 | + c.setCurve(p1, p2, p3, p4); | |
534 | + g2.draw(c); | |
535 | + curveMade = false; | |
536 | + curveMade2 = false; | |
537 | + } | |
538 | + } | |
539 | + repaint(); | |
540 | + } | |
541 | + | |
542 | + /** Changes the foreground color to the selected color. **/ | |
543 | + public void eyeDropperOperation(MouseEvent e) | |
544 | + { | |
545 | + // creates a screen shot and takes the pixel color where the mouse is situated | |
546 | + try{ | |
547 | + Robot robot = new Robot(); | |
548 | + ColorBox.foreGround.setBackground(robot.getPixelColor(ControlClass.xCoord+e.getX(),ControlClass.yCoord+e.getY())); | |
549 | + }catch(AWTException awt){} | |
550 | + } | |
551 | + | |
552 | + /** Creates a line from two points. **/ | |
553 | + public void lineOperation(MouseEvent e) | |
554 | + { | |
555 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
556 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
557 | + g.setColor(drawColor); | |
558 | + | |
559 | + if (mouseHasMoved(e)) | |
560 | + { | |
561 | + mouseX = e.getX(); | |
562 | + mouseY = e.getY(); | |
563 | + | |
564 | + g.drawLine(originX, originY, mouseX, mouseY); | |
565 | + | |
566 | + prevX = mouseX; | |
567 | + prevY = mouseY; | |
568 | + } | |
569 | + pasteFromResize(); | |
570 | + g.drawLine(originX, originY, mouseX, mouseY); | |
571 | + repaint(); | |
572 | + } | |
573 | + | |
574 | + /** | |
575 | + * Draw a rectangle from two given points. Draws a fill operation, a outlined | |
576 | + * rectangle or a outlined with fill operation. | |
577 | + **/ | |
578 | + public void drawRectangle(MouseEvent e) | |
579 | + { | |
580 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
581 | + g.setColor(drawColor); | |
582 | + | |
583 | + // gets the mouse coordinate and gets the width and height | |
584 | + mouseX = e.getX(); | |
585 | + mouseY = e.getY(); | |
586 | + int width = mouseX-originX; | |
587 | + int height = mouseY-originY; | |
588 | + | |
589 | + // draws a filled rectangle | |
590 | + if(extraOperations == FILL) | |
591 | + { | |
592 | + if(width > 0 && height > 0) | |
593 | + g.fillRect(originX,originY,width,height); | |
594 | + else if(width < 0 && height < 0) | |
595 | + g.fillRect(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
596 | + else if(width < 0 && height > 0) | |
597 | + g.fillRect(originX+width,originY,Math.abs(width),height); | |
598 | + else if(width > 0 && height < 0) | |
599 | + g.fillRect(originX,originY+height,width,Math.abs(height)); | |
600 | + } | |
601 | + // draws a normal rectangle | |
602 | + else if(extraOperations == NORMAL) | |
603 | + { | |
604 | + if(width > 0 && height > 0) | |
605 | + for(int i=0;i<lineWidth;i++) | |
606 | + g.drawRect(originX+i,originY+i,width-(i*2),height-(i*2)); | |
607 | + else if(width < 0 && height < 0) | |
608 | + for(int i=0;i<lineWidth;i++) | |
609 | + g.drawRect(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2)); | |
610 | + else if(width < 0 && height > 0) | |
611 | + for(int i=0;i<lineWidth;i++) | |
612 | + g.drawRect(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2)); | |
613 | + else if(width > 0 && height < 0) | |
614 | + for(int i=0;i<lineWidth;i++) | |
615 | + g.drawRect(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2)); | |
616 | + } | |
617 | + // draws a filled rectangle with an outline | |
618 | + else if(extraOperations == OUTLINE) | |
619 | + { | |
620 | + if(width > 0 && height > 0) | |
621 | + { | |
622 | + g.fillRect(originX,originY,width,height); | |
623 | + g.setColor(oppositeColor); | |
624 | + g.fillRect(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2)); | |
625 | + } | |
626 | + else if(width < 0 && height < 0) | |
627 | + { | |
628 | + g.fillRect(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
629 | + g.setColor(oppositeColor); | |
630 | + g.fillRect(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
631 | + } | |
632 | + else if(width < 0 && height > 0) | |
633 | + { | |
634 | + g.fillRect(originX+width,originY,Math.abs(width),height); | |
635 | + g.setColor(oppositeColor); | |
636 | + g.fillRect(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2)); | |
637 | + } | |
638 | + else if(width > 0 && height < 0) | |
639 | + { | |
640 | + g.fillRect(originX,originY+height,width,Math.abs(height)); | |
641 | + g.setColor(oppositeColor); | |
642 | + g.fillRect(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
643 | + } | |
644 | + } | |
645 | + } | |
646 | + | |
647 | + /** Calls the drawRectangle operation. **/ | |
648 | + public void rectOperation(MouseEvent e) | |
649 | + { | |
650 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
651 | + | |
652 | + if (mouseHasMoved(e)) | |
653 | + drawRectangle(e); | |
654 | + | |
655 | + pasteFromResize(); | |
656 | + drawRectangle(e); | |
657 | + repaint(); | |
658 | + } | |
659 | + | |
660 | + /** This method draws a rectangle with rounded edges. **/ | |
661 | + public void drawRoundRectangle(MouseEvent e) | |
662 | + { | |
663 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
664 | + g.setColor(drawColor); | |
665 | + | |
666 | + // gets the mouse coordinate and gets the width and height | |
667 | + mouseX = e.getX(); | |
668 | + mouseY = e.getY(); | |
669 | + int width = mouseX-originX; | |
670 | + int height = mouseY-originY; | |
671 | + | |
672 | + // draws a filled rounded rectangle | |
673 | + if(extraOperations == FILL) | |
674 | + { | |
675 | + if(width > 0 && height > 0) | |
676 | + g.fillRoundRect(originX,originY,width,height,25,25); | |
677 | + else if(width < 0 && height < 0) | |
678 | + g.fillRoundRect(originX+width,originY+height,Math.abs(width),Math.abs(height),25,25); | |
679 | + else if(width < 0 && height > 0) | |
680 | + g.fillRoundRect(originX+width,originY,Math.abs(width),height,25,25); | |
681 | + else if(width > 0 && height < 0) | |
682 | + g.fillRoundRect(originX,originY+height,width,Math.abs(height),25,25); | |
683 | + } | |
684 | + // draws a normal rounded rectangle | |
685 | + else if(extraOperations == NORMAL) | |
686 | + { | |
687 | + if(width > 0 && height > 0) | |
688 | + for(int i=0;i<lineWidth;i++) | |
689 | + g.drawRoundRect(originX+i,originY+i,width-(i*2),height-(i*2),25,25); | |
690 | + else if(width < 0 && height < 0) | |
691 | + for(int i=0;i<lineWidth;i++) | |
692 | + g.drawRoundRect(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2),25,25); | |
693 | + else if(width < 0 && height > 0) | |
694 | + for(int i=0;i<lineWidth;i++) | |
695 | + g.drawRoundRect(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2),25,25); | |
696 | + else if(width > 0 && height < 0) | |
697 | + for(int i=0;i<lineWidth;i++) | |
698 | + g.drawRoundRect(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2),25,25); | |
699 | + } | |
700 | + // draws a filled rounded rectangle with an outline | |
701 | + else if(extraOperations == OUTLINE) | |
702 | + { | |
703 | + if(width > 0 && height > 0) | |
704 | + { | |
705 | + g.fillRoundRect(originX,originY,width,height,25,25); | |
706 | + g.setColor(oppositeColor); | |
707 | + g.fillRoundRect(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2),25,25); | |
708 | + } | |
709 | + else if(width < 0 && height < 0) | |
710 | + { | |
711 | + g.fillRoundRect(originX+width,originY+height,Math.abs(width),Math.abs(height),25,25); | |
712 | + g.setColor(oppositeColor); | |
713 | + g.fillRoundRect(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2),25,25); | |
714 | + } | |
715 | + else if(width < 0 && height > 0) | |
716 | + { | |
717 | + g.fillRoundRect(originX+width,originY,Math.abs(width),height,25,25); | |
718 | + g.setColor(oppositeColor); | |
719 | + g.fillRoundRect(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2),25,25); | |
720 | + } | |
721 | + else if(width > 0 && height < 0) | |
722 | + { | |
723 | + g.fillRoundRect(originX,originY+height,width,Math.abs(height),25,25); | |
724 | + g.setColor(oppositeColor); | |
725 | + g.fillRoundRect(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2),25,25); | |
726 | + } | |
727 | + } | |
728 | + } | |
729 | + | |
730 | + /** Calls the drawRoundRectangle operation. **/ | |
731 | + public void roundOperation(MouseEvent e) | |
732 | + { | |
733 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
734 | + | |
735 | + if (mouseHasMoved(e)) | |
736 | + drawRoundRectangle(e); | |
737 | + | |
738 | + pasteFromResize(); | |
739 | + drawRoundRectangle(e); | |
740 | + repaint(); | |
741 | + } | |
742 | + | |
743 | + /** Draws an oval onto the screen. **/ | |
744 | + public void drawOval(MouseEvent e) | |
745 | + { | |
746 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
747 | + g.setColor(drawColor); | |
748 | + | |
749 | + // gets the mouse coordinate and gets the width and height | |
750 | + mouseX = e.getX(); | |
751 | + mouseY = e.getY(); | |
752 | + int width = mouseX-originX; | |
753 | + int height = mouseY-originY; | |
754 | + | |
755 | + // draws a filled oval | |
756 | + if(extraOperations == FILL) | |
757 | + { | |
758 | + if(width > 0 && height > 0) | |
759 | + g.fillOval(originX,originY,width,height); | |
760 | + else if(width < 0 && height < 0) | |
761 | + g.fillOval(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
762 | + else if(width < 0 && height > 0) | |
763 | + g.fillOval(originX+width,originY,Math.abs(width),height); | |
764 | + else if(width > 0 && height < 0) | |
765 | + g.fillOval(originX,originY+height,width,Math.abs(height)); | |
766 | + } | |
767 | + // draws a normal oval | |
768 | + else if(extraOperations == NORMAL) | |
769 | + { | |
770 | + if(width > 0 && height > 0) | |
771 | + for(int i=0;i<lineWidth;i++) | |
772 | + g.drawOval(originX+i,originY+i,width-(i*2),height-(i*2)); | |
773 | + else if(width < 0 && height < 0) | |
774 | + for(int i=0;i<lineWidth;i++) | |
775 | + g.drawOval(originX+width+i,originY+height+i,Math.abs(width)-(i*2),Math.abs(height)-(i*2)); | |
776 | + else if(width < 0 && height > 0) | |
777 | + for(int i=0;i<lineWidth;i++) | |
778 | + g.drawOval(originX+width+i,originY+i,Math.abs(width)-(i*2),height-(i*2)); | |
779 | + else if(width > 0 && height < 0) | |
780 | + for(int i=0;i<lineWidth;i++) | |
781 | + g.drawOval(originX+i,originY+height+i,width-(i*2),Math.abs(height)-(i*2)); | |
782 | + } | |
783 | + // draws a filled oval with an outline | |
784 | + else if(extraOperations == OUTLINE) | |
785 | + { | |
786 | + if(width > 0 && height > 0) | |
787 | + { | |
788 | + g.fillOval(originX,originY,width,height); | |
789 | + g.setColor(oppositeColor); | |
790 | + g.fillOval(originX+lineWidth,originY+lineWidth,width-(lineWidth*2),height-(lineWidth*2)); | |
791 | + } | |
792 | + else if(width < 0 && height < 0) | |
793 | + { | |
794 | + g.fillOval(originX+width,originY+height,Math.abs(width),Math.abs(height)); | |
795 | + g.setColor(oppositeColor); | |
796 | + g.fillOval(originX+width+lineWidth,originY+height+lineWidth,Math.abs(width)-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
797 | + } | |
798 | + else if(width < 0 && height > 0) | |
799 | + { | |
800 | + g.fillOval(originX+width,originY,Math.abs(width),height); | |
801 | + g.setColor(oppositeColor); | |
802 | + g.fillOval(originX+width+lineWidth,originY+lineWidth,Math.abs(width)-(lineWidth*2),height-(lineWidth*2)); | |
803 | + } | |
804 | + else if(width > 0 && height < 0) | |
805 | + { | |
806 | + g.fillOval(originX,originY+height,width,Math.abs(height)); | |
807 | + g.setColor(oppositeColor); | |
808 | + g.fillOval(originX+lineWidth,originY+height+lineWidth,width-(lineWidth*2),Math.abs(height)-(lineWidth*2)); | |
809 | + } | |
810 | + } | |
811 | + } | |
812 | + | |
813 | + /** Calls the drawOval operation. **/ | |
814 | + public void ovalOperation(MouseEvent e) | |
815 | + { | |
816 | + setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); | |
817 | + | |
818 | + if (mouseHasMoved(e)) | |
819 | + drawOval(e); | |
820 | + | |
821 | + pasteFromResize(); | |
822 | + drawOval(e); | |
823 | + repaint(); | |
824 | + } | |
825 | + | |
826 | + /** Drag Brush Operation. You draw a shape around as your brush. **/ | |
827 | + public void dragOperation(MouseEvent e) | |
828 | + { | |
829 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
830 | + g.setColor(drawColor); | |
831 | + | |
832 | + if (mouseHasMoved(e)) | |
833 | + if(extraOperations == LINE) | |
834 | + g.drawLine(e.getX(),e.getY(),e.getX()+radius,e.getY()-radius); | |
835 | + else if(extraOperations == RECT) | |
836 | + g.drawRect(e.getX(),e.getY(),radius,radius); | |
837 | + else if(extraOperations == OVAL) | |
838 | + g.drawOval(e.getX(),e.getY(),radius,radius); | |
839 | + repaint(); | |
840 | + } | |
841 | + | |
842 | + /** Spray brush tool. Draws many tiny dots within a set radius. **/ | |
843 | + public void sprayOperation(MouseEvent e) | |
844 | + { | |
845 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
846 | + g.setColor(drawColor); | |
847 | + // sets random points to the origin and draws them onto the screen | |
848 | + for(int i=0;i<radius;i++) | |
849 | + { | |
850 | + setRandomPts(e); | |
851 | + g.fillOval(originX,originY,1,1); | |
852 | + repaint(); | |
853 | + } | |
854 | + } | |
855 | + | |
856 | + /** Gets random points that are within a circle with a certain radius. **/ | |
857 | + public void setRandomPts(MouseEvent e) | |
858 | + { | |
859 | + boolean valid = false; | |
860 | + do{ | |
861 | + originX = randInt(e.getX()-radius,e.getX()+radius); | |
862 | + originY = randInt(e.getY()-radius,e.getX()+radius); | |
863 | + if(Math.pow(e.getX()-originX,2)+Math.pow(e.getY()-originY,2) <= Math.pow(radius,2)) | |
864 | + valid = true; | |
865 | + }while(!valid); | |
866 | + } | |
867 | + | |
868 | + /** Gets a random integer within a set bound **/ | |
869 | + public int randInt(int start, int end) | |
870 | + { | |
871 | + return (int)(Math.random()*Math.abs(end-start)+start); | |
872 | + } | |
873 | + | |
874 | + /** Draws a clear white rectangle onto the screen. **/ | |
875 | + public void clearImage() | |
876 | + { | |
877 | + Graphics g = getBufferStrategy().getDrawGraphics(); | |
878 | + g.clearRect(0,0,getWidth(),getHeight()); | |
879 | + repaint(); | |
880 | + } | |
881 | + | |
882 | + /** Set these variables with the values of the mouse coordinates. **/ | |
883 | + public void setFieldDefaults(MouseEvent e) | |
884 | + { | |
885 | + prevX = e.getX(); | |
886 | + prevY = e.getY(); | |
887 | + originX = e.getX(); | |
888 | + originY = e.getY(); | |
889 | + } | |
890 | + | |
891 | + /** Checks if the mouse has moved. **/ | |
892 | + public boolean mouseHasMoved(MouseEvent e) | |
893 | + { | |
894 | + return (mouseX != e.getX() || mouseY != e.getY()); | |
895 | + } | |
896 | + | |
897 | + /** | |
898 | + * If the curve is created and there is a focus then save and paste the image. | |
899 | + * When the mouse is clicked set the drawColor and the oppositeColor. Check | |
900 | + * if the polygon operation and the curve tool is interrupted. | |
901 | + **/ | |
902 | + public void mousePressed(MouseEvent e) | |
903 | + { | |
904 | + | |
905 | + if(e.getButton() == e.BUTTON1) | |
906 | + { | |
907 | + drawColor = ColorBox.foreGround.getBackground(); | |
908 | + oppositeColor = ColorBox.backGround.getBackground(); | |
909 | + } | |
910 | + if(e.getButton() == e.BUTTON3) | |
911 | + { | |
912 | + drawColor = ColorBox.backGround.getBackground(); | |
913 | + oppositeColor = ColorBox.foreGround.getBackground(); | |
914 | + } | |
915 | + checkPolyOff(); | |
916 | + checkCurveOff(); | |
917 | + } | |
918 | + | |
919 | + /** | |
920 | + * This method controls which operation is being performed when the mouse | |
921 | + * is released. | |
922 | + **/ | |
923 | + public void mouseReleased(MouseEvent e) | |
924 | + { | |
925 | + int currentTool = ToolBox.toolSelected; | |
926 | + | |
927 | + if(currentTool == ToolBox.POLYGON) | |
928 | + prePolyOperation(e); | |
929 | + else if(currentTool == ToolBox.CURVELINE) | |
930 | + curveLineOperation(e); | |
931 | + else if(currentTool == ToolBox.TEXT) | |
932 | + textOperation(e); | |
933 | + else if(currentTool == ToolBox.SELECT) | |
934 | + selectOperation(e); | |
935 | + else if(currentTool == ToolBox.FILL) | |
936 | + fillOperation(e); | |
937 | + else if(currentTool == ToolBox.RANDOMDRAW) | |
938 | + randomDraw(); | |
939 | + } | |
940 | + | |
941 | + /** | |
942 | + * This method controls which oepration is being performed when the mouse is | |
943 | + * being dragged. | |
944 | + **/ | |
945 | + public void mouseDragged(MouseEvent e) | |
946 | + { | |
947 | + dragSave = true; | |
948 | + | |
949 | + int currentTool = ToolBox.toolSelected; | |
950 | + | |
951 | + if(currentTool == ToolBox.ERASER) | |
952 | + eraserOperation(e); | |
953 | + else if(currentTool == ToolBox.PENCIL) | |
954 | + penOperation(e); | |
955 | + else if(currentTool == ToolBox.BRUSH) | |
956 | + brushOperation(e); | |
957 | + else if(currentTool == ToolBox.EYEDROPPER) | |
958 | + eyeDropperOperation(e); | |
959 | + else if(currentTool == ToolBox.SPRAY) | |
960 | + sprayOperation(e); | |
961 | + else if(currentTool == ToolBox.LINE) | |
962 | + lineOperation(e); | |
963 | + else if(currentTool == ToolBox.RECTANGLE) | |
964 | + rectOperation(e); | |
965 | + else if(currentTool == ToolBox.OVAL) | |
966 | + ovalOperation(e); | |
967 | + else if(currentTool == ToolBox.ROUNDRECTANGLE) | |
968 | + roundOperation(e); | |
969 | + else if(currentTool == ToolBox.DRAG) | |
970 | + dragOperation(e); | |
971 | + } | |
972 | + | |
973 | + /** Invoked when the mouse button has been clicked (pressed and released) on a component. **/ | |
974 | + public void mouseClicked(MouseEvent e) | |
975 | + { | |
976 | + // when the drag is released then undo the save. | |
977 | + dragSave = false; | |
978 | + } | |
979 | + | |
980 | + /** Invoked when the mouse enters a component.**/ | |
981 | + public void mouseEntered(MouseEvent e){} | |
982 | + /** Invoked when the mouse exits a component. **/ | |
983 | + public void mouseExited(MouseEvent e){} | |
984 | + /** If it is not the polygon operation then call setFieldDefaults. **/ | |
985 | + public void mouseMoved(MouseEvent e) | |
986 | + { | |
987 | + if(!polyStart) | |
988 | + setFieldDefaults(e); | |
989 | + } | |
990 | + /** If the focus is lost then do not save the screen. **/ | |
991 | + public void focusLost(FocusEvent e){save = false;} | |
992 | + /** If the focus is found then save the screen. **/ | |
993 | + public void focusGained(FocusEvent e){save = true;} | |
994 | + } | |
0 | 995 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,189 @@ |
1 | +import java.io.File; | |
2 | +import java.util.Hashtable; | |
3 | +import java.util.Enumeration; | |
4 | +import javax.swing.*; | |
5 | +import javax.swing.filechooser.*; | |
6 | + | |
7 | +public class ExampleFileFilter extends FileFilter { | |
8 | + | |
9 | + private Hashtable filters = null; | |
10 | + private String description = null; | |
11 | + private String fullDescription = null; | |
12 | + private boolean useExtensionsInDescription = true; | |
13 | + | |
14 | + /** | |
15 | + * Creates a file filter. If no filters are added, then all | |
16 | + * files are accepted. | |
17 | + * | |
18 | + */ | |
19 | + public ExampleFileFilter() { | |
20 | + this.filters = new Hashtable(); | |
21 | + } | |
22 | + | |
23 | + /** | |
24 | + * Creates a file filter that accepts files with the given extension. | |
25 | + * Example: new ExampleFileFilter("jpg"); | |
26 | + * | |
27 | + */ | |
28 | + public ExampleFileFilter(String extension) { | |
29 | + this(extension,null); | |
30 | + } | |
31 | + | |
32 | + /** | |
33 | + * Creates a file filter that accepts the given file type. | |
34 | + * Example: new ExampleFileFilter("jpg", "JPEG Image Images"); | |
35 | + * | |
36 | + * Note that the "." before the extension is not needed. If | |
37 | + * provided, it will be ignored. | |
38 | + * | |
39 | + */ | |
40 | + public ExampleFileFilter(String extension, String description) { | |
41 | + this(); | |
42 | + if(extension!=null) addExtension(extension); | |
43 | + if(description!=null) setDescription(description); | |
44 | + } | |
45 | + | |
46 | + /** | |
47 | + * Creates a file filter from the given string array. | |
48 | + * Example: new ExampleFileFilter(String {"gif", "jpg"}); | |
49 | + * | |
50 | + * Note that the "." before the extension is not needed adn | |
51 | + * will be ignored. | |
52 | + * | |
53 | + */ | |
54 | + public ExampleFileFilter(String[] filters) { | |
55 | + this(filters, null); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * Creates a file filter from the given string array and description. | |
60 | + * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); | |
61 | + * | |
62 | + * Note that the "." before the extension is not needed and will be ignored. | |
63 | + * | |
64 | + */ | |
65 | + public ExampleFileFilter(String[] filters, String description) { | |
66 | + this(); | |
67 | + for (int i = 0; i < filters.length; i++) { | |
68 | + // add filters one by one | |
69 | + addExtension(filters[i]); | |
70 | + } | |
71 | + if(description!=null) setDescription(description); | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * Return true if this file should be shown in the directory pane, | |
76 | + * false if it shouldn't. | |
77 | + * | |
78 | + * Files that begin with "." are ignored. | |
79 | + * | |
80 | + */ | |
81 | + public boolean accept(File f) { | |
82 | + if(f != null) { | |
83 | + if(f.isDirectory()) { | |
84 | + return true; | |
85 | + } | |
86 | + String extension = getExtension(f); | |
87 | + if(extension != null && filters.get(getExtension(f)) != null) { | |
88 | + return true; | |
89 | + }; | |
90 | + } | |
91 | + return false; | |
92 | + } | |
93 | + | |
94 | + /** | |
95 | + * Return the extension portion of the file's name . | |
96 | + * | |
97 | + */ | |
98 | + public String getExtension(File f) { | |
99 | + if(f != null) { | |
100 | + String filename = f.getName(); | |
101 | + int i = filename.lastIndexOf('.'); | |
102 | + if(i>0 && i<filename.length()-1) { | |
103 | + return filename.substring(i+1).toLowerCase(); | |
104 | + }; | |
105 | + } | |
106 | + return null; | |
107 | + } | |
108 | + | |
109 | + /** | |
110 | + * Adds a filetype "dot" extension to filter against. | |
111 | + * | |
112 | + * For example: the following code will create a filter that filters | |
113 | + * out all files except those that end in ".jpg" and ".tif": | |
114 | + * | |
115 | + * ExampleFileFilter filter = new ExampleFileFilter(); | |
116 | + * filter.addExtension("jpg"); | |
117 | + * filter.addExtension("tif"); | |
118 | + * | |
119 | + * Note that the "." before the extension is not needed and will be ignored. | |
120 | + */ | |
121 | + public void addExtension(String extension) { | |
122 | + if(filters == null) { | |
123 | + filters = new Hashtable(6); | |
124 | + } | |
125 | + filters.put(extension.toLowerCase(), this); | |
126 | + fullDescription = null; | |
127 | + } | |
128 | + | |
129 | + | |
130 | + /** | |
131 | + * Returns the human readable description of this filter. For | |
132 | + * example: "JPEG and GIF Image Files (*.jpg, *.gif)" | |
133 | + * | |
134 | + */ | |
135 | + public String getDescription() { | |
136 | + if(fullDescription == null) { | |
137 | + if(description == null || isExtensionListInDescription()) { | |
138 | + fullDescription = description==null ? "(" : description + " ("; | |
139 | + // build the description from the extension list | |
140 | + Enumeration extensions = filters.keys(); | |
141 | + if(extensions != null) { | |
142 | + fullDescription += "." + (String) extensions.nextElement(); | |
143 | + while (extensions.hasMoreElements()) { | |
144 | + fullDescription += ", ." + (String) extensions.nextElement(); | |
145 | + } | |
146 | + } | |
147 | + fullDescription += ")"; | |
148 | + } else { | |
149 | + fullDescription = description; | |
150 | + } | |
151 | + } | |
152 | + return fullDescription; | |
153 | + } | |
154 | + | |
155 | + /** | |
156 | + * Sets the human readable description of this filter. For | |
157 | + * example: filter.setDescription("Gif and JPG Images"); | |
158 | + * | |
159 | + */ | |
160 | + public void setDescription(String description) { | |
161 | + this.description = description; | |
162 | + fullDescription = null; | |
163 | + } | |
164 | + | |
165 | + /** | |
166 | + * Determines whether the extension list (.jpg, .gif, etc) should | |
167 | + * show up in the human readable description. | |
168 | + * | |
169 | + * Only relevent if a description was provided in the constructor | |
170 | + * or using setDescription(); | |
171 | + * | |
172 | + */ | |
173 | + public void setExtensionListInDescription(boolean b) { | |
174 | + useExtensionsInDescription = b; | |
175 | + fullDescription = null; | |
176 | + } | |
177 | + | |
178 | + /** | |
179 | + * Returns whether the extension list (.jpg, .gif, etc) should | |
180 | + * show up in the human readable description. | |
181 | + * | |
182 | + * Only relevent if a description was provided in the constructor | |
183 | + * or using setDescription(); | |
184 | + * | |
185 | + */ | |
186 | + public boolean isExtensionListInDescription() { | |
187 | + return useExtensionsInDescription; | |
188 | + } | |
189 | +} | ... | ... |
... | ... | @@ -0,0 +1,189 @@ |
1 | +import java.io.File; | |
2 | +import java.util.Hashtable; | |
3 | +import java.util.Enumeration; | |
4 | +import javax.swing.*; | |
5 | +import javax.swing.filechooser.*; | |
6 | + | |
7 | +public class ExampleFileFilter extends FileFilter { | |
8 | + | |
9 | + private Hashtable filters = null; | |
10 | + private String description = null; | |
11 | + private String fullDescription = null; | |
12 | + private boolean useExtensionsInDescription = true; | |
13 | + | |
14 | + /** | |
15 | + * Creates a file filter. If no filters are added, then all | |
16 | + * files are accepted. | |
17 | + * | |
18 | + */ | |
19 | + public ExampleFileFilter() { | |
20 | + this.filters = new Hashtable(); | |
21 | + } | |
22 | + | |
23 | + /** | |
24 | + * Creates a file filter that accepts files with the given extension. | |
25 | + * Example: new ExampleFileFilter("jpg"); | |
26 | + * | |
27 | + */ | |
28 | + public ExampleFileFilter(String extension) { | |
29 | + this(extension,null); | |
30 | + } | |
31 | + | |
32 | + /** | |
33 | + * Creates a file filter that accepts the given file type. | |
34 | + * Example: new ExampleFileFilter("jpg", "JPEG Image Images"); | |
35 | + * | |
36 | + * Note that the "." before the extension is not needed. If | |
37 | + * provided, it will be ignored. | |
38 | + * | |
39 | + */ | |
40 | + public ExampleFileFilter(String extension, String description) { | |
41 | + this(); | |
42 | + if(extension!=null) addExtension(extension); | |
43 | + if(description!=null) setDescription(description); | |
44 | + } | |
45 | + | |
46 | + /** | |
47 | + * Creates a file filter from the given string array. | |
48 | + * Example: new ExampleFileFilter(String {"gif", "jpg"}); | |
49 | + * | |
50 | + * Note that the "." before the extension is not needed adn | |
51 | + * will be ignored. | |
52 | + * | |
53 | + */ | |
54 | + public ExampleFileFilter(String[] filters) { | |
55 | + this(filters, null); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * Creates a file filter from the given string array and description. | |
60 | + * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images"); | |
61 | + * | |
62 | + * Note that the "." before the extension is not needed and will be ignored. | |
63 | + * | |
64 | + */ | |
65 | + public ExampleFileFilter(String[] filters, String description) { | |
66 | + this(); | |
67 | + for (int i = 0; i < filters.length; i++) { | |
68 | + // add filters one by one | |
69 | + addExtension(filters[i]); | |
70 | + } | |
71 | + if(description!=null) setDescription(description); | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * Return true if this file should be shown in the directory pane, | |
76 | + * false if it shouldn't. | |
77 | + * | |
78 | + * Files that begin with "." are ignored. | |
79 | + * | |
80 | + */ | |
81 | + public boolean accept(File f) { | |
82 | + if(f != null) { | |
83 | + if(f.isDirectory()) { | |
84 | + return true; | |
85 | + } | |
86 | + String extension = getExtension(f); | |
87 | + if(extension != null && filters.get(getExtension(f)) != null) { | |
88 | + return true; | |
89 | + }; | |
90 | + } | |
91 | + return false; | |
92 | + } | |
93 | + | |
94 | + /** | |
95 | + * Return the extension portion of the file's name . | |
96 | + * | |
97 | + */ | |
98 | + public String getExtension(File f) { | |
99 | + if(f != null) { | |
100 | + String filename = f.getName(); | |
101 | + int i = filename.lastIndexOf('.'); | |
102 | + if(i>0 && i<filename.length()-1) { | |
103 | + return filename.substring(i+1).toLowerCase(); | |
104 | + }; | |
105 | + } | |
106 | + return null; | |
107 | + } | |
108 | + | |
109 | + /** | |
110 | + * Adds a filetype "dot" extension to filter against. | |
111 | + * | |
112 | + * For example: the following code will create a filter that filters | |
113 | + * out all files except those that end in ".jpg" and ".tif": | |
114 | + * | |
115 | + * ExampleFileFilter filter = new ExampleFileFilter(); | |
116 | + * filter.addExtension("jpg"); | |
117 | + * filter.addExtension("tif"); | |
118 | + * | |
119 | + * Note that the "." before the extension is not needed and will be ignored. | |
120 | + */ | |
121 | + public void addExtension(String extension) { | |
122 | + if(filters == null) { | |
123 | + filters = new Hashtable(5); | |
124 | + } | |
125 | + filters.put(extension.toLowerCase(), this); | |
126 | + fullDescription = null; | |
127 | + } | |
128 | + | |
129 | + | |
130 | + /** | |
131 | + * Returns the human readable description of this filter. For | |
132 | + * example: "JPEG and GIF Image Files (*.jpg, *.gif)" | |
133 | + * | |
134 | + */ | |
135 | + public String getDescription() { | |
136 | + if(fullDescription == null) { | |
137 | + if(description == null || isExtensionListInDescription()) { | |
138 | + fullDescription = description==null ? "(" : description + " ("; | |
139 | + // build the description from the extension list | |
140 | + Enumeration extensions = filters.keys(); | |
141 | + if(extensions != null) { | |
142 | + fullDescription += "." + (String) extensions.nextElement(); | |
143 | + while (extensions.hasMoreElements()) { | |
144 | + fullDescription += ", ." + (String) extensions.nextElement(); | |
145 | + } | |
146 | + } | |
147 | + fullDescription += ")"; | |
148 | + } else { | |
149 | + fullDescription = description; | |
150 | + } | |
151 | + } | |
152 | + return fullDescription; | |
153 | + } | |
154 | + | |
155 | + /** | |
156 | + * Sets the human readable description of this filter. For | |
157 | + * example: filter.setDescription("Gif and JPG Images"); | |
158 | + * | |
159 | + */ | |
160 | + public void setDescription(String description) { | |
161 | + this.description = description; | |
162 | + fullDescription = null; | |
163 | + } | |
164 | + | |
165 | + /** | |
166 | + * Determines whether the extension list (.jpg, .gif, etc) should | |
167 | + * show up in the human readable description. | |
168 | + * | |
169 | + * Only relevent if a description was provided in the constructor | |
170 | + * or using setDescription(); | |
171 | + * | |
172 | + */ | |
173 | + public void setExtensionListInDescription(boolean b) { | |
174 | + useExtensionsInDescription = b; | |
175 | + fullDescription = null; | |
176 | + } | |
177 | + | |
178 | + /** | |
179 | + * Returns whether the extension list (.jpg, .gif, etc) should | |
180 | + * show up in the human readable description. | |
181 | + * | |
182 | + * Only relevent if a description was provided in the constructor | |
183 | + * or using setDescription(); | |
184 | + * | |
185 | + */ | |
186 | + public boolean isExtensionListInDescription() { | |
187 | + return useExtensionsInDescription; | |
188 | + } | |
189 | +} | ... | ... |
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,587 @@ |
1 | +import java.awt.BorderLayout; | |
2 | +import java.awt.Container; | |
3 | +import java.awt.Font; | |
4 | +import java.awt.Frame; | |
5 | +import java.awt.GraphicsEnvironment; | |
6 | +import java.awt.event.ActionEvent; | |
7 | +import java.awt.event.ActionListener; | |
8 | +import java.awt.event.ItemEvent; | |
9 | +import java.awt.event.ItemListener; | |
10 | +import java.awt.event.WindowAdapter; | |
11 | +import java.awt.event.WindowEvent; | |
12 | +import java.util.ArrayList; | |
13 | +import java.util.Iterator; | |
14 | + | |
15 | +import javax.swing.Box; | |
16 | +import javax.swing.BoxLayout; | |
17 | +import javax.swing.ButtonGroup; | |
18 | +import javax.swing.JButton; | |
19 | +import javax.swing.JComboBox; | |
20 | +import javax.swing.JDialog; | |
21 | +import javax.swing.JFrame; | |
22 | +import javax.swing.JLabel; | |
23 | +import javax.swing.JList; | |
24 | +import javax.swing.JOptionPane; | |
25 | +import javax.swing.JPanel; | |
26 | +import javax.swing.JRadioButton; | |
27 | +import javax.swing.JScrollPane; | |
28 | +import javax.swing.JTextArea; | |
29 | +import javax.swing.border.EtchedBorder; | |
30 | +import javax.swing.border.TitledBorder; | |
31 | +import javax.swing.event.ChangeEvent; | |
32 | +import javax.swing.event.ChangeListener; | |
33 | +import javax.swing.event.ListSelectionEvent; | |
34 | +import javax.swing.event.ListSelectionListener; | |
35 | + | |
36 | + | |
37 | +/** | |
38 | + * This is a JDialog subclass that allows the user to select a font, in any | |
39 | + * style and size, from the list of available fonts on the system. The dialog is | |
40 | + * modal. Display it with show(); this method does not return until the user | |
41 | + * dismisses the dialog. When show() returns, call getSelectedFont() to obtain | |
42 | + * the user's selection. If the user clicked the dialog's "Cancel" button, | |
43 | + * getSelectedFont() will return null. | |
44 | + */ | |
45 | +public class FontChooser extends JDialog { | |
46 | + // These fields define the component properties | |
47 | + String family; // The name of the font family | |
48 | + | |
49 | + int style; // The font style | |
50 | + | |
51 | + int size; // The font size | |
52 | + | |
53 | + Font selectedFont; // The Font they correspond to | |
54 | + | |
55 | + // This is the list of all font families on the system | |
56 | + String[] fontFamilies; | |
57 | + | |
58 | + // The various Swing components used in the dialog | |
59 | + ItemChooser families, styles, sizes; | |
60 | + | |
61 | + JTextArea preview; | |
62 | + | |
63 | + JButton okay, cancel; | |
64 | + | |
65 | + // The names to appear in the "Style" menu | |
66 | + static final String[] styleNames = new String[] { "Plain", "Italic", | |
67 | + "Bold", "BoldItalic" }; | |
68 | + | |
69 | + // The style values that correspond to those names | |
70 | + static final Integer[] styleValues = new Integer[] { | |
71 | + new Integer(Font.PLAIN), new Integer(Font.ITALIC), | |
72 | + new Integer(Font.BOLD), new Integer(Font.BOLD + Font.ITALIC) }; | |
73 | + | |
74 | + // The size "names" to appear in the size menu | |
75 | + static final String[] sizeNames = new String[] { "8", "10", "12", "14", | |
76 | + "18", "20", "24", "28", "32", "40", "48", "56", "64", "72" }; | |
77 | + | |
78 | + // This is the default preview string displayed in the dialog box | |
79 | + static final String defaultPreviewString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" | |
80 | + + "abcdefghijklmnopqrstuvwxyz\n" | |
81 | + + "1234567890!@#$%^&*()_-=+[]{}<,.>\n" | |
82 | + + "The quick brown fox jumps over the lazy dog"; | |
83 | + | |
84 | + /** Create a font chooser dialog for the specified frame. */ | |
85 | + public FontChooser(Frame owner) { | |
86 | + super(owner, "Choose a Font"); // Set dialog frame and title | |
87 | + | |
88 | + // This dialog must be used as a modal dialog. In order to be used | |
89 | + // as a modeless dialog, it would have to fire a PropertyChangeEvent | |
90 | + // whenever the selected font changed, so that applications could be | |
91 | + // notified of the user's selections. | |
92 | + setModal(true); | |
93 | + | |
94 | + // Figure out what fonts are available on the system | |
95 | + GraphicsEnvironment env = GraphicsEnvironment | |
96 | + .getLocalGraphicsEnvironment(); | |
97 | + fontFamilies = env.getAvailableFontFamilyNames(); | |
98 | + | |
99 | + // Set initial values for the properties | |
100 | + family = fontFamilies[0]; | |
101 | + style = Font.PLAIN; | |
102 | + size = 18; | |
103 | + selectedFont = new Font(family, style, size); | |
104 | + | |
105 | + // Create ItemChooser objects that allow the user to select font | |
106 | + // family, style, and size. | |
107 | + families = new ItemChooser("Family", fontFamilies, null, 0, | |
108 | + ItemChooser.COMBOBOX); | |
109 | + styles = new ItemChooser("Style", styleNames, styleValues, 0, | |
110 | + ItemChooser.COMBOBOX); | |
111 | + sizes = new ItemChooser("Size", sizeNames, null, 4, | |
112 | + ItemChooser.COMBOBOX); | |
113 | + | |
114 | + // Now register event listeners to handle selections | |
115 | + families.addItemChooserListener(new ItemChooser.Listener() { | |
116 | + public void itemChosen(ItemChooser.Event e) { | |
117 | + setFontFamily((String) e.getSelectedValue()); | |
118 | + } | |
119 | + }); | |
120 | + styles.addItemChooserListener(new ItemChooser.Listener() { | |
121 | + public void itemChosen(ItemChooser.Event e) { | |
122 | + setFontStyle(((Integer) e.getSelectedValue()).intValue()); | |
123 | + } | |
124 | + }); | |
125 | + sizes.addItemChooserListener(new ItemChooser.Listener() { | |
126 | + public void itemChosen(ItemChooser.Event e) { | |
127 | + setFontSize(Integer.parseInt((String) e.getSelectedValue())); | |
128 | + } | |
129 | + }); | |
130 | + | |
131 | + // Create a component to preview the font. | |
132 | + preview = new JTextArea(defaultPreviewString, 5, 40); | |
133 | + preview.setFont(selectedFont); | |
134 | + | |
135 | + // Create buttons to dismiss the dialog, and set handlers on them | |
136 | + okay = new JButton("Okay"); | |
137 | + cancel = new JButton("Cancel"); | |
138 | + okay.addActionListener(new ActionListener() { | |
139 | + public void actionPerformed(ActionEvent e) { | |
140 | + hide(); | |
141 | + } | |
142 | + }); | |
143 | + cancel.addActionListener(new ActionListener() { | |
144 | + public void actionPerformed(ActionEvent e) { | |
145 | + selectedFont = null; | |
146 | + hide(); | |
147 | + } | |
148 | + }); | |
149 | + | |
150 | + // Put the ItemChoosers in a Box | |
151 | + Box choosersBox = Box.createHorizontalBox(); | |
152 | + choosersBox.add(Box.createHorizontalStrut(15)); | |
153 | + choosersBox.add(families); | |
154 | + choosersBox.add(Box.createHorizontalStrut(15)); | |
155 | + choosersBox.add(styles); | |
156 | + choosersBox.add(Box.createHorizontalStrut(15)); | |
157 | + choosersBox.add(sizes); | |
158 | + choosersBox.add(Box.createHorizontalStrut(15)); | |
159 | + choosersBox.add(Box.createGlue()); | |
160 | + | |
161 | + // Put the dismiss buttons in another box | |
162 | + Box buttonBox = Box.createHorizontalBox(); | |
163 | + buttonBox.add(Box.createGlue()); | |
164 | + buttonBox.add(okay); | |
165 | + buttonBox.add(Box.createGlue()); | |
166 | + buttonBox.add(cancel); | |
167 | + buttonBox.add(Box.createGlue()); | |
168 | + | |
169 | + // Put the choosers at the top, the buttons at the bottom, and | |
170 | + // the preview in the middle. | |
171 | + Container contentPane = getContentPane(); | |
172 | + contentPane.add(new JScrollPane(preview), BorderLayout.CENTER); | |
173 | + contentPane.add(choosersBox, BorderLayout.NORTH); | |
174 | + contentPane.add(buttonBox, BorderLayout.SOUTH); | |
175 | + | |
176 | + // Set the dialog size based on the component size. | |
177 | + pack(); | |
178 | + } | |
179 | + | |
180 | + /** | |
181 | + * Call this method after show() to obtain the user's selection. If the user | |
182 | + * used the "Cancel" button, this will return null | |
183 | + */ | |
184 | + public Font getSelectedFont() { | |
185 | + return selectedFont; | |
186 | + } | |
187 | + | |
188 | + // These are other property getter methods | |
189 | + public String getFontFamily() { | |
190 | + return family; | |
191 | + } | |
192 | + | |
193 | + public int getFontStyle() { | |
194 | + return style; | |
195 | + } | |
196 | + | |
197 | + public int getFontSize() { | |
198 | + return size; | |
199 | + } | |
200 | + | |
201 | + // The property setter methods are a little more complicated. | |
202 | + // Note that none of these setter methods update the corresponding | |
203 | + // ItemChooser components as they ought to. | |
204 | + public void setFontFamily(String name) { | |
205 | + family = name; | |
206 | + changeFont(); | |
207 | + } | |
208 | + | |
209 | + public void setFontStyle(int style) { | |
210 | + this.style = style; | |
211 | + changeFont(); | |
212 | + } | |
213 | + | |
214 | + public void setFontSize(int size) { | |
215 | + this.size = size; | |
216 | + changeFont(); | |
217 | + } | |
218 | + | |
219 | + public void setSelectedFont(Font font) { | |
220 | + selectedFont = font; | |
221 | + family = font.getFamily(); | |
222 | + style = font.getStyle(); | |
223 | + size = font.getSize(); | |
224 | + preview.setFont(font); | |
225 | + } | |
226 | + | |
227 | + // This method is called when the family, style, or size changes | |
228 | + protected void changeFont() { | |
229 | + selectedFont = new Font(family, style, size); | |
230 | + preview.setFont(selectedFont); | |
231 | + } | |
232 | + | |
233 | + // Override this inherited method to prevent anyone from making us modeless | |
234 | + public boolean isModal() { | |
235 | + return true; | |
236 | + } | |
237 | + | |
238 | + public static void main(String[] args) { | |
239 | + // Create some components and a FontChooser dialog | |
240 | + final JFrame frame = new JFrame("demo"); | |
241 | + final JButton button = new JButton("Push Me!"); | |
242 | + final FontChooser chooser = new FontChooser(frame); | |
243 | + | |
244 | + // Handle button clicks | |
245 | + button.addActionListener(new ActionListener() { | |
246 | + public void actionPerformed(ActionEvent e) { | |
247 | + // Pop up the dialog | |
248 | + chooser.show(); | |
249 | + // Get the user's selection | |
250 | + Font font = chooser.getSelectedFont(); | |
251 | + // If not cancelled, set the button font | |
252 | + if (font != null) | |
253 | + button.setFont(font); | |
254 | + } | |
255 | + }); | |
256 | + | |
257 | + // Display the demo | |
258 | + frame.getContentPane().add(button); | |
259 | + frame.setSize(200, 100); | |
260 | + frame.show(); | |
261 | + } | |
262 | +} | |
263 | +/** | |
264 | + * This class is a Swing component that presents a choice to the user. It allows | |
265 | + * the choice to be presented in a JList, in a JComboBox, or with a bordered | |
266 | + * group of JRadioButton components. Additionally, it displays the name of the | |
267 | + * choice with a JLabel. It allows an arbitrary value to be associated with each | |
268 | + * possible choice. Note that this component only allows one item to be selected | |
269 | + * at a time. Multiple selections are not supported. | |
270 | + */ | |
271 | + | |
272 | +class ItemChooser extends JPanel { | |
273 | + // These fields hold property values for this component | |
274 | + String name; // The overall name of the choice | |
275 | + | |
276 | + String[] labels; // The text for each choice option | |
277 | + | |
278 | + Object[] values; // Arbitrary values associated with each option | |
279 | + | |
280 | + int selection; // The selected choice | |
281 | + | |
282 | + int presentation; // How the choice is presented | |
283 | + | |
284 | + // These are the legal values for the presentation field | |
285 | + public static final int LIST = 1; | |
286 | + | |
287 | + public static final int COMBOBOX = 2; | |
288 | + | |
289 | + public static final int RADIOBUTTONS = 3; | |
290 | + | |
291 | + // These components are used for each of the 3 possible presentations | |
292 | + JList list; // One type of presentation | |
293 | + | |
294 | + JComboBox combobox; // Another type of presentation | |
295 | + | |
296 | + JRadioButton[] radiobuttons; // Yet another type | |
297 | + | |
298 | + // The list of objects that are interested in our state | |
299 | + ArrayList listeners = new ArrayList(); | |
300 | + | |
301 | + // The constructor method sets everything up | |
302 | + public ItemChooser(String name, String[] labels, Object[] values, | |
303 | + int defaultSelection, int presentation) { | |
304 | + // Copy the constructor arguments to instance fields | |
305 | + this.name = name; | |
306 | + this.labels = labels; | |
307 | + this.values = values; | |
308 | + this.selection = defaultSelection; | |
309 | + this.presentation = presentation; | |
310 | + | |
311 | + // If no values were supplied, use the labels | |
312 | + if (values == null) | |
313 | + this.values = labels; | |
314 | + | |
315 | + // Now create content and event handlers based on presentation type | |
316 | + switch (presentation) { | |
317 | + case LIST: | |
318 | + initList(); | |
319 | + break; | |
320 | + case COMBOBOX: | |
321 | + initComboBox(); | |
322 | + break; | |
323 | + case RADIOBUTTONS: | |
324 | + initRadioButtons(); | |
325 | + break; | |
326 | + } | |
327 | + } | |
328 | + | |
329 | + // Initialization for JList presentation | |
330 | + void initList() { | |
331 | + list = new JList(labels); // Create the list | |
332 | + list.setSelectedIndex(selection); // Set initial state | |
333 | + | |
334 | + // Handle state changes | |
335 | + list.addListSelectionListener(new ListSelectionListener() { | |
336 | + public void valueChanged(ListSelectionEvent e) { | |
337 | + ItemChooser.this.select(list.getSelectedIndex()); | |
338 | + } | |
339 | + }); | |
340 | + | |
341 | + // Lay out list and name label vertically | |
342 | + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // vertical | |
343 | + this.add(new JLabel(name)); // Display choice name | |
344 | + this.add(new JScrollPane(list)); // Add the JList | |
345 | + } | |
346 | + | |
347 | + // Initialization for JComboBox presentation | |
348 | + void initComboBox() { | |
349 | + combobox = new JComboBox(labels); // Create the combo box | |
350 | + combobox.setSelectedIndex(selection); // Set initial state | |
351 | + | |
352 | + // Handle changes to the state | |
353 | + combobox.addItemListener(new ItemListener() { | |
354 | + public void itemStateChanged(ItemEvent e) { | |
355 | + ItemChooser.this.select(combobox.getSelectedIndex()); | |
356 | + } | |
357 | + }); | |
358 | + | |
359 | + // Lay out combo box and name label horizontally | |
360 | + this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); | |
361 | + this.add(new JLabel(name)); | |
362 | + this.add(combobox); | |
363 | + } | |
364 | + | |
365 | + // Initialization for JRadioButton presentation | |
366 | + void initRadioButtons() { | |
367 | + // Create an array of mutually exclusive radio buttons | |
368 | + radiobuttons = new JRadioButton[labels.length]; // the array | |
369 | + ButtonGroup radioButtonGroup = new ButtonGroup(); // used for exclusion | |
370 | + ChangeListener listener = new ChangeListener() { // A shared listener | |
371 | + public void stateChanged(ChangeEvent e) { | |
372 | + JRadioButton b = (JRadioButton) e.getSource(); | |
373 | + if (b.isSelected()) { | |
374 | + // If we received this event because a button was | |
375 | + // selected, then loop through the list of buttons to | |
376 | + // figure out the index of the selected one. | |
377 | + for (int i = 0; i < radiobuttons.length; i++) { | |
378 | + if (radiobuttons[i] == b) { | |
379 | + ItemChooser.this.select(i); | |
380 | + return; | |
381 | + } | |
382 | + } | |
383 | + } | |
384 | + } | |
385 | + }; | |
386 | + | |
387 | + // Display the choice name in a border around the buttons | |
388 | + this.setBorder(new TitledBorder(new EtchedBorder(), name)); | |
389 | + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); | |
390 | + | |
391 | + // Create the buttons, add them to the button group, and specify | |
392 | + // the event listener for each one. | |
393 | + for (int i = 0; i < labels.length; i++) { | |
394 | + radiobuttons[i] = new JRadioButton(labels[i]); | |
395 | + if (i == selection) | |
396 | + radiobuttons[i].setSelected(true); | |
397 | + radiobuttons[i].addChangeListener(listener); | |
398 | + radioButtonGroup.add(radiobuttons[i]); | |
399 | + this.add(radiobuttons[i]); | |
400 | + } | |
401 | + } | |
402 | + | |
403 | + // These simple property accessor methods just return field values | |
404 | + // These are read-only properties. The values are set by the constructor | |
405 | + // and may not be changed. | |
406 | + public String getName() { | |
407 | + return name; | |
408 | + } | |
409 | + | |
410 | + public int getPresentation() { | |
411 | + return presentation; | |
412 | + } | |
413 | + | |
414 | + public String[] getLabels() { | |
415 | + return labels; | |
416 | + } | |
417 | + | |
418 | + public Object[] getValues() { | |
419 | + return values; | |
420 | + } | |
421 | + | |
422 | + /** Return the index of the selected item */ | |
423 | + public int getSelectedIndex() { | |
424 | + return selection; | |
425 | + } | |
426 | + | |
427 | + /** Return the object associated with the selected item */ | |
428 | + public Object getSelectedValue() { | |
429 | + return values[selection]; | |
430 | + } | |
431 | + | |
432 | + /** | |
433 | + * Set the selected item by specifying its index. Calling this method | |
434 | + * changes the on-screen display but does not generate events. | |
435 | + */ | |
436 | + public void setSelectedIndex(int selection) { | |
437 | + switch (presentation) { | |
438 | + case LIST: | |
439 | + list.setSelectedIndex(selection); | |
440 | + break; | |
441 | + case COMBOBOX: | |
442 | + combobox.setSelectedIndex(selection); | |
443 | + break; | |
444 | + case RADIOBUTTONS: | |
445 | + radiobuttons[selection].setSelected(true); | |
446 | + break; | |
447 | + } | |
448 | + this.selection = selection; | |
449 | + } | |
450 | + | |
451 | + /** | |
452 | + * This internal method is called when the selection changes. It stores the | |
453 | + * new selected index, and fires events to any registered listeners. The | |
454 | + * event listeners registered on the JList, JComboBox, or JRadioButtons all | |
455 | + * call this method. | |
456 | + */ | |
457 | + protected void select(int selection) { | |
458 | + this.selection = selection; // Store the new selected index | |
459 | + if (!listeners.isEmpty()) { // If there are any listeners registered | |
460 | + // Create an event object to describe the selection | |
461 | + ItemChooser.Event e = new ItemChooser.Event(this, selection, | |
462 | + values[selection]); | |
463 | + // Loop through the listeners using an Iterator | |
464 | + for (Iterator i = listeners.iterator(); i.hasNext();) { | |
465 | + ItemChooser.Listener l = (ItemChooser.Listener) i.next(); | |
466 | + l.itemChosen(e); // Notify each listener of the selection | |
467 | + } | |
468 | + } | |
469 | + } | |
470 | + | |
471 | + // These methods are for event listener registration and deregistration | |
472 | + public void addItemChooserListener(ItemChooser.Listener l) { | |
473 | + listeners.add(l); | |
474 | + } | |
475 | + | |
476 | + public void removeItemChooserListener(ItemChooser.Listener l) { | |
477 | + listeners.remove(l); | |
478 | + } | |
479 | + | |
480 | + /** | |
481 | + * This inner class defines the event type generated by ItemChooser objects | |
482 | + * The inner class name is Event, so the full name is ItemChooser.Event | |
483 | + */ | |
484 | + public static class Event extends java.util.EventObject { | |
485 | + int selectedIndex; // index of the selected item | |
486 | + | |
487 | + Object selectedValue; // the value associated with it | |
488 | + | |
489 | + public Event(ItemChooser source, int selectedIndex, Object selectedValue) { | |
490 | + super(source); | |
491 | + this.selectedIndex = selectedIndex; | |
492 | + this.selectedValue = selectedValue; | |
493 | + } | |
494 | + | |
495 | + public ItemChooser getItemChooser() { | |
496 | + return (ItemChooser) getSource(); | |
497 | + } | |
498 | + | |
499 | + public int getSelectedIndex() { | |
500 | + return selectedIndex; | |
501 | + } | |
502 | + | |
503 | + public Object getSelectedValue() { | |
504 | + return selectedValue; | |
505 | + } | |
506 | + } | |
507 | + | |
508 | + /** | |
509 | + * This inner interface must be implemented by any object that wants to be | |
510 | + * notified when the current selection in a ItemChooser component changes. | |
511 | + */ | |
512 | + public interface Listener extends java.util.EventListener { | |
513 | + public void itemChosen(ItemChooser.Event e); | |
514 | + } | |
515 | + | |
516 | + /** | |
517 | + * This inner class is a simple demonstration of the ItemChooser component | |
518 | + * It uses command-line arguments as ItemChooser labels and values. | |
519 | + */ | |
520 | + public static class Demo { | |
521 | + public static void main(String[] args) { | |
522 | + // Create a window, arrange to handle close requests | |
523 | + final JFrame frame = new JFrame("ItemChooser Demo"); | |
524 | + frame.addWindowListener(new WindowAdapter() { | |
525 | + public void windowClosing(WindowEvent e) { | |
526 | + System.exit(0); | |
527 | + } | |
528 | + }); | |
529 | + | |
530 | + // A "message line" to display results in | |
531 | + final JLabel msgline = new JLabel(" "); | |
532 | + | |
533 | + // Create a panel holding three ItemChooser components | |
534 | + JPanel chooserPanel = new JPanel(); | |
535 | + final ItemChooser c1 = new ItemChooser("Choice #1", args, null, 0, | |
536 | + ItemChooser.LIST); | |
537 | + final ItemChooser c2 = new ItemChooser("Choice #2", args, null, 0, | |
538 | + ItemChooser.COMBOBOX); | |
539 | + final ItemChooser c3 = new ItemChooser("Choice #3", args, null, 0, | |
540 | + ItemChooser.RADIOBUTTONS); | |
541 | + | |
542 | + // An event listener that displays changes on the message line | |
543 | + ItemChooser.Listener l = new ItemChooser.Listener() { | |
544 | + public void itemChosen(ItemChooser.Event e) { | |
545 | + msgline.setText(e.getItemChooser().getName() + ": " | |
546 | + + e.getSelectedIndex() + ": " | |
547 | + + e.getSelectedValue()); | |
548 | + } | |
549 | + }; | |
550 | + c1.addItemChooserListener(l); | |
551 | + c2.addItemChooserListener(l); | |
552 | + c3.addItemChooserListener(l); | |
553 | + | |
554 | + // Instead of tracking every change with a ItemChooser.Listener, | |
555 | + // applications can also just query the current state when | |
556 | + // they need it. Here's a button that does that. | |
557 | + JButton report = new JButton("Report"); | |
558 | + report.addActionListener(new ActionListener() { | |
559 | + public void actionPerformed(ActionEvent e) { | |
560 | + // Note the use of multi-line italic HTML text | |
561 | + // with the JOptionPane message dialog box. | |
562 | + String msg = "<html><i>" + c1.getName() + ": " | |
563 | + + c1.getSelectedValue() + "<br>" + c2.getName() | |
564 | + + ": " + c2.getSelectedValue() + "<br>" | |
565 | + + c3.getName() + ": " + c3.getSelectedValue() | |
566 | + + "</i>"; | |
567 | + JOptionPane.showMessageDialog(frame, msg); | |
568 | + } | |
569 | + }); | |
570 | + | |
571 | + // Add the 3 ItemChooser objects, and the Button to the panel | |
572 | + chooserPanel.add(c1); | |
573 | + chooserPanel.add(c2); | |
574 | + chooserPanel.add(c3); | |
575 | + chooserPanel.add(report); | |
576 | + | |
577 | + // Add the panel and the message line to the window | |
578 | + Container contentPane = frame.getContentPane(); | |
579 | + contentPane.add(chooserPanel, BorderLayout.CENTER); | |
580 | + contentPane.add(msgline, BorderLayout.SOUTH); | |
581 | + | |
582 | + // Set the window size and pop it up. | |
583 | + frame.pack(); | |
584 | + frame.show(); | |
585 | + } | |
586 | + } | |
587 | +} | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,36 @@ |
1 | +import javax.swing.JDialog; | |
2 | +import javax.swing.JLabel; | |
3 | + | |
4 | +/** | |
5 | + * A simple dialog that displays information about the program being used. | |
6 | + **/ | |
7 | + | |
8 | +public class HelpDialog extends JDialog | |
9 | +{ | |
10 | + /** | |
11 | + * Creates a new HelpDialog. Sets the title to "about". | |
12 | + */ | |
13 | + public HelpDialog() | |
14 | + { | |
15 | + setTitle("About"); | |
16 | + getContentPane().setLayout(null); | |
17 | + setSize(400, 300); | |
18 | + | |
19 | + JLabel lbl = new JLabel("JPaint "); | |
20 | + JLabel lbl2 = new JLabel("Created By: "); | |
21 | + JLabel lbl3 = new JLabel("LENTIEUL Romuald "); | |
22 | + JLabel lbl4 = new JLabel(" &"); | |
23 | + JLabel lbl5 = new JLabel("MAZIER Leo "); | |
24 | + lbl.setBounds(150,20,200,20); | |
25 | + lbl2.setBounds(20,40,200,20); | |
26 | + lbl3.setBounds(20,80,200,20); | |
27 | + lbl4.setBounds(20,120,200,20); | |
28 | + lbl5.setBounds(20,160,200,20); | |
29 | + getContentPane().add(lbl); | |
30 | + getContentPane().add(lbl2); | |
31 | + getContentPane().add(lbl3); | |
32 | + getContentPane().add(lbl4); | |
33 | + getContentPane().add(lbl5); | |
34 | + setVisible(true); | |
35 | + } | |
36 | +} | |
0 | 37 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +import javax.swing.JDialog; | |
2 | +import javax.swing.JLabel; | |
3 | + | |
4 | +/** | |
5 | + * A simple dialog that displays information about the program being used. | |
6 | + **/ | |
7 | + | |
8 | +public class HelpDialog extends JDialog | |
9 | +{ | |
10 | + /** | |
11 | + * Creates a new HelpDialog. Sets the title to "about". | |
12 | + */ | |
13 | + public HelpDialog() | |
14 | + { | |
15 | + setTitle("About"); | |
16 | + getContentPane().setLayout(null); | |
17 | + setSize(400, 300); | |
18 | + | |
19 | + JLabel lbl = new JLabel("JPaint "); | |
20 | + JLabel lbl2 = new JLabel("Created By: "); | |
21 | + JLabel lbl3 = new JLabel("LENTIEUL Romuald "); | |
22 | + JLabel lbl4 = new JLabel(" &"); | |
23 | + JLabel lbl5 = new JLabel("MAZIER Leo "); | |
24 | + lbl.setBounds(150,20,200,20); | |
25 | + lbl2.setBounds(20,40,200,20); | |
26 | + lbl3.setBounds(20,80,200,20); | |
27 | + lbl4.setBounds(20,120,200,20); | |
28 | + lbl5.setBounds(20,160,200,20); | |
29 | + getContentPane().add(lbl); | |
30 | + getContentPane().add(lbl2); | |
31 | + getContentPane().add(lbl3); | |
32 | + getContentPane().add(lbl4); | |
33 | + getContentPane().add(lbl5); | |
34 | + setVisible(true); | |
35 | + } | |
36 | +} | |
0 | 37 | \ No newline at end of file | ... | ... |
339 Bytes
252 Bytes
176 Bytes
188 Bytes
207 Bytes
181 Bytes
214 Bytes
181 Bytes
246 Bytes
414 Bytes
250 Bytes
182 Bytes
197 Bytes
291 Bytes
198 Bytes
202 Bytes
36.9 KB
91 Bytes
681 Bytes
87 Bytes
1.4 KB
76 Bytes
445 Bytes
89 Bytes
1.41 KB
110 Bytes
1.37 KB
95 Bytes
686 Bytes
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,37 @@ |
1 | +import javax.swing.JFileChooser; | |
2 | +import java.awt.Component; | |
3 | +import javax.swing.JDialog; | |
4 | +import javax.swing.JButton; | |
5 | + | |
6 | +/** | |
7 | + * JPaint File Chooser class opens up a file dialog where you can browse | |
8 | + * for the file you are looking for. You can either choose to save or open the | |
9 | + * file aftewards. | |
10 | + **/ | |
11 | +public class JPaintFileChooser extends JFileChooser | |
12 | +{ | |
13 | + private JDialog fileChooserDialog; | |
14 | + private ExampleFileFilter filter; | |
15 | + | |
16 | + /** | |
17 | + * Creates a default JPaintFileChooser Constructor and sets the extensions | |
18 | + * for files that are only allowed to be opened and saved. | |
19 | + **/ | |
20 | + public JPaintFileChooser(Component parent) | |
21 | + { | |
22 | + // creates a filter and adds the valid extensions on to it | |
23 | + filter = new ExampleFileFilter (); | |
24 | + filter.addExtension("jpg"); | |
25 | + filter.addExtension("gif"); | |
26 | + filter.addExtension("png"); | |
27 | + setFileFilter(filter); | |
28 | + // instantiates the file chooser dialog | |
29 | + fileChooserDialog = createDialog(parent); | |
30 | + } | |
31 | + | |
32 | + /** Returns the file chooser dialog. **/ | |
33 | + public JDialog getFileChooserDialog() | |
34 | + { | |
35 | + return fileChooserDialog; | |
36 | + } | |
37 | +} | |
0 | 38 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,38 @@ |
1 | +import javax.swing.JFileChooser; | |
2 | +import java.awt.Component; | |
3 | +import javax.swing.JDialog; | |
4 | +import javax.swing.JButton; | |
5 | + | |
6 | +/** | |
7 | + * JPaint File Chooser class opens up a file dialog where you can browse | |
8 | + * for the file you are looking for. You can either choose to save or open the | |
9 | + * file aftewards. | |
10 | + **/ | |
11 | +public class JPaintFileChooser extends JFileChooser | |
12 | +{ | |
13 | + private JDialog fileChooserDialog; | |
14 | + private ExampleFileFilter filter; | |
15 | + | |
16 | + /** | |
17 | + * Creates a default JPaintFileChooser Constructor and sets the extensions | |
18 | + * for files that are only allowed to be opened and saved. | |
19 | + **/ | |
20 | + public JPaintFileChooser(Component parent) | |
21 | + { | |
22 | + // creates a filter and adds the valid extensions on to it | |
23 | + filter = new ExampleFileFilter (); | |
24 | + filter.addExtension("jpg"); | |
25 | + filter.addExtension("gif"); | |
26 | + filter.addExtension("png"); | |
27 | + filter.addExtension("pdf"); | |
28 | + setFileFilter(filter); | |
29 | + // instantiates the file chooser dialog | |
30 | + fileChooserDialog = createDialog(parent); | |
31 | + } | |
32 | + | |
33 | + /** Returns the file chooser dialog. **/ | |
34 | + public JDialog getFileChooserDialog() | |
35 | + { | |
36 | + return fileChooserDialog; | |
37 | + } | |
38 | +} | |
0 | 39 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,112 @@ |
1 | +import java.awt.GridBagLayout; | |
2 | +import java.awt.GridBagConstraints; | |
3 | +import java.awt.Component; | |
4 | + | |
5 | +/** | |
6 | + * JPaintLayoutManager is an extension of GridBagLayout, this class is a helper | |
7 | + * class for the GridBagLayout. It makes using this layout easier. | |
8 | + **/ | |
9 | +public class JPaintLayoutManager extends GridBagLayout | |
10 | +{ | |
11 | + private GridBagConstraints gbc; | |
12 | + private Component comp; | |
13 | + | |
14 | + /** Creates a default JPaintLayoutManager. **/ | |
15 | + public JPaintLayoutManager() | |
16 | + { | |
17 | + gbc = new GridBagConstraints(); | |
18 | + } | |
19 | + | |
20 | + /** Sets the components necessary to get the layout started. **/ | |
21 | + public void setComponent(Component compo, int gridx, int gridy, int gridwidth, int gridHeight, | |
22 | + int ipadx, int ipady, int weightx, int weighty, int fill) | |
23 | + { | |
24 | + comp = compo; | |
25 | + gbc.gridx = gridx; | |
26 | + gbc.gridy = gridy; | |
27 | + gbc.gridwidth = gridwidth; | |
28 | + gbc.gridheight = gridHeight; | |
29 | + gbc.ipadx = ipadx; | |
30 | + gbc.ipady = ipady; | |
31 | + gbc.weightx = weightx; | |
32 | + gbc.weighty = weighty; | |
33 | + gbc.fill = fill; | |
34 | + setConstraints(compo, gbc); | |
35 | + } | |
36 | + | |
37 | + /** Sets the gridX. **/ | |
38 | + public void setgridx(int gridx) | |
39 | + { | |
40 | + gbc.gridx = gridx; | |
41 | + setConstraints(comp, gbc); | |
42 | + } | |
43 | + | |
44 | + /** Sets the gridY. **/ | |
45 | + public void setGridY(int gridy) | |
46 | + { | |
47 | + gbc.gridy = gridy; | |
48 | + setConstraints(comp, gbc); | |
49 | + } | |
50 | + | |
51 | + /** Sets the gridWidth. **/ | |
52 | + public void setGridWidth(int gridwidth) | |
53 | + { | |
54 | + gbc.gridwidth = gridwidth; | |
55 | + setConstraints(comp, gbc); | |
56 | + } | |
57 | + | |
58 | + /** Sets the gridHeight. **/ | |
59 | + public void setGridHeight(int gridheight) | |
60 | + { | |
61 | + gbc.gridheight = gridheight; | |
62 | + setConstraints(comp, gbc); | |
63 | + } | |
64 | + | |
65 | + /** Sets the ipadX. **/ | |
66 | + public void setIpadx(int ipadx) | |
67 | + { | |
68 | + gbc.ipadx = ipadx; | |
69 | + setConstraints(comp, gbc); | |
70 | + } | |
71 | + | |
72 | + /** Sets the ipadY. **/ | |
73 | + public void setIpady(int ipady) | |
74 | + { | |
75 | + gbc.ipady = ipady; | |
76 | + setConstraints(comp, gbc); | |
77 | + } | |
78 | + | |
79 | + /** Sets the xWeight. **/ | |
80 | + public void setWeightx(int weightx) | |
81 | + { | |
82 | + gbc.weightx = weightx; | |
83 | + setConstraints(comp, gbc); | |
84 | + } | |
85 | + | |
86 | + /** Sets the yWeight. **/ | |
87 | + public void setWeighty(int weighty) | |
88 | + { | |
89 | + gbc.weighty = weighty; | |
90 | + setConstraints(comp, gbc); | |
91 | + } | |
92 | + | |
93 | + /** Sets the anchor. **/ | |
94 | + public void setAnchor(int anchor) | |
95 | + { | |
96 | + gbc.anchor = anchor; | |
97 | + setConstraints(comp, gbc); | |
98 | + } | |
99 | + | |
100 | + /** Resets all the variables to its default settings. **/ | |
101 | + public void reset() | |
102 | + { | |
103 | + gbc.gridx = 0; | |
104 | + gbc.gridy = 0; | |
105 | + gbc.gridwidth = 0; | |
106 | + gbc.gridheight = 0; | |
107 | + gbc.ipadx = 0; | |
108 | + gbc.ipady = 0; | |
109 | + gbc.weightx = 0; | |
110 | + gbc.weighty= 0; | |
111 | + } | |
112 | +} | |
0 | 113 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,22 @@ |
1 | +/** | |
2 | + * This is the main class which controls the splash screen loading and starts | |
3 | + * the program. | |
4 | + **/ | |
5 | +public class Main | |
6 | +{ | |
7 | + /** Main Class **/ | |
8 | + public static void main(String [] args) | |
9 | + { | |
10 | + ControlClass control; | |
11 | + //Freeware fware = new Freeware(); | |
12 | + String status = "HELLO !"; | |
13 | + | |
14 | + SplashScreen splash = new SplashScreen("Images\\JPaintSplash.jpg"); | |
15 | + | |
16 | + control = new ControlClass(); | |
17 | + | |
18 | + try{Thread.sleep(1000);}catch(InterruptedException ie){} | |
19 | + splash.closeWindow(); | |
20 | + | |
21 | + } | |
22 | +} | |
0 | 23 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,22 @@ |
1 | +/** | |
2 | + * This is the main class which controls the splash screen loading and starts | |
3 | + * the program. | |
4 | + **/ | |
5 | +public class Main | |
6 | +{ | |
7 | + /** Main Class **/ | |
8 | + public static void main(String [] args) | |
9 | + { | |
10 | + ControlClass control; | |
11 | + //Freeware fware = new Freeware(); | |
12 | + String status = "open"; | |
13 | + | |
14 | + SplashScreen splash = new SplashScreen("Images\\JPaintSplash.jpg"); | |
15 | + | |
16 | + control = new ControlClass(); | |
17 | + | |
18 | + try{Thread.sleep(1000);}catch(InterruptedException ie){} | |
19 | + splash.closeWindow(); | |
20 | + | |
21 | + } | |
22 | +} | |
0 | 23 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,186 @@ |
1 | +import javax.swing.JMenuBar; | |
2 | +import javax.swing.JMenu; | |
3 | +import javax.swing.JMenuItem; | |
4 | +import java.awt.event.ActionListener; | |
5 | +import java.awt.event.KeyEvent; | |
6 | +import java.awt.event.ActionEvent; | |
7 | +import javax.swing.KeyStroke; | |
8 | +import java.util.Map; | |
9 | +import java.util.HashMap; | |
10 | + | |
11 | +/** | |
12 | + * The MainMenu class will serve as the menu bar for JPaint. | |
13 | + * It will contain all basic functions, i.e. new file, etc. There are keyboard | |
14 | + * shorts cuts to speed up the process. | |
15 | + **/ | |
16 | +public class MainMenu extends JMenuBar | |
17 | +{ | |
18 | + private Map JMenuItemsMap; | |
19 | + private JMenu menuFile, menuEdit, menuView, menuHelp; | |
20 | + private JMenuItem fileNew, fileOpen, fileClose, fileSave, fileSaveAs, | |
21 | + filePrint, fileExit, editCut, editCopy, editPaste, editDelete, viewToolBox, | |
22 | + viewColorBox, imageClear, imageRotate, helpAbout; | |
23 | + | |
24 | + /** | |
25 | + * Creates a default constructor for the MainMenu. Instantiates the menus | |
26 | + * and the menu items and the keys. Creates a maps to make accessors easier. | |
27 | + **/ | |
28 | + public MainMenu() | |
29 | + { | |
30 | + // instantiations for menus, menuitems and hotkeys | |
31 | + initJMenus(); | |
32 | + initJMenuItems(); | |
33 | + initHotKeys(); | |
34 | + | |
35 | + // creates a hashmap and then adds the items to the hashmap | |
36 | + JMenuItemsMap = new HashMap(19); | |
37 | + addJMenuItemsToMap(); | |
38 | + | |
39 | + // adds everything onto the screen | |
40 | + addAll(); | |
41 | + } | |
42 | + | |
43 | + /** Adds the menubar onto the screen. **/ | |
44 | + public void addAll() | |
45 | + { | |
46 | + add(menuFile); | |
47 | + menuFile.add(fileNew); | |
48 | + menuFile.add(fileOpen); | |
49 | + menuFile.add(fileClose); | |
50 | + menuFile.addSeparator(); | |
51 | + menuFile.add(fileSave); | |
52 | + menuFile.add(fileSaveAs); | |
53 | + menuFile.addSeparator(); | |
54 | + menuFile.add(filePrint); | |
55 | + menuFile.addSeparator(); | |
56 | + menuFile.add(fileExit); | |
57 | + | |
58 | + add(menuEdit); | |
59 | + | |
60 | + menuEdit.add(editCut); | |
61 | + menuEdit.add(editCopy); | |
62 | + menuEdit.add(editPaste); | |
63 | + menuEdit.addSeparator(); | |
64 | + menuEdit.add(editDelete); | |
65 | + menuEdit.addSeparator(); | |
66 | + menuEdit.add(imageRotate); | |
67 | + menuEdit.addSeparator(); | |
68 | + menuEdit.add(imageClear); | |
69 | + | |
70 | + add(menuView); | |
71 | + | |
72 | + menuView.add(viewToolBox); | |
73 | + menuView.add(viewColorBox); | |
74 | + | |
75 | + add(menuHelp); | |
76 | + | |
77 | + menuHelp.add(helpAbout); | |
78 | + } | |
79 | + | |
80 | + /** Adds actionlisteners to each of the menu items. **/ | |
81 | + public void addActionListener(ActionListener listener) | |
82 | + { | |
83 | + fileNew.addActionListener(listener); | |
84 | + fileOpen.addActionListener(listener); | |
85 | + fileClose.addActionListener(listener); | |
86 | + fileSave.addActionListener(listener); | |
87 | + fileSaveAs.addActionListener(listener); | |
88 | + filePrint.addActionListener(listener); | |
89 | + fileExit.addActionListener(listener); | |
90 | + | |
91 | + editCut.addActionListener(listener); | |
92 | + editCopy.addActionListener(listener); | |
93 | + editPaste.addActionListener(listener); | |
94 | + editDelete.addActionListener(listener); | |
95 | + | |
96 | + viewToolBox.addActionListener(listener); | |
97 | + viewColorBox.addActionListener(listener); | |
98 | + | |
99 | + imageRotate.addActionListener(listener); | |
100 | + imageClear.addActionListener(listener); | |
101 | + | |
102 | + helpAbout.addActionListener(listener); | |
103 | + } | |
104 | + | |
105 | + /** This method instantiates all the menus. **/ | |
106 | + public void initJMenus() | |
107 | + { | |
108 | + menuFile = new JMenu("File"); | |
109 | + menuEdit = new JMenu("Edit"); | |
110 | + menuView = new JMenu("View"); | |
111 | + menuHelp = new JMenu("About"); | |
112 | + } | |
113 | + | |
114 | + /** This method instantiates all the jmenu items. **/ | |
115 | + public void initJMenuItems() | |
116 | + { | |
117 | + fileNew = new JMenuItem("New"); | |
118 | + fileOpen = new JMenuItem("Open"); | |
119 | + fileClose = new JMenuItem("Close"); | |
120 | + fileSave = new JMenuItem("Save"); | |
121 | + fileSaveAs = new JMenuItem("Save As"); | |
122 | + filePrint = new JMenuItem("Print"); | |
123 | + fileExit = new JMenuItem("Exit"); | |
124 | + | |
125 | + editCut = new JMenuItem("Cut"); | |
126 | + editCopy = new JMenuItem("Copy"); | |
127 | + editPaste = new JMenuItem("Paste"); | |
128 | + editDelete = new JMenuItem("Delete"); | |
129 | + | |
130 | + viewToolBox = new JMenuItem("ToolBox"); | |
131 | + viewColorBox = new JMenuItem("ColorBox"); | |
132 | + | |
133 | + imageClear = new JMenuItem("Clear Image"); | |
134 | + imageRotate = new JMenuItem("Rotate"); | |
135 | + | |
136 | + helpAbout = new JMenuItem("About JPaint"); | |
137 | + } | |
138 | + | |
139 | + /** This method sets all the hot keys for the menu items. **/ | |
140 | + public void initHotKeys() | |
141 | + { | |
142 | + fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); | |
143 | + fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); | |
144 | + fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); | |
145 | + fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F12, ActionEvent.CTRL_MASK)); | |
146 | + filePrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK)); | |
147 | + fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); | |
148 | + fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK)); | |
149 | + viewToolBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK)); | |
150 | + viewColorBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK)); | |
151 | + editCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); | |
152 | + editCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); | |
153 | + editPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); | |
154 | + editDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK)); | |
155 | + imageRotate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK)); | |
156 | + imageClear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK)); | |
157 | + helpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK)); | |
158 | + } | |
159 | + | |
160 | + /** This method adds the menu items to the map. **/ | |
161 | + public void addJMenuItemsToMap() | |
162 | + { | |
163 | + JMenuItemsMap.put("fileNew", fileNew); | |
164 | + JMenuItemsMap.put("fileOpen", fileOpen); | |
165 | + JMenuItemsMap.put("fileClose", fileClose); | |
166 | + JMenuItemsMap.put("fileSave", fileSave); | |
167 | + JMenuItemsMap.put("fileSaveAs", fileSaveAs); | |
168 | + JMenuItemsMap.put("filePrint", filePrint); | |
169 | + JMenuItemsMap.put("fileExit", fileExit); | |
170 | + JMenuItemsMap.put("editCut", editCut); | |
171 | + JMenuItemsMap.put("editCopy", editCopy); | |
172 | + JMenuItemsMap.put("editPaste", editPaste); | |
173 | + JMenuItemsMap.put("editDelete", editDelete); | |
174 | + JMenuItemsMap.put("viewToolBox", viewToolBox); | |
175 | + JMenuItemsMap.put("viewColorBox", viewColorBox); | |
176 | + JMenuItemsMap.put("imageRotate", imageRotate); | |
177 | + JMenuItemsMap.put("imageClear", imageClear); | |
178 | + JMenuItemsMap.put("helpAbout", helpAbout); | |
179 | + } | |
180 | + | |
181 | + /** Returns the JMenuItem. **/ | |
182 | + public JMenuItem getJMenuItem(String str) | |
183 | + { | |
184 | + return (JMenuItem)JMenuItemsMap.get(str); | |
185 | + } | |
186 | +} | |
0 | 187 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,186 @@ |
1 | +import javax.swing.JMenuBar; | |
2 | +import javax.swing.JMenu; | |
3 | +import javax.swing.JMenuItem; | |
4 | +import java.awt.event.ActionListener; | |
5 | +import java.awt.event.KeyEvent; | |
6 | +import java.awt.event.ActionEvent; | |
7 | +import javax.swing.KeyStroke; | |
8 | +import java.util.Map; | |
9 | +import java.util.HashMap; | |
10 | + | |
11 | +/** | |
12 | + * The MainMenu class will serve as the menu bar for JPaint. | |
13 | + * It will contain all basic functions, i.e. new file, etc. There are keyboard | |
14 | + * shorts cuts to speed up the process. | |
15 | + **/ | |
16 | +public class MainMenu extends JMenuBar | |
17 | +{ | |
18 | + private Map JMenuItemsMap; | |
19 | + private JMenu menuFile, menuEdit, menuView, menuHelp; | |
20 | + private JMenuItem fileNew, fileOpen, fileClose, fileSave, fileSaveAs, | |
21 | + filePrint, fileExit, editCut, editCopy, editPaste, editDelete, viewToolBox, | |
22 | + viewColorBox, imageClear, imageRotate, helpAbout; | |
23 | + | |
24 | + /** | |
25 | + * Creates a default constructor for the MainMenu. Instantiates the menus | |
26 | + * and the menu items and the keys. Creates a maps to make accessors easier. | |
27 | + **/ | |
28 | + public MainMenu() | |
29 | + { | |
30 | + // instantiations for menus, menuitems and hotkeys | |
31 | + initJMenus(); | |
32 | + initJMenuItems(); | |
33 | + initHotKeys(); | |
34 | + | |
35 | + // creates a hashmap and then adds the items to the hashmap | |
36 | + JMenuItemsMap = new HashMap(19); | |
37 | + addJMenuItemsToMap(); | |
38 | + | |
39 | + // adds everything onto the screen | |
40 | + addAll(); | |
41 | + } | |
42 | + | |
43 | + /** Adds the menubar onto the screen. **/ | |
44 | + public void addAll() | |
45 | + { | |
46 | + add(menuFile); | |
47 | + menuFile.add(fileNew); | |
48 | + menuFile.add(fileOpen); | |
49 | + menuFile.add(fileClose); | |
50 | + menuFile.addSeparator(); | |
51 | + menuFile.add(fileSave); | |
52 | + menuFile.add(fileSaveAs); | |
53 | + menuFile.addSeparator(); | |
54 | + menuFile.add(filePrint); | |
55 | + menuFile.addSeparator(); | |
56 | + menuFile.add(fileExit); | |
57 | + | |
58 | + add(menuEdit); | |
59 | + | |
60 | + menuEdit.add(editCut); | |
61 | + menuEdit.add(editCopy); | |
62 | + menuEdit.add(editPaste); | |
63 | + menuEdit.addSeparator(); | |
64 | + menuEdit.add(editDelete); | |
65 | + menuEdit.addSeparator(); | |
66 | + menuEdit.add(imageRotate); | |
67 | + menuEdit.addSeparator(); | |
68 | + menuEdit.add(imageClear); | |
69 | + | |
70 | + add(menuView); | |
71 | + | |
72 | + menuView.add(viewToolBox); | |
73 | + menuView.add(viewColorBox); | |
74 | + | |
75 | + add(menuHelp); | |
76 | + | |
77 | + menuHelp.add(helpAbout); | |
78 | + } | |
79 | + | |
80 | + /** Adds actionlisteners to each of the menu items. **/ | |
81 | + public void addActionListener(ActionListener listener) | |
82 | + { | |
83 | + fileNew.addActionListener(listener); | |
84 | + fileOpen.addActionListener(listener); | |
85 | + fileClose.addActionListener(listener); | |
86 | + fileSave.addActionListener(listener); | |
87 | + fileSaveAs.addActionListener(listener); | |
88 | + filePrint.addActionListener(listener); | |
89 | + fileExit.addActionListener(listener); | |
90 | + | |
91 | + editCut.addActionListener(listener); | |
92 | + editCopy.addActionListener(listener); | |
93 | + editPaste.addActionListener(listener); | |
94 | + editDelete.addActionListener(listener); | |
95 | + | |
96 | + viewToolBox.addActionListener(listener); | |
97 | + viewColorBox.addActionListener(listener); | |
98 | + | |
99 | + imageRotate.addActionListener(listener); | |
100 | + imageClear.addActionListener(listener); | |
101 | + | |
102 | + helpAbout.addActionListener(listener); | |
103 | + } | |
104 | + | |
105 | + /** This method instantiates all the menus. **/ | |
106 | + public void initJMenus() | |
107 | + { | |
108 | + menuFile = new JMenu("File"); | |
109 | + menuEdit = new JMenu("Edit"); | |
110 | + menuView = new JMenu("View"); | |
111 | + menuHelp = new JMenu("About"); | |
112 | + } | |
113 | + | |
114 | + /** This method instantiates all the jmenu items. **/ | |
115 | + public void initJMenuItems() | |
116 | + { | |
117 | + fileNew = new JMenuItem("New"); | |
118 | + fileOpen = new JMenuItem("Open"); | |
119 | + fileClose = new JMenuItem("Close"); | |
120 | + fileSave = new JMenuItem("Save"); | |
121 | + fileSaveAs = new JMenuItem("Save As"); | |
122 | + filePrint = new JMenuItem("Print"); | |
123 | + fileExit = new JMenuItem("Exit"); | |
124 | + | |
125 | + editCut = new JMenuItem("Cut"); | |
126 | + editCopy = new JMenuItem("Copy"); | |
127 | + editPaste = new JMenuItem("Paste"); | |
128 | + editDelete = new JMenuItem("Delete"); | |
129 | + | |
130 | + viewToolBox = new JMenuItem("ToolBox"); | |
131 | + viewColorBox = new JMenuItem("ColorBox"); | |
132 | + | |
133 | + imageClear = new JMenuItem("Clear Image"); | |
134 | + imageRotate = new JMenuItem("Rotate"); | |
135 | + | |
136 | + helpAbout = new JMenuItem("About JPaint"); | |
137 | + } | |
138 | + | |
139 | + /** This method sets all the hot keys for the menu items. **/ | |
140 | + public void initHotKeys() | |
141 | + { | |
142 | + fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); | |
143 | + fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); | |
144 | + fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); | |
145 | + fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F12, ActionEvent.CTRL_MASK)); | |
146 | + filePrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK)); | |
147 | + fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); | |
148 | + fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK)); | |
149 | + viewToolBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK)); | |
150 | + viewColorBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK)); | |
151 | + editCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); | |
152 | + editCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); | |
153 | + editPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); | |
154 | + editDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK)); | |
155 | + imageRotate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK)); | |
156 | + imageClear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK)); | |
157 | + helpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK)); | |
158 | + } | |
159 | + | |
160 | + /** This method adds the menu items to the map. **/ | |
161 | + public void addJMenuItemsToMap() | |
162 | + { | |
163 | + JMenuItemsMap.put("fileNew", fileNew); | |
164 | + JMenuItemsMap.put("fileOpen", fileOpen); | |
165 | + JMenuItemsMap.put("fileClose", fileClose); | |
166 | + JMenuItemsMap.put("fileSave", fileSave); | |
167 | + JMenuItemsMap.put("fileSaveAs", fileSaveAs); | |
168 | + JMenuItemsMap.put("filePrint", filePrint); | |
169 | + JMenuItemsMap.put("fileExit", fileExit); | |
170 | + JMenuItemsMap.put("editCut", editCut); | |
171 | + JMenuItemsMap.put("editCopy", editCopy); | |
172 | + JMenuItemsMap.put("editPaste", editPaste); | |
173 | + JMenuItemsMap.put("editDelete", editDelete); | |
174 | + JMenuItemsMap.put("viewToolBox", viewToolBox); | |
175 | + JMenuItemsMap.put("viewColorBox", viewColorBox); | |
176 | + JMenuItemsMap.put("imageRotate", imageRotate); | |
177 | + JMenuItemsMap.put("imageClear", imageClear); | |
178 | + JMenuItemsMap.put("helpAbout", helpAbout); | |
179 | + } | |
180 | + | |
181 | + /** Returns the JMenuItem. **/ | |
182 | + public JMenuItem getJMenuItem(String str) | |
183 | + { | |
184 | + return (JMenuItem)JMenuItemsMap.get(str); | |
185 | + } | |
186 | +} | |
0 | 187 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | + | |
2 | +JFLAGS = -g | |
3 | +JC = javac | |
4 | +RM = rm -f | |
5 | +.SUFFIXES: .java .class | |
6 | +.java.class: | |
7 | + $(JC) $(JFLAGS) $*.java | |
8 | + | |
9 | +CLASSES = \ | |
10 | + *.java \ | |
11 | + Main.java | |
12 | + | |
13 | +default: classes | |
14 | + | |
15 | +classes: $(CLASSES:.java=.class) | |
16 | + | |
17 | +clean: | |
18 | + $(RM) *.class | ... | ... |
... | ... | @@ -0,0 +1,10 @@ |
1 | +JPaint | |
2 | +====== | |
3 | + | |
4 | +JPaint is an art development program created by Java. It has the standard functions of MSPaint with a few variations and some unique features. | |
5 | +Created: June 15/2005 | |
6 | +Tested on => Windows XP (Java 1.4.2) | |
7 | + | |
8 | +Disclaimer: This code might not work anymore as it was created several years ago. | |
9 | + | |
10 | +[![](http://i44.tinypic.com/lcd4j.png)](http://i44.tinypic.com/lcd4j.png) | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,75 @@ |
1 | +import java.awt.Color; | |
2 | +/** | |
3 | + * RandomPoint class is similar to a point with a color. It stores points, | |
4 | + * it is used for the random drawing method. | |
5 | + **/ | |
6 | + public class RandomPoint | |
7 | +{ | |
8 | + private Color c; | |
9 | + private int x,y,width,height; | |
10 | + | |
11 | + /** Creates a default RandomPoint. **/ | |
12 | + public RandomPoint(){} | |
13 | + | |
14 | + /** Creates a RandomPoint and takes in the screen sizes width and height. **/ | |
15 | + public RandomPoint(int width, int height) | |
16 | + { | |
17 | + // takes in variables and then randomly generates a starting x | |
18 | + // and y location | |
19 | + this.width = width; | |
20 | + this.height = height; | |
21 | + x = (int)(Math.random()*width); | |
22 | + y = (int)(Math.random()*height); | |
23 | + } | |
24 | + | |
25 | + /** Figures out the next x location. **/ | |
26 | + public int nextX() | |
27 | + { | |
28 | + // checks if its a negative of a positive number then generates a random | |
29 | + // point up to 50 places away, if its out of bounds then it returns to | |
30 | + // the bounds | |
31 | + int sign = (int)(Math.random()*2); | |
32 | + if(sign == 0) | |
33 | + x = x + (int)(Math.random()*50+1); | |
34 | + else | |
35 | + x = x - (int)(Math.random()*50+1); | |
36 | + | |
37 | + if(x < 0) | |
38 | + x = 0; | |
39 | + if(x > width) | |
40 | + x = width; | |
41 | + | |
42 | + return x; | |
43 | + } | |
44 | + | |
45 | + /** Figures out the next y location. **/ | |
46 | + public int nextY() | |
47 | + { | |
48 | + // checks if its a negative of a positive number then generates a random | |
49 | + // point up to 50 places away, if its out of bounds then it returns to | |
50 | + // the bounds | |
51 | + int sign = (int)(Math.random()*2); | |
52 | + if(sign == 0) | |
53 | + y = y + (int)(Math.random()*200+1); | |
54 | + else | |
55 | + y = y - (int)(Math.random()*200+1); | |
56 | + | |
57 | + if(y < 0) | |
58 | + y = 0; | |
59 | + if(y > height) | |
60 | + y = height; | |
61 | + | |
62 | + return y; | |
63 | + } | |
64 | + | |
65 | + /** This method returns a random color. **/ | |
66 | + public Color getColor() | |
67 | + { | |
68 | + return new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); | |
69 | + } | |
70 | + | |
71 | + /** This method returns the x value. **/ | |
72 | + public int getX(){return x;} | |
73 | + /** This method returns the y value. **/ | |
74 | + public int getY(){return y;} | |
75 | +} | |
0 | 76 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,57 @@ |
1 | +import javax.swing.JLabel; | |
2 | +import javax.swing.ImageIcon; | |
3 | +import javax.swing.JPanel; | |
4 | +import java.awt.Dimension; | |
5 | +import java.awt.BorderLayout; | |
6 | +import java.awt.Window; | |
7 | +import java.awt.Frame; | |
8 | +import java.awt.Color; | |
9 | +import java.awt.Toolkit; | |
10 | + | |
11 | +/** | |
12 | + * Splash Screen loads up for this program. Before the program runs a screen | |
13 | + * with an image pops up for a second, then it loads the rest of the program. | |
14 | + **/ | |
15 | +public class SplashScreen extends Window | |
16 | +{ | |
17 | + JLabel lblStatus = new JLabel("LOADING JPaint is opening..."); | |
18 | + | |
19 | + /** | |
20 | + * Creates a default constructor. Creates a new frame and adds the | |
21 | + * background image onto it. | |
22 | + **/ | |
23 | + public SplashScreen(String strImageFileName) | |
24 | + { | |
25 | + super(new Frame()); | |
26 | + | |
27 | + ImageIcon imageScreen = new ImageIcon(strImageFileName); | |
28 | + JLabel lblImage = new JLabel(imageScreen); | |
29 | + lblStatus.setBackground(Color.black); | |
30 | + lblStatus.setForeground(Color.white); | |
31 | + JPanel pnlIm = new JPanel(new BorderLayout()); | |
32 | + pnlIm.add(lblImage, BorderLayout.CENTER); | |
33 | + pnlIm.add(lblStatus, BorderLayout.SOUTH); | |
34 | + pnlIm.setBackground(Color.black); | |
35 | + add(pnlIm); | |
36 | + pack(); | |
37 | + | |
38 | + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
39 | + Dimension windowSize = getSize(); | |
40 | + this.setBounds((screenSize.width - windowSize.width)/2, (screenSize.height - windowSize.height)/2, windowSize.width, windowSize.height); | |
41 | + | |
42 | + setVisible(true); | |
43 | + } | |
44 | + | |
45 | + /** Updates the status bar with the new string. **/ | |
46 | + public void updateStatus(String temp) | |
47 | + { | |
48 | + lblStatus.setText(temp); | |
49 | + setVisible(true); | |
50 | + } | |
51 | + | |
52 | + /** Closes the splash screen. **/ | |
53 | + public void closeWindow() | |
54 | + { | |
55 | + dispose(); | |
56 | + } | |
57 | +} | |
0 | 58 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,57 @@ |
1 | +import javax.swing.JLabel; | |
2 | +import javax.swing.ImageIcon; | |
3 | +import javax.swing.JPanel; | |
4 | +import java.awt.Dimension; | |
5 | +import java.awt.BorderLayout; | |
6 | +import java.awt.Window; | |
7 | +import java.awt.Frame; | |
8 | +import java.awt.Color; | |
9 | +import java.awt.Toolkit; | |
10 | + | |
11 | +/** | |
12 | + * Splash Screen loads up for this program. Before the program runs a screen | |
13 | + * with an image pops up for a second, then it loads the rest of the program. | |
14 | + **/ | |
15 | +public class SplashScreen extends Window | |
16 | +{ | |
17 | + JLabel lblStatus = new JLabel("LOADING JPaint is opening..."); | |
18 | + | |
19 | + /** | |
20 | + * Creates a default constructor. Creates a new frame and adds the | |
21 | + * background image onto it. | |
22 | + **/ | |
23 | + public SplashScreen(String strImageFileName) | |
24 | + { | |
25 | + super(new Frame()); | |
26 | + | |
27 | + ImageIcon imageScreen = new ImageIcon(strImageFileName); | |
28 | + JLabel lblImage = new JLabel(imageScreen); | |
29 | + lblStatus.setBackground(Color.black); | |
30 | + lblStatus.setForeground(Color.black); | |
31 | + JPanel pnlIm = new JPanel(new BorderLayout()); | |
32 | + pnlIm.add(lblImage, BorderLayout.CENTER); | |
33 | + pnlIm.add(lblStatus, BorderLayout.SOUTH); | |
34 | + pnlIm.setBackground(Color.black); | |
35 | + add(pnlIm); | |
36 | + pack(); | |
37 | + | |
38 | + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
39 | + Dimension windowSize = getSize(); | |
40 | + this.setBounds((screenSize.width - windowSize.width)/2, (screenSize.height - windowSize.height)/2, windowSize.width, windowSize.height); | |
41 | + | |
42 | + setVisible(true); | |
43 | + } | |
44 | + | |
45 | + /** Updates the status bar with the new string. **/ | |
46 | + public void updateStatus(String temp) | |
47 | + { | |
48 | + lblStatus.setText(temp); | |
49 | + setVisible(true); | |
50 | + } | |
51 | + | |
52 | + /** Closes the splash screen. **/ | |
53 | + public void closeWindow() | |
54 | + { | |
55 | + dispose(); | |
56 | + } | |
57 | +} | |
0 | 58 | \ No newline at end of file | ... | ... |
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
... | ... | @@ -0,0 +1,269 @@ |
1 | +import javax.swing.JButton; | |
2 | +import javax.swing.JPanel; | |
3 | +import javax.swing.JTextField; | |
4 | +import javax.swing.ImageIcon; | |
5 | +import java.awt.event.ActionListener; | |
6 | +import java.awt.event.ActionEvent; | |
7 | + | |
8 | +/** | |
9 | + * SubToolBox contains special features for certain differnet tools. Such as | |
10 | + * setting the line width, setting the radius of the spray, choosing a brush, | |
11 | + * selecting whether you want to draw a normal, outline or filled shape. | |
12 | + */ | |
13 | +public class SubToolBox extends JPanel | |
14 | +{ | |
15 | + private JButton current; | |
16 | + | |
17 | + /** Creates a default SubToolBox. **/ | |
18 | + public SubToolBox() | |
19 | + { | |
20 | + // sets the default stuff and then starts off the choose panel method | |
21 | + setSize(40,66); | |
22 | + setLayout(null); | |
23 | + setVisible(true); | |
24 | + choosePanel(); | |
25 | + } | |
26 | + | |
27 | + /** Handles a switch in the toolbox panel with the corresponding tool. **/ | |
28 | + public void choosePanel() | |
29 | + { | |
30 | + // removes the current panel and adds the one corresponding to the selected tool. | |
31 | + removeAll(); | |
32 | + int tbs = ToolBox.toolSelected; | |
33 | + if(tbs == ToolBox.RECTANGLE || tbs == ToolBox.OVAL || tbs == ToolBox.ROUNDRECTANGLE) | |
34 | + shapePanel(); | |
35 | + else if(tbs == ToolBox.LINE) | |
36 | + linePanel(); | |
37 | + else if(tbs == ToolBox.SPRAY) | |
38 | + sprayPanel(); | |
39 | + else if(tbs == ToolBox.BRUSH) | |
40 | + brushPanel(); | |
41 | + else if(tbs == ToolBox.DRAG) | |
42 | + dragPanel(); | |
43 | + else if(tbs == ToolBox.RANDOMDRAW) | |
44 | + randomPanel(); | |
45 | + repaint(); | |
46 | + } | |
47 | + | |
48 | + /** Allows user to select settings for drawing random lines. **/ | |
49 | + public void randomPanel() | |
50 | + { | |
51 | + JTextField field = new JTextField(); | |
52 | + JButton submit = new JButton(); | |
53 | + field.setBounds(25,0,40,22); | |
54 | + submit.setBounds(25,22,40,22); | |
55 | + add(field); | |
56 | + add(submit); | |
57 | + submit.addActionListener(new JRandom(field)); | |
58 | + } | |
59 | + | |
60 | + /** Allows the user to set the thickness of a line. **/ | |
61 | + public void linePanel() | |
62 | + { | |
63 | + // creates a textbox and a button to submit the size of the line | |
64 | + JTextField field = new JTextField(); | |
65 | + JButton submit = new JButton(); | |
66 | + field.setBounds(25,0,40,22); | |
67 | + submit.setBounds(25,22,40,22); | |
68 | + add(field); | |
69 | + add(submit); | |
70 | + submit.addActionListener(new JTextListener(field)); | |
71 | + } | |
72 | + | |
73 | + /** Allows the user to set the size of the spray. **/ | |
74 | + public void sprayPanel() | |
75 | + { | |
76 | + // creates a field to set the size of the spray | |
77 | + JTextField field = new JTextField(); | |
78 | + JButton submit = new JButton(); | |
79 | + field.setBounds(25,0,40,22); | |
80 | + submit.setBounds(25,22,40,22); | |
81 | + add(field); | |
82 | + add(submit); | |
83 | + submit.addActionListener(new JSprayListener(field)); | |
84 | + } | |
85 | + | |
86 | + /** Allows the user to select the item to drag. **/ | |
87 | + public void dragPanel() | |
88 | + { | |
89 | + JButton rect = new JButton(new ImageIcon("Images//box.gif")); | |
90 | + JButton oval = new JButton(new ImageIcon("Images//oval.gif")); | |
91 | + rect.setSelectedIcon(new ImageIcon("Images//boxPressed.gif")); | |
92 | + oval.setSelectedIcon(new ImageIcon("Images//ovalPressed.gif")); | |
93 | + rect.setBounds(25,0,40,22); | |
94 | + oval.setBounds(25,22,40,22); | |
95 | + add(rect); | |
96 | + add(oval); | |
97 | + rect.addActionListener(new RectListener(rect)); | |
98 | + oval.addActionListener(new OvalListener(oval)); | |
99 | + JButton line = new JButton(new ImageIcon("Images//line.gif")); | |
100 | + line.setSelectedIcon(new ImageIcon("Images//linePressed.gif")); | |
101 | + line.setBounds(25,44,40,22); | |
102 | + add(line); | |
103 | + line.addActionListener(new LineListener(line)); | |
104 | + setCurrent(line); | |
105 | + DrawingCanvas.extraOperations = DrawingCanvas.LINE; | |
106 | + } | |
107 | + | |
108 | + /** Allows the user to select a style of brush. **/ | |
109 | + public void brushPanel() | |
110 | + { | |
111 | + // loads up the images when its pressed and when its not and sets | |
112 | + // its location and adds it to the screen. | |
113 | + JButton rect = new JButton(new ImageIcon("Images//box.gif")); | |
114 | + JButton oval = new JButton(new ImageIcon("Images//oval.gif")); | |
115 | + rect.setSelectedIcon(new ImageIcon("Images//boxPressed.gif")); | |
116 | + oval.setSelectedIcon(new ImageIcon("Images//ovalPressed.gif")); | |
117 | + rect.setBounds(25,0,40,22); | |
118 | + oval.setBounds(25,22,40,22); | |
119 | + add(rect); | |
120 | + add(oval); | |
121 | + rect.addActionListener(new RectListener(rect)); | |
122 | + oval.addActionListener(new OvalListener(oval)); | |
123 | + setCurrent(rect); | |
124 | + DrawingCanvas.extraOperations = DrawingCanvas.RECT; | |
125 | + } | |
126 | + | |
127 | + /** Creates a shapes panel and allows the user to choose the type. **/ | |
128 | + public void shapePanel() | |
129 | + { | |
130 | + // loads up the images when its pressed and when its not and sets | |
131 | + // its location and adds it to the screen. | |
132 | + JButton normal = new JButton(new ImageIcon("Images//normal.gif")); | |
133 | + JButton fill = new JButton(new ImageIcon("Images//fill.gif")); | |
134 | + JButton outline = new JButton(new ImageIcon("Images//outline.gif")); | |
135 | + normal.setSelectedIcon(new ImageIcon("Images//normalPressed.gif")); | |
136 | + fill.setSelectedIcon(new ImageIcon("Images//fillPressed.gif")); | |
137 | + outline.setSelectedIcon(new ImageIcon("Images//outlinePressed.gif")); | |
138 | + normal.setBounds(25,0,40,22); | |
139 | + fill.setBounds(25,22,40,22); | |
140 | + outline.setBounds(25,44,40,22); | |
141 | + add(normal); | |
142 | + add(fill); | |
143 | + add(outline); | |
144 | + normal.addActionListener(new NormalListener(normal)); | |
145 | + fill.addActionListener(new FillListener(fill)); | |
146 | + outline.addActionListener(new OutlineListener(outline)); | |
147 | + setCurrent(normal); | |
148 | + DrawingCanvas.extraOperations = DrawingCanvas.NORMAL; | |
149 | + } | |
150 | + | |
151 | + /** Sets the current special selection to the field. **/ | |
152 | + public void setCurrent(JButton b) | |
153 | + { | |
154 | + current = b; | |
155 | + current.setSelected(true); | |
156 | + } | |
157 | + /** Rectangle Listener, if button is pressed change field. **/ | |
158 | + public class RectListener implements ActionListener | |
159 | + { | |
160 | + private JButton b; | |
161 | + | |
162 | + public RectListener(JButton b){this.b=b;} | |
163 | + | |
164 | + public void actionPerformed(ActionEvent e) | |
165 | + { | |
166 | + current.setSelected(false); | |
167 | + setCurrent(b); | |
168 | + DrawingCanvas.extraOperations = DrawingCanvas.RECT; | |
169 | + } | |
170 | + } | |
171 | + /** Oval Listener, if button is pressed change field. **/ | |
172 | + public class OvalListener implements ActionListener | |
173 | + { | |
174 | + private JButton b; | |
175 | + | |
176 | + public OvalListener(JButton b){this.b=b;} | |
177 | + | |
178 | + public void actionPerformed(ActionEvent e) | |
179 | + { | |
180 | + current.setSelected(false); | |
181 | + setCurrent(b); | |
182 | + DrawingCanvas.extraOperations = DrawingCanvas.OVAL; | |
183 | + } | |
184 | + } | |
185 | + /** Line Listener, if button is pressed change field. **/ | |
186 | + public class LineListener implements ActionListener | |
187 | + { | |
188 | + private JButton b; | |
189 | + | |
190 | + public LineListener(JButton b){this.b=b;} | |
191 | + | |
192 | + public void actionPerformed(ActionEvent e) | |
193 | + { | |
194 | + current.setSelected(false); | |
195 | + setCurrent(b); | |
196 | + DrawingCanvas.extraOperations = DrawingCanvas.LINE; | |
197 | + } | |
198 | + } | |
199 | + /** Spray Listener, changes size of spray and brush. **/ | |
200 | + public class JSprayListener implements ActionListener | |
201 | + { | |
202 | + private JTextField f; | |
203 | + public JSprayListener(JTextField f){this.f=f;} | |
204 | + public void actionPerformed(ActionEvent e) | |
205 | + { | |
206 | + DrawingCanvas.radius = Math.abs(Integer.parseInt(f.getText())); | |
207 | + } | |
208 | + } | |
209 | + /** Random Listener, changes amount of lines. **/ | |
210 | + public class JRandom implements ActionListener | |
211 | + { | |
212 | + private JTextField f; | |
213 | + public JRandom (JTextField f){this.f=f;} | |
214 | + public void actionPerformed(ActionEvent e) | |
215 | + { | |
216 | + DrawingCanvas.amtLines = Integer.parseInt(f.getText()); | |
217 | + } | |
218 | + } | |
219 | + /** Text Listener, changes size of line. **/ | |
220 | + public class JTextListener implements ActionListener | |
221 | + { | |
222 | + private JTextField f; | |
223 | + public JTextListener(JTextField f){this.f=f;} | |
224 | + public void actionPerformed(ActionEvent e) | |
225 | + { | |
226 | + DrawingCanvas.lineWidth = Integer.parseInt(f.getText()); | |
227 | + } | |
228 | + } | |
229 | + /** Normal Listener, extra operations is set to normal. **/ | |
230 | + public class NormalListener implements ActionListener | |
231 | + { | |
232 | + private JButton b; | |
233 | + public NormalListener(JButton b){this.b=b;} | |
234 | + public void actionPerformed(ActionEvent e) | |
235 | + { | |
236 | + current.setSelected(false); | |
237 | + setCurrent(b); | |
238 | + DrawingCanvas.extraOperations = DrawingCanvas.NORMAL; | |
239 | + } | |
240 | + } | |
241 | + /** Fill Listener, extra operations is set to fill. **/ | |
242 | + public class FillListener implements ActionListener | |
243 | + { | |
244 | + private JButton b; | |
245 | + | |
246 | + public FillListener(JButton b){this.b=b;} | |
247 | + | |
248 | + public void actionPerformed(ActionEvent e) | |
249 | + { | |
250 | + current.setSelected(false); | |
251 | + setCurrent(b); | |
252 | + DrawingCanvas.extraOperations = DrawingCanvas.FILL; | |
253 | + } | |
254 | + } | |
255 | + /** Outline Listener, extra operations is set to outline. **/ | |
256 | + public class OutlineListener implements ActionListener | |
257 | + { | |
258 | + private JButton b; | |
259 | + | |
260 | + public OutlineListener(JButton b){this.b=b;} | |
261 | + | |
262 | + public void actionPerformed(ActionEvent e) | |
263 | + { | |
264 | + current.setSelected(false); | |
265 | + setCurrent(b); | |
266 | + DrawingCanvas.extraOperations = DrawingCanvas.OUTLINE; | |
267 | + } | |
268 | + } | |
269 | +} | |
0 | 270 | \ No newline at end of file | ... | ... |
No preview for this file type
... | ... | @@ -0,0 +1,38 @@ |
1 | +import javax.swing.JTabbedPane; | |
2 | +import java.awt.event.MouseListener; | |
3 | +import java.awt.event.MouseEvent; | |
4 | + | |
5 | +/** | |
6 | + * TabbedPanel holds all the drawing canvases together so you can have multiple | |
7 | + * documents opened all at once. This gives you multiple worksheets to work with. | |
8 | + **/ | |
9 | +public class TabbedPanel extends JTabbedPane implements MouseListener | |
10 | +{ | |
11 | + /** Creates a default TabbedPanel. **/ | |
12 | + //public TabbedPanel(){} | |
13 | + | |
14 | + /** Creates a TabbedPanel witht a unique id **/ | |
15 | + public TabbedPanel(int tabbedPlacement) | |
16 | + { | |
17 | + super(tabbedPlacement); | |
18 | + addMouseListener(this); | |
19 | + } | |
20 | + | |
21 | + /** If the mouse is pressed then change the currentFile id **/ | |
22 | + public void mousePressed(MouseEvent e) | |
23 | + { | |
24 | + ControlClass.currentFile = ((JTabbedPane)e.getSource()).getSelectedIndex(); | |
25 | + } | |
26 | + | |
27 | + /** Invoked when the mouse is clicked. **/ | |
28 | + public void mouseClicked(MouseEvent e){} | |
29 | + | |
30 | + /** Invoked when the mouse has entered a component. **/ | |
31 | + public void mouseEntered(MouseEvent e){} | |
32 | + | |
33 | + /** Invoked when the moues has exited a component. **/ | |
34 | + public void mouseExited(MouseEvent e){} | |
35 | + | |
36 | + /** Invoked when the mouse has been released. **/ | |
37 | + public void mouseReleased(MouseEvent e){} | |
38 | +} | |
0 | 39 | \ No newline at end of file | ... | ... |