Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help to change the below Java code so the output show a red circle and a yellow square and a blue triangle like in
Please help to change the below Java code so the output show a red circle and a yellow square and a blue triangle like in the added pic. Please send the code back in whole with the changes in it Thanks! import javax.swing.;
import java.awt.;
import java.awt.event.;
class Shape
int x y; Position of the top left corner
int width, height;
Color color;
public Shapeint x int y int width, int height, Color color
this.x x;
this.y y;
this.width width;
this.height height;
this.color color;
public void drawGraphics g
gsetColorcolor;
Implement drawing logic for the shape eg gfillRect, gfillOval, etc.
gfillRectx y width, height;
public boolean containsPoint point
Implement logic to check if a point is inside the shape
return pointx x && point.x x width && point.y y && point.y y height;
public void moveint dx int dy
x dx;
y dy;
class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener
private Shape shapes;
private Shape selectedShape;
private Point lastMousePosition;
public DrawingPanel
shapes new Shape;
shapes new Shape Color.BLUE;
shapes new Shape Color.RED;
shapes new Shape Color.YELLOW;
addMouseListenerthis;
addMouseMotionListenerthis;
@Override
protected void paintComponentGraphics g
super.paintComponentg;
for Shape shape : shapes
shape.drawg;
@Override
public void mousePressedMouseEvent e
Point mousePoint egetPoint;
for int i shapes.length ; i ; i
if shapesicontainsmousePoint
selectedShape shapesi;
lastMousePosition mousePoint;
break;
@Override
public void mouseDraggedMouseEvent e
if selectedShape null
int dx egetX lastMousePosition.x;
int dy egetY lastMousePosition.y;
selectedShape.movedx dy;
lastMousePosition egetPoint;
repaint;
Implement other MouseListener and MouseMotionListener methods
@Override
public void mouseClickedMouseEvent e
@Override
public void mouseReleasedMouseEvent e
@Override
public void mouseEnteredMouseEvent e
@Override
public void mouseExitedMouseEvent e
@Override
public void mouseMovedMouseEvent e
public static void mainString args
JFrame frame new JFrameShape Manipulation Program";
frame.setDefaultCloseOperationJFrameEXITONCLOSE;
frame.setSize;
DrawingPanel panel new DrawingPanel;
frame.addpanel;
frame.setVisibletrue;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started