Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. public class PostItNoteFrame extends JFrame implements ActionListener, MouseListener, MouseMotionListener{
  2.  
  3.  
  4. //create objects,strings,variables etc
  5. JPanel TArea;
  6. JTextArea postItNoteContent = new JTextArea();
  7. JScrollPane postItNoteScroll = new JScrollPane(postItNoteContent);
  8.  
  9. private String Text;
  10. private static int PostItNoteCount = 1;
  11. private Font buttonFont = new Font("Arial", Font.BOLD, 30);
  12. private Color bodyColor = new Color(253, 253, 201);
  13. private Color titleColor = new Color(248, 247, 182);
  14.  
  15. BorderLayout bl = new BorderLayout();
  16.  
  17. /**
  18. * post it note frame
  19. * sets the post it notes basic settings
  20. * @param dim dimensions of post it note
  21. */
  22. public PostItNoteFrame (Point dim){
  23.  
  24. // set Frame to visible, set dimensions, set title, set layout
  25. this.setSize(new Dimension(dim.x, dim.y));
  26. this.setTitle("Post-It Note");
  27. this.setLayout(bl);
  28. Point StartLoc = this.getLocation();
  29. StartLoc.x = StartLoc.x+this.getWidth();
  30.  
  31.  
  32.  
  33. //Create the buttons and add to there respect areas
  34. this.add(createButtonArea(), BorderLayout.NORTH);
  35. this.add(createContent(), BorderLayout.CENTER);
  36.  
  37. // closes process after exiting using close button
  38. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.  
  40. // set undercorated true
  41. this.setUndecorated(true);
  42. this.addMouseListener(this);
  43.  
  44. // Set
  45. this.setVisible(true);
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. /**
  53. * create the content note area
  54. * @returns the content area of the post it note
  55. */
  56. private JScrollPane createContent(){
  57.  
  58. // create text area And scroll Pane
  59.  
  60.  
  61. //set background colours set the scroll border to null
  62. postItNoteContent.setBackground(bodyColor);
  63. postItNoteScroll.setBackground(bodyColor);
  64. postItNoteScroll.setBorder(null);
  65. postItNoteContent.setMargin(new Insets(10,10,10,10));
  66.  
  67. return postItNoteScroll;
  68.  
  69. }
  70.  
  71. /**
  72. * create button area method
  73. * setups the button areas
  74. * @return the newly formated jpanel
  75. */
  76. private JPanel createButtonArea(){
  77.  
  78.  
  79. //create button area
  80. JPanel buttonArea = new JPanel();
  81. JButton newPostItNote = new JButton ("+");
  82. JButton deletePostItNote = new JButton("x");
  83.  
  84. //set background color
  85. buttonArea.setBackground(titleColor);
  86.  
  87. // reset the Layout
  88. buttonArea.setLayout(new BorderLayout());
  89.  
  90.  
  91.  
  92. // set settings for PostItNote
  93. newPostItNote.setFont(buttonFont);
  94. newPostItNote.setForeground(Color.GRAY);
  95. newPostItNote.setBackground(titleColor);
  96. newPostItNote.setOpaque(true);
  97. newPostItNote.setBorderPainted(false);
  98.  
  99. // set settings for DeletePostItNote
  100. deletePostItNote.setFont(buttonFont);
  101. deletePostItNote.setForeground(Color.GRAY);
  102. deletePostItNote.setBackground(titleColor);
  103. deletePostItNote.setOpaque(true);
  104. deletePostItNote.setBorderPainted(false);
  105.  
  106. // create buttons and then add them (Set their alignment)
  107. buttonArea.add(newPostItNote, BorderLayout.WEST);
  108. buttonArea.add(deletePostItNote, BorderLayout.EAST);
  109.  
  110.  
  111. // set action command and listeners
  112. newPostItNote.setActionCommand("CreateNewPostItNote");
  113. newPostItNote.addActionListener(this);
  114. deletePostItNote.setActionCommand("deletePostItNote");
  115. deletePostItNote.addActionListener(this);
  116.  
  117. return buttonArea;
  118.  
  119. }
  120.  
  121. /**
  122. * method for events
  123. * create new post it note and delete note button events
  124. */
  125. public void actionPerformed(ActionEvent e) {
  126. // TODO Auto-generated method stub
  127. this.addMouseListener(this);
  128. if(e.getActionCommand().equals("CreateNewPostItNote")) {
  129.  
  130. //Calculate the location for the new note to be placed
  131. Point startLoc = this.getLocation();
  132. //Move across by the width of the note
  133. startLoc.x = startLoc.x+this.getWidth();
  134. //Check we fit within the screen x size
  135. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  136.  
  137. if (startLoc.x > screen.width - getWidth()){
  138. //If so move down y and reset x pos
  139. startLoc.x = 0;
  140. startLoc.y += this.getHeight();
  141. }
  142. PostItNoteFrame note = new PostItNoteFrame(startLoc, new Point(200,200));
  143. incPostItNoteCount();
  144.  
  145. }
  146.  
  147. else if(e.getActionCommand().equals("deletePostItNote")){
  148. this.dispose();
  149. decPostItNoteCount();
  150.  
  151. if (getPostItNoteCount() < 1){
  152. System.out.println("Exiting Post-It note application.");
  153. System.exit(0);
  154. }
  155. }
  156. }
  157.  
  158.  
  159. *
  160. * @return the copied text
  161. */
  162. public String getCopiedText(){
  163. return Text;
  164. }
  165.  
  166. /**
  167. *
  168. * @param Text to be copied
  169. */
  170. public void setCopiedText(String Text){
  171. this.Text = Text;
  172. }
  173.  
  174. /**
  175. *
  176. * @param postItNoteContent to set
  177. */
  178. public void setPostItNoteContent(JTextArea postItNoteContent){
  179. this.postItNoteContent = postItNoteContent;
  180. }
  181.  
  182. /**
  183. *
  184. * @return content of post it note content
  185. */
  186. public JTextArea getPostItNoteContent(){
  187. return postItNoteContent;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement