Interactive 2D graphics
Graphics2D, picking and moving objects
The first update this year has been severely delayed because of personal and technical reasons, but let me not bug you with excuses. What is worth noting though, is that I have now switched domain name provider to Decna.com. This should mean a more reliable service and easier update of pages.
As for the exercises this week, let's look at some graphics and object picking with the mouse. The Java API features several neat classes for shapes which may be used for both the actual drawing of then and to implement user logic or geometrical
|
To start off this week, try to draw a few filled objects on the screen like circles, ovals, squares and rectangles. To makes this most convenient, use the Graphics2D class in java.awt. Using this is pretty straight forward: In the paint-method of your component, simply cast the incoming Graphics object to Graphics2D.
Hint: To get a jump start with this task, have a look at the graphics exercise in week 49 last year.
MyShapes.java
|
|
Now, let your window or frame implement MouseListener to track mouse actions in the component. When you click on one of your shapes, output the name of it.
Hint: The method contains(Point2D p) of the Shape class could be useful here.
MyShapes.java
|
|
Continue to implement the MouseMotionListener or MouseInputListener to detect mouse movements as well. You could now let the user be able to move you circles and rectangles around on the screen by selecting and dragging them with the mouse.
Hint: Have a look at week 25 last year for implementing the dragging feature.
MyMovingShapes.java
|
|
Finally, you might extend your simple object oriented drawing program to let the user click on
points in the corners of the shape and change the look of it. This could be limited to only
resizing the shapes, or extended to drawing arbitrary polygon figures.
|
|
|