Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a canvas class that extends JComponent. Make it implement the MouseListener interface. In the MouseListener methods, just print the event that is passed

1. Write a canvas class that extends JComponent. Make it implement the MouseListener interface. In the MouseListener methods, just print the event that is passed as argument to the constructor. In the canvas class constructor, have it add itself as its own MouseListener.

2. Add code to make your canvas draw a small filled circle on itself when it receives a mouseClicked event, at the location of the event: use MouseEvent.getX() and MouseEvent.getY().

3. Have your canvas implement the java.awt.event.MouseMotionListener interface and add itself as its own MouseMotionListener. Make your program draw small filled circles as the mouse is dragged. Congratulations: youve recreated the basics of MacPaint!

4. Extend your program to work as follows: When the mouse is first pressed, draw a small circle as above. As the mouse is dragged, draw a line connecting the original point to the current position of the mouse. Erase the previous line first so that your canvas doesnt get covered in black. When the mouse is released, leave the last line on the canvas. Think about what you need to keep track of between events to make this work. This needs to be part of the state of your canvas. . .

5. Have your canvas implement the java.awt.event.KeyListener interface, add itself as its own KeyListener, and call setFocusable(true) in order to receive keyboard focus. When a key is typed, your program should draw the corresponding character (see KeyEvent.getKeyChar()) on the canvas at the location of the event. If another key is typed without the mouse being clicked, then draw the next character to the right of the previous one as if you were typing in a text field. Again, think about what state you need to maintain to do this. It doesnt have to be perfect (you can hard-code the width of the characters if you like).

6. Let the user change the color used for painting by pressing a number key. Let the different numeric keys correspond to different colors and switch the drawing color depending on which key is pressed. If you want to use the numeric keypad for this, you might look into KeyEvent.getKeyLocation() and KeyEvent.KEY LOCATION NUMPAD, as well as the VK NUMPAD constants.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago