Menu
When a menu bar is constructed, the invisible but "click-able" buttons are created. The MouseListener and MouseMotionListener, which are implemented by the PaintApp class, listen for whenever the mouse is clicked, dragged, or released inside the application. A MouseEvent is passed to methods like mousePressed() and the program can get the coordinates of where the mouse clicked.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public Menu(PaintApp pLab, Layers lArray) { // When the menu object is constructed, the invisible but "click-able" buttons are also constructed paintLab = pLab; // Initializes paintLab to the actual PaintApp, and layersArray to the actual layers array layersArray = lArray; // So that Menu class' methods can access the non-static fields and methods, such as imageArray selectColor = new Color( 0 , 0 , 0 ); fontColor = new Color( 0 , 0 , 0 ); btnFont = new Font( "Georgia" ,Font.BOLD, 17 ); numTool = 1 ; strokeSize = 15 ; colorRect = new Rectangle( 13 , 35 , 35 , 35 ); // Color button (invisible but "click-able") pencilRect = new Rectangle( 53 , 35 , 80 , 35 ); // Tool buttons (invisible but "click-able") brushRect = new Rectangle( 138 , 35 , 80 , 35 ); sprayRect = new Rectangle( 223 , 35 , 80 , 35 ); sizeRect = new Rectangle( 308 , 35 , 60 , 35 ); // Size buttons (invisible but "click-able") newRect = new Rectangle( 373 , 35 , 70 , 35 ); // Layer buttons (invisible but "click-able") prevRect = new Rectangle( 448 , 35 , 70 , 35 ); nextRect = new Rectangle( 523 , 35 , 70 , 35 ); delRect = new Rectangle( 598 , 35 , 70 , 35 ); menuRect = new Rectangle( 8 , 30 ,paintLab.getAppWidth(), paintLab.getMenuHeight()); } |
public Menu(PaintApp pLab, Layers lArray) { // When the menu object is constructed, the invisible but "click-able" buttons are also constructed paintLab = pLab; // Initializes paintLab to the actual PaintApp, and layersArray to the actual layers array layersArray = lArray; // So that Menu class' methods can access the non-static fields and methods, such as imageArray selectColor = new Color(0,0,0); fontColor = new Color(0,0,0); btnFont = new Font("Georgia",Font.BOLD,17); numTool = 1; strokeSize = 15; colorRect = new Rectangle(13, 35, 35, 35); // Color button (invisible but "click-able") pencilRect = new Rectangle(53, 35, 80, 35); // Tool buttons (invisible but "click-able") brushRect = new Rectangle(138, 35, 80, 35); sprayRect = new Rectangle(223, 35, 80, 35); sizeRect = new Rectangle(308, 35, 60, 35); // Size buttons (invisible but "click-able") newRect = new Rectangle(373, 35, 70, 35); // Layer buttons (invisible but "click-able") prevRect = new Rectangle(448, 35, 70, 35); nextRect = new Rectangle(523, 35, 70, 35); delRect = new Rectangle(598, 35, 70, 35); menuRect = new Rectangle(8,30,paintLab.getAppWidth(), paintLab.getMenuHeight()); }
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | public void mousePressed(MouseEvent e) { // When any of the mouse buttons are pressed down menuBar.select(e.getX(), e.getY()); newX = e.getX(); newY = e.getY(); oldX = e.getX(); oldY = e.getY(); if (menuBar.menuRect.contains(e.getX(),e.getY())) { continuePaint = false ; // After the user selects a menu item, continuePaint will prevent the area under the menu from getting painted. menuChange = true ; repaint(); } if (canvasRect.contains(e.getX(), e.getY())) { // The application repaints if the user clicks inside the canvas repaint(); } } |
public void mousePressed(MouseEvent e) { // When any of the mouse buttons are pressed down menuBar.select(e.getX(), e.getY()); newX = e.getX(); newY = e.getY(); oldX = e.getX(); oldY = e.getY(); if (menuBar.menuRect.contains(e.getX(),e.getY())) { continuePaint = false; // After the user selects a menu item, continuePaint will prevent the area under the menu from getting painted. menuChange = true; repaint(); } if (canvasRect.contains(e.getX(), e.getY())) { // The application repaints if the user clicks inside the canvas repaint(); } }
The select() calls a certain function, such as Layer's newLayer(), based on where the user clicked.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | public void select( int x, int y) { // Selects menu items if (colorRect.contains(x, y)) { // If mouse clicks on the color button selectColor(); } if (pencilRect.contains(x, y)) { // If mouse selects a tool numTool = 1 ; } else if (brushRect.contains(x, y)) { numTool = 2 ; } else if (sprayRect.contains(x, y)) { numTool = 3 ; } if (sizeRect.contains(x, y)) { // If mouse clicks on the size button selectSize(); } if (newRect.contains(x, y)) { // If mouse clicks on a layer button layersArray.newLayer(); } else if (prevRect.contains(x, y)) { layersArray.previousLayer(); } else if (nextRect.contains(x, y)) { layersArray.nextLayer(); } else if (delRect.contains(x, y)) { layersArray.deleteLayer(); } } |
public void select(int x, int y) { // Selects menu items if (colorRect.contains(x, y)) { // If mouse clicks on the color button selectColor(); } if (pencilRect.contains(x, y)) { // If mouse selects a tool numTool = 1; } else if (brushRect.contains(x, y)) { numTool = 2; } else if (sprayRect.contains(x, y)) { numTool = 3; } if (sizeRect.contains(x, y)) { // If mouse clicks on the size button selectSize(); } if (newRect.contains(x, y)) { // If mouse clicks on a layer button layersArray.newLayer(); } else if (prevRect.contains(x, y)) { layersArray.previousLayer(); } else if (nextRect.contains(x, y)) { layersArray.nextLayer(); } else if (delRect.contains(x, y)) { layersArray.deleteLayer(); } }