Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment #: 7
- // Name: Your name
- // StudentID: Your id
- // Lecture: Your lecture
- // Description: it needs to be filled.
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*; // to use listener interfaces
- import javax.swing.event.*;
- import java.util.ArrayList;
- public class WholePanel extends JPanel
- {
- private ArrayList charList;
- private Color currentColor;
- private char currentChar;
- private char finalChar;
- private int currentSize;
- private CanvasPanel canvas;
- private JPanel menuPanel;
- private JButton undo;
- private Point point;
- private ArrayList<Letter> letterList;
- private Letter letter;
- public WholePanel()
- {
- letterList = new ArrayList<Letter>();
- //black is the default color
- currentColor = Color.black;
- //20 is the default font size
- currentSize = 20;
- // set the default character to 'a'
- currentChar = 'a';
- charList = new ArrayList();
- //create "undo" button
- undo = new JButton ("Undo");
- menuPanel = new JPanel();
- menuPanel.add(undo);
- canvas = new CanvasPanel();
- JSplitPane sPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, menuPanel, canvas);
- setLayout(new BorderLayout());
- add(sPane, BorderLayout.CENTER);
- //to be completed
- }
- //insert ButtonListener, ColorListener, and SizeListener classes
- //CanvasPanel is the panel where pressed keys will be drawn
- private class CanvasPanel extends JPanel
- {
- //Constructor to initialize the canvas panel
- public CanvasPanel( )
- {
- // make this canvas panel listen to keys
- addKeyListener (new LetterListener());
- // make this canvas panel listen to mouse
- addMouseListener(new PointListener());
- setBackground(Color.white);
- //This method needs to be called for this panel to listen to keys
- //When panel listens to other things, and go back to listen
- //to keys, this method needs to be called again.
- requestFocus();
- }
- //this method draws all characters pressed by a user so far
- public void paintComponent(Graphics page)
- {
- super.paintComponent(page);
- //set color, font, then draw a string containing a character
- page.setColor(currentColor);
- page.setFont(new Font("TimesRoman", Font.PLAIN, currentSize));
- //draws the string
- String tmp2 = "";
- String tmp = "";
- tmp = Character.toString(currentChar);
- tmp2 = tmp2 + tmp;
- char finalChar = String.charAt(String.length());
- letter = new Letter(point.x, point.y, finalChar, currentColor, currentSize);
- letterList.add(letter);
- for (int i=0; i<letterList.size(); i++)
- {
- letterList.get(i).draw(page);
- }
- }
- /** This method is overriden to enable keyboard focus */
- public boolean isFocusable()
- {
- return true;
- }
- // listener class to listen to keyboard keys
- private class LetterListener implements KeyListener
- {
- public void keyReleased(KeyEvent e) {}
- public void keyTyped(KeyEvent e) {}
- // in case that a key is pressed, the following will be executed.
- public void keyPressed(KeyEvent e)
- {
- //get a letter (character key) pressed by a user
- currentChar = e.getKeyChar();
- //call paintComponent method indirectly
- canvas.repaint();
- }
- } // end of LetterListener
- //Mouse Listener class
- public class PointListener implements MouseListener
- {
- //in case that a user presses using a mouse,
- //record the point where it was pressed.
- public void mousePressed (MouseEvent event)
- {
- canvas.requestFocus();
- point = event.getPoint();
- repaint();
- }
- public void mouseClicked (MouseEvent event) {}
- public void mouseReleased (MouseEvent event) {}
- public void mouseEntered (MouseEvent event) {}
- public void mouseExited (MouseEvent event) {}
- public void mouseMoved(MouseEvent event) {}
- public void mouseDragged(MouseEvent event) {}
- }
- } // end of Canvas Panel Class
- } // end of Whole Panel Class
Add Comment
Please, Sign In to add comment