Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. // Assignment #: 7
  2. // Name: Your name
  3. // StudentID: Your id
  4. // Lecture: Your lecture
  5. // Description: it needs to be filled.
  6.  
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*; // to use listener interfaces
  10. import javax.swing.event.*;
  11. import java.util.ArrayList;
  12.  
  13. public class WholePanel extends JPanel
  14. {
  15. private ArrayList charList;
  16. private Color currentColor;
  17. private char currentChar;
  18.  
  19. private char finalChar;
  20.  
  21. private int currentSize;
  22. private CanvasPanel canvas;
  23. private JPanel menuPanel;
  24. private JButton undo;
  25.  
  26. private Point point;
  27. private ArrayList<Letter> letterList;
  28. private Letter letter;
  29.  
  30. public WholePanel()
  31. {
  32. letterList = new ArrayList<Letter>();
  33.  
  34. //black is the default color
  35. currentColor = Color.black;
  36. //20 is the default font size
  37. currentSize = 20;
  38.  
  39. // set the default character to 'a'
  40. currentChar = 'a';
  41.  
  42. charList = new ArrayList();
  43.  
  44. //create "undo" button
  45. undo = new JButton ("Undo");
  46.  
  47. menuPanel = new JPanel();
  48. menuPanel.add(undo);
  49.  
  50. canvas = new CanvasPanel();
  51.  
  52. JSplitPane sPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, menuPanel, canvas);
  53.  
  54. setLayout(new BorderLayout());
  55. add(sPane, BorderLayout.CENTER);
  56.  
  57.  
  58. //to be completed
  59.  
  60. }
  61.  
  62.  
  63. //insert ButtonListener, ColorListener, and SizeListener classes
  64.  
  65.  
  66. //CanvasPanel is the panel where pressed keys will be drawn
  67. private class CanvasPanel extends JPanel
  68. {
  69. //Constructor to initialize the canvas panel
  70. public CanvasPanel( )
  71. {
  72. // make this canvas panel listen to keys
  73. addKeyListener (new LetterListener());
  74. // make this canvas panel listen to mouse
  75. addMouseListener(new PointListener());
  76.  
  77. setBackground(Color.white);
  78.  
  79. //This method needs to be called for this panel to listen to keys
  80. //When panel listens to other things, and go back to listen
  81. //to keys, this method needs to be called again.
  82. requestFocus();
  83.  
  84. }
  85.  
  86.  
  87. //this method draws all characters pressed by a user so far
  88. public void paintComponent(Graphics page)
  89. {
  90. super.paintComponent(page);
  91.  
  92. //set color, font, then draw a string containing a character
  93. page.setColor(currentColor);
  94. page.setFont(new Font("TimesRoman", Font.PLAIN, currentSize));
  95.  
  96. //draws the string
  97. String tmp2 = "";
  98. String tmp = "";
  99. tmp = Character.toString(currentChar);
  100. tmp2 = tmp2 + tmp;
  101. char finalChar = String.charAt(String.length());
  102.  
  103. letter = new Letter(point.x, point.y, finalChar, currentColor, currentSize);
  104. letterList.add(letter);
  105. for (int i=0; i<letterList.size(); i++)
  106. {
  107. letterList.get(i).draw(page);
  108. }
  109. }
  110.  
  111. /** This method is overriden to enable keyboard focus */
  112. public boolean isFocusable()
  113. {
  114. return true;
  115. }
  116.  
  117. // listener class to listen to keyboard keys
  118. private class LetterListener implements KeyListener
  119. {
  120. public void keyReleased(KeyEvent e) {}
  121.  
  122. public void keyTyped(KeyEvent e) {}
  123.  
  124. // in case that a key is pressed, the following will be executed.
  125. public void keyPressed(KeyEvent e)
  126. {
  127. //get a letter (character key) pressed by a user
  128. currentChar = e.getKeyChar();
  129. //call paintComponent method indirectly
  130. canvas.repaint();
  131. }
  132. } // end of LetterListener
  133.  
  134.  
  135. //Mouse Listener class
  136. public class PointListener implements MouseListener
  137. {
  138. //in case that a user presses using a mouse,
  139. //record the point where it was pressed.
  140. public void mousePressed (MouseEvent event)
  141. {
  142. canvas.requestFocus();
  143. point = event.getPoint();
  144. repaint();
  145. }
  146.  
  147. public void mouseClicked (MouseEvent event) {}
  148. public void mouseReleased (MouseEvent event) {}
  149. public void mouseEntered (MouseEvent event) {}
  150. public void mouseExited (MouseEvent event) {}
  151. public void mouseMoved(MouseEvent event) {}
  152. public void mouseDragged(MouseEvent event) {}
  153.  
  154. }
  155.  
  156. } // end of Canvas Panel Class
  157.  
  158. } // end of Whole Panel Class
Add Comment
Please, Sign In to add comment