Codes are as follow:
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.lang.StringBuffer;
- public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, KeyListener
- {
- private Point oldPoint, currentPoint;
- private String s;
- private FontMetrics fm;
- private JButton circBtn, rectBtn, lineBtn, eraseBtn, clearBtn, sendBtn;
- JPanel drawingArea = new JPanel();
- JPanel jp = new JPanel();
- TextField message = new TextField("Draw away! Click anywhere on canvas to type");
- int option;
- int startx, starty; // The location of the mouse when the dragging started.
- int mousex, mousey; // The location of the mouse during dragging.
- public void init() {
- //initialize the "old" point
- oldPoint = new Point(0,0);
- this.setSize(new Dimension(600, 500));
- }
- public static void main(String[] args) {
- new JPanels();
- }
- public JPanels() {
- super("Welcome to Whiteboard!");
- Container content = getContentPane();
- content.setBackground(Color.lightGray);
- circBtn = new JButton("Circle");
- rectBtn = new JButton("Rectangle");
- lineBtn = new JButton("Line");
- eraseBtn = new JButton("Eraser");
- clearBtn = new JButton("Clear");
- sendBtn = new JButton("Send");
- jp.setLayout(new GridLayout(0,6));
- jp.add(circBtn,0);
- jp.add(rectBtn,1);
- jp.add(lineBtn,2);
- jp.add(eraseBtn,3);
- jp.add(clearBtn,4);
- jp.add(sendBtn,5);
- message.setBackground(Color.gray);
- message.setEditable(false);
- drawingArea.setPreferredSize(new Dimension(500, 400));
- drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
- drawingArea.setBackground(Color.white);
- content.add(jp, BorderLayout.NORTH);
- content.add(drawingArea, BorderLayout.CENTER);
- content.add(message, BorderLayout.SOUTH);
- pack();
- this.setVisible(true);
- this.setResizable(true);
- drawingArea.addMouseListener(this);
- drawingArea.addMouseMotionListener(this);
- //ActionPerformed methods for the buttons on the side panel
- circBtn.setToolTipText("Draw a Circle");
- circBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- circBtn_actionPerformed(e);
- }
- });
- rectBtn.setToolTipText("Draw a Rectangle");
- rectBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- rectBtn_actionPerformed(e);
- }
- });
- lineBtn.setToolTipText("Draw a Line");
- lineBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- lineBtn_actionPerformed(e);
- }
- });
- eraseBtn.setToolTipText("Eraser");
- eraseBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- eraseBtn_actionPerformed(e);
- }
- });
- clearBtn.setToolTipText("Clear Canvas");
- clearBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- clearBtn_actionPerformed(e);
- }
- });
- sendBtn.setToolTipText("Send drawing to friend");
- sendBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- sendBtn_actionPerformed(e);
- }
- });
- }
- private void circBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Circle Button test");
- // TODO: Add any handling code here
- option = 1;
- }
- private void rectBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Rect Button test");
- // TODO: Add any handling code here
- option = 2;
- }
- private void lineBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Line Button test");
- // TODO: Add any handling code here
- option = 3;
- }
- private void eraseBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Erase Button test");
- // TODO: Add any handling code here
- option = 4;
- }
- private void clearBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Clear Button test");
- // TODO: Add any handling code here
- drawingArea.repaint();
- message.setText("Canvas cleared!");
- }
- private void sendBtn_actionPerformed(ActionEvent e)
- {
- System.out.println("This is the Send Button test");
- // TODO: Add any handling code here
- }
- //when the mouse is pressed, a point is formed
- public void mousePressed(MouseEvent e){
- startx = mousex = e.getX(); // Save coords of mouse position.
- starty = mousey = e.getY();
- oldPoint = e.getPoint();
- String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");
- System.out.println(msg);
- message.setText(msg);
- s = "";
- }
- //mouseDragged will do the line drawing
- public void mouseDragged(MouseEvent e){
- mousex = e.getX();
- mousey = e.getY();
- currentPoint = e.getPoint();
- String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
- System.out.println(msg);
- message.setText(msg);
- if(option == 1){
- //drawingArea.getGraphics().drawOval(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
- Graphics g = drawingArea.getGraphics();
- drawOval(g);
- }
- else if (option == 2){
- // drawingArea.getGraphics().drawRect(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
- Graphics g = drawingArea.getGraphics();
- drawRect(g);
- }
- else if (option == 3){
- drawingArea.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
- }
- else if (option == 4){
- Graphics g = drawingArea.getGraphics();
- g.setColor(Color.white);
- g.fillOval(oldPoint.x,oldPoint.y,20,20);
- }
- oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
- }
- public boolean keyDown(Event e, int key) {
- String s = String.valueOf((char)key);
- drawingArea.getGraphics().drawString(s, oldPoint.x, oldPoint.y);
- System.out.println("this is s: ");
- Font font = new Font("Arial", Font.BOLD, 12);
- fm = getFontMetrics(font);
- return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
- }
- protected boolean record(int x, int y) {
- oldPoint.x = x;
- oldPoint.y = y;
- return(true);
- }
- public void paint(Graphics g)
- {
- }
- void drawRect(Graphics g) {
- int x, y; // Top left corner of the rectangle.
- int w, h; // Width and height of the rectangle.
- // x,y,w,h must be computed from the coordinates
- // of the two corner points.
- if (mousex > startx) {
- x = startx;
- w = mousex - startx;
- }
- else {
- x = mousex;
- w = startx - mousex;
- }
- if (mousey > starty) {
- y = starty;
- h = mousey - starty;
- }
- else {
- y = mousey;
- h = starty - mousey;
- }
- g.setColor(Color.white);
- g.fillRect(x, y, w, h);
- g.setColor(Color.black);
- g.drawRect(x, y, w-1, h-1);
- }
- // end drawRect()
- void drawOval(Graphics g) {
- int x, y; // Top left corner of the rectangle.
- int w, h; // Width and height of the rectangle.
- // x,y,w,h must be computed from the coordinates
- // of the two corner points.
- if (mousex > startx) {
- x = startx;
- w = mousex - startx;
- }
- else {
- x = mousex;
- w = startx - mousex;
- }
- if (mousey > starty) {
- y = starty;
- h = mousey - starty;
- }
- else {
- y = mousey;
- h = starty - mousey;
- }
- g.setColor(Color.white);
- g.fillOval(x, y, w, h);
- g.setColor(Color.black);
- g.drawOval(x, y, w-1, h-1);
- }
- // end drawRect()
- //abstract methods promised by the MouseListener and MouseMotionListener interfaces
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseReleased(MouseEvent e){}
- public void mouseClicked(MouseEvent e){}
- public void mouseMoved(MouseEvent e){}
- public void keyReleased(KeyEvent e){}
- public void keyPressed(KeyEvent e){}
- }