Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Click areas are outside color areas, thanks for help to correct this in the below Java code so that you can only click inside the
Click areas are outside color areas, thanks for help to correct this in the below Java code so that you can only click inside the colored areas when moving the figures around with the mouse curzor. Please send back the code in whole with the changes of the code 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;
Shape temp shapesi;
for int j i; j shapes.length ; j
shapesj shapesj ;
shapesshapeslength temp;
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