In this project you will write an application for drawing Geometrical Shapes: Yo
ID: 663826 • Letter: I
Question
In this project you will write an application
for drawing Geometrical Shapes:
Your application will be build with a frame (JFrame) and
a tool bar (JToolBar) that will offer the user
a choice of figures to draw.
The set of choices should include:
-Oval -specifying enclosing rectangle
-Circle -same
-Square -same
-Rectangle -same
-Triangle - specifying three points (x,y) coordinates.
Once a choice of shape is made, your cursor will be
changed to a cross-hair cursor, indicating your
graphical editor is in drawing mode.
Once in drawing mode the first left button click will
define the origin of your shape. You have the choice
of requiring the user to either:
release the button and click again to define the
opposite diagonal corner of the rectangular
region that will enclose the figure,
or
to require the user to drag the cursor with
the left mouse button pressed down and then
release the button at the other end of the
rectangle defining the region that encloses
the shape being drawn.
For the triangle shape you will allow the user
to choose three points by clicking three times.
Once you capture these two or three points your program will
proceed to instantiate an object of that class of shape
and add it to an ordered list of Shape objects held in
an JPanel object. This list could be a Java collection-class
of object.
After every object entered your application will
proceed to repaint the JPanel holding all the
shapes the user has entered so far.
Once the panel is refreshed the cursor will switch back
to the normal arrow-head cursor and the customer can
select another menu choice.
Your main application class must be derived from the
JFrame class and must include a BorderLayout manager with
the JPanel object at the center section and with the JToolBar
object at the north layout section or sector.
Your application must provide not only MouseListener
interface implementations but also properly exit on user
requests for window close.
Please and thank you :)
Explanation / Answer
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Shape; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.geom.Arc2D; import java.awt.geom.CubicCurve2D; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.QuadCurve2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.util.Random; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; public class ShapeTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new ShapeTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } /** * This frame contains a combo box to select a shape and a component to draw it. */ class ShapeTestFrame extends JFrame { public ShapeTestFrame() { setTitle("ShapeTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); final ShapeComponent comp = new ShapeComponent(); add(comp, BorderLayout.CENTER); final JComboBox comboBox = new JComboBox(); comboBox.addItem(new LineMaker()); comboBox.addItem(new RectangleMaker()); comboBox.addItem(new RoundRectangleMaker()); comboBox.addItem(new EllipseMaker()); comboBox.addItem(new ArcMaker()); comboBox.addItem(new PolygonMaker()); comboBox.addItem(new QuadCurveMaker()); comboBox.addItem(new CubicCurveMaker()); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ShapeMaker shapeMaker = (ShapeMaker) comboBox.getSelectedItem(); comp.setShapeMaker(shapeMaker); } }); add(comboBox, BorderLayout.NORTH); comp.setShapeMaker((ShapeMaker) comboBox.getItemAt(0)); } private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 300; } /** * This component draws a shape and allows the user to move the points that define it. */ class ShapeComponent extends JComponent { public ShapeComponent() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { Point p = event.getPoint(); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.