Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help to change in the below Java code so when a figure moves with the mouse curzor, it ends up on top so
Please help to change in the below Java code so when a figure moves with the mouse curzor, it ends up on top" so that it covers other figures that are in an overlapping position. Please send back the code in whole with the changes in it Thanks! import javax.swing.;
import java.awt.;
import java.awt.event.;
class Shape
int x y;
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;
Use appropriate drawing methods for each shape
if color Color.RED
gfillOvalx y width, height; Draw circle for red shape
else if color Color.YELLOW
gfillRectx y width, height; Draw square for yellow shape
else if color Color.BLUE
int xPoints x x width x width;
int yPoints y height, y y height;
gfillPolygonxPoints yPoints, ; Draw triangle for blue shape
public boolean containsPoint point
Implement logic to check if point is inside the shape based on its type
if color Color.RED
return Math.powpointx x width Math.powpointy y height Math.powwidth ;
else if color Color.YELLOW
return point.x x && point.x x width && point.y y && point.y y height;
else if color Color.BLUE
return point.x x && point.x x width && point.y y && point.y y height;
return false;
public void moveint dx int dy
x dx;
y dy;
abstract 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
public static void mainString args
JFrame frame new JFrameShape Manipulation Program";
frame.setDefaultCloseOperationJFrameEXITONCLOSE;
frame.setSize;
DrawingPanel panel new DrawingPanel
@Override
public void mouseMovedMouseEvent e
@Override
public void mouseClickedMouseEvent e
@Override
public void mouseReleasedMouseEvent e
@Override
public void mouseEnteredMouseEvent e
@Override
public void mouseExitedMouseEvent e
;
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