Question: 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 Shape(int 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 draw(Graphics g){
g.setColor(color);
// Implement drawing logic for the shape (e.g., g.fillRect, g.fillOval, etc.)
g.fillRect(x, y, width, height);
}
public boolean contains(Point point){
// Implement logic to check if a point is inside the shape
return (point.x >= x && point.x = x + width && point.y >= y && point.y = y + height);
}
public void move(int 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[3];
shapes[0]= new Shape(50,50,50,50, Color.BLUE);
shapes[1]= new Shape(100,100,50,50, Color.RED);
shapes[2]= new Shape(150,150,50,50, Color.YELLOW);
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Shape shape : shapes){
shape.draw(g);
}
}
@Override
public void mousePressed(MouseEvent e){
Point mousePoint = e.getPoint();
for (int i = shapes.length -1; i >=0; i--){
if (shapes[i].contains(mousePoint)){
selectedShape = shapes[i];
lastMousePosition = mousePoint;
break;
}
}
}
@Override
public void mouseDragged(MouseEvent e){
if (selectedShape != null){
int dx = e.getX()- lastMousePosition.x;
int dy = e.getY()- lastMousePosition.y;
selectedShape.move(dx, dy);
lastMousePosition = e.getPoint();
repaint();
}
}
// Implement other MouseListener and MouseMotionListener methods
@Override
public void mouseClicked(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
@Override
public void mouseMoved(MouseEvent e){
}
public static void main(String[] args){
JFrame frame = new JFrame("Shape Manipulation Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.setVisible(true);
}
}
 Please help to change the below Java code so the output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!