Answered step by step
Verified Expert Solution
Link Copied!

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 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);
// Use appropriate drawing methods for each shape
if (color == Color.RED){
g.fillOval(x, y, width, height); // Draw circle for red shape
} else if (color == Color.YELLOW){
g.fillRect(x, y, width, height); // Draw square for yellow shape
} else if (color == Color.BLUE){
int[] xPoints ={x, x + width /2, x + width};
int[] yPoints ={y + height, y, y + height};
g.fillPolygon(xPoints, yPoints, 3); // Draw triangle for blue shape
}
}
public boolean contains(Point point){
// Implement logic to check if point is inside the shape based on its type
if (color == Color.RED){
return Math.pow(point.x - x - width /2,2)+ Math.pow(point.y - y - height /2,2)<= Math.pow(width /2,2);
} 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 move(int 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[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;
Shape temp = shapes[i];
for (int j = i; j < shapes.length -1; j++){
shapes[j]= shapes[j +1];
}
shapes[shapes.length -1]= temp;
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
//...
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(){
@Override
public void mouseMoved(MouseEvent e){
}
@Override
public void mouseClicked(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
};
frame.add(panel);
frame.setVisible(true);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Ensure continued excellence in people management.

Answered: 1 week ago

Question

Enhance the international team by recruiting the best people.

Answered: 1 week ago