Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.Drawer;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.*;
  8.  
  9. public class View {
  10.  
  11. /*Body of view */
  12. JFrame Body = new JFrame();
  13.  
  14. /* menu bar */
  15. private JMenuBar menuBar ;
  16. private JMenu fileMenu ;
  17. private JMenuItem saveMenuItem , loadMenuItem;
  18.  
  19. /* header and footer of view */
  20. JPanel header , footer ;
  21.  
  22. /* Header or Drawing buttons (shapes and logic) */
  23. private JButton selectButton , moveButton , deleteButton , resizeButton ,
  24. rectButton , squareButton , lineButton , brushButton ,
  25. circleButton , ellipseButton , triButton , polygonButton ;
  26.  
  27. private JRadioButton strokeRadioButton , fillRadioButton;
  28.  
  29. private JButton selectingColor ;
  30. private Color curColor = Color.BLACK;
  31. private JSlider transSlider;
  32. private JLabel transLabel;
  33.  
  34.  
  35. /*Footer component*/
  36. JLabel stateScreen , xScreen , yScreen;
  37. JButton undoButton , redoButton;
  38.  
  39.  
  40. /**
  41. * instantiate all objects
  42. */
  43.  
  44. private void createObjects () {
  45.  
  46. menuBar = new JMenuBar();
  47. fileMenu = new JMenu("File");
  48. saveMenuItem = new JMenuItem("save");
  49. loadMenuItem = new JMenuItem("load");
  50.  
  51. header = new JPanel();
  52. footer = new JPanel();
  53.  
  54. selectButton = new JButton ("sel");
  55. moveButton = new JButton ("move");
  56. deleteButton = new JButton ("del");
  57. resizeButton = new JButton ("resize");
  58.  
  59. rectButton = new JButton ("rect");
  60. squareButton = new JButton ("sq");
  61. lineButton = new JButton ("line");
  62. brushButton = new JButton ("brush");
  63. circleButton = new JButton ("cir");
  64. ellipseButton = new JButton ("elipse");
  65. triButton = new JButton ("tri");
  66. polygonButton = new JButton ("pol");
  67.  
  68. strokeRadioButton = new JRadioButton("stroke", true);
  69. fillRadioButton = new JRadioButton("fill", false);
  70. ButtonGroup group = new ButtonGroup();
  71. group.add(strokeRadioButton);
  72. group.add(fillRadioButton);
  73.  
  74. selectingColor = new JButton("choose color");
  75. selectingColor .addActionListener( new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent e) {
  78. curColor = JColorChooser.showDialog(null,"choose color", curColor);
  79. if( curColor == null )
  80. curColor = Color.BLACK;
  81. }
  82. });
  83.  
  84. transSlider = new JSlider();
  85. transLabel = new JLabel("Transparent: 1");
  86.  
  87.  
  88. stateScreen = new JLabel ("");
  89. xScreen = new JLabel ("X :");
  90. yScreen = new JLabel ("Y :");
  91.  
  92. undoButton = new JButton("undo");
  93. redoButton = new JButton("redo");
  94.  
  95. }
  96. /**
  97. * creating header of view
  98. */
  99. private void createHeader () {
  100. header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
  101.  
  102.  
  103. JPanel buttonsGrid = new JPanel( new GridLayout (3, 4) );
  104.  
  105. buttonsGrid.add(selectButton);
  106. buttonsGrid.add(moveButton);
  107. buttonsGrid.add(deleteButton);
  108. buttonsGrid.add(resizeButton);
  109.  
  110. buttonsGrid.add(lineButton);
  111. buttonsGrid.add(brushButton);
  112. buttonsGrid.add(rectButton);
  113. buttonsGrid.add(squareButton);
  114.  
  115. buttonsGrid.add(circleButton);
  116. buttonsGrid.add(ellipseButton);
  117. buttonsGrid.add(triButton);
  118. buttonsGrid.add(polygonButton);
  119.  
  120. header.add(buttonsGrid);
  121. header.add( new JSeparator( SwingConstants.VERTICAL ) );
  122.  
  123. buttonsGrid = new JPanel ( new GridLayout ( 2,1 ) );
  124.  
  125. buttonsGrid.add(strokeRadioButton);
  126. buttonsGrid.add(fillRadioButton);
  127.  
  128. header.add(buttonsGrid);
  129. header.add( new JSeparator( SwingConstants.VERTICAL ) );
  130.  
  131. buttonsGrid = new JPanel ( new GridLayout ( 2,1 ) );
  132. buttonsGrid.add( selectingColor );
  133. // trans component
  134. JPanel trans = new JPanel( new FlowLayout() );
  135. trans.add(transLabel);
  136. trans.add(transSlider);
  137.  
  138. buttonsGrid.add(trans);
  139.  
  140. header.add(buttonsGrid);
  141.  
  142. }
  143. /**
  144. * creating footer of View
  145. */
  146. private void createFooter () {
  147. footer.setLayout( new BorderLayout() );
  148. JPanel tempLayout = new JPanel ( new GridLayout (1,3) );
  149. tempLayout.add(stateScreen);
  150. tempLayout.add(xScreen);
  151. tempLayout.add(yScreen);
  152. footer.add(tempLayout);
  153. tempLayout = new JPanel ( new FlowLayout () );
  154. tempLayout.add(undoButton);
  155. tempLayout.add(redoButton);
  156. footer.add(tempLayout, BorderLayout.EAST);
  157. }
  158.  
  159. private void addMenuBar () {
  160. fileMenu.add(saveMenuItem);
  161. fileMenu.add(loadMenuItem);
  162. menuBar.add(fileMenu);
  163. }
  164.  
  165. public View () {
  166. Body = new JFrame () ;
  167. Body.setTitle("Drawer");
  168.  
  169. createObjects();
  170. addMenuBar();
  171. createHeader();
  172. createFooter();
  173.  
  174. Body.setJMenuBar(menuBar);
  175. Body.add(header , BorderLayout.NORTH);
  176. Body.add(new DrawingArea() , BorderLayout.CENTER );
  177. Body.add(footer , BorderLayout.SOUTH);
  178.  
  179. Body.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  180. Body.pack();
  181. Body.setVisible(true);
  182.  
  183. }
  184.  
  185. private class DrawingArea extends JPanel {
  186. public DrawingArea() {
  187. setBackground(Color.WHITE);
  188. }
  189. }
  190.  
  191. public static void main ( String [] args ) {
  192. View obj = new View();
  193. }
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. // we gonna need it later
  216. ///**
  217. //* this function to create Buttons with draw lisnter
  218. //* @param name : this is name of Button
  219. //* @return JButton ret
  220. //*/
  221. //private JButton makeButtonWithDrawingLisnter ( final String name ) {
  222. // JButton ret = new JButton(name);
  223. //
  224. // ret.addActionListener(new ActionListener() {
  225. // public void actionPerformed(ActionEvent e) {
  226. // // draw here
  227. // System.out.println("Draw here");
  228. // }
  229. // });
  230. //
  231. // return ret ;
  232. //}
  233. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement