Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to make this below Java code work, that it will be animated. When the user places the mouse over a shape, holds down

Please help to make this below Java code work, that it will be animated. When the user places the mouse over a shape, holds down the button and drags the mouse, the shape should move. One solution is to simply move and redraw the figure.
The shapes should react to clicks just when the mouse is over it. You must therefore take into account that the circle and the triangle are not rectangular.
For example, it should be possible to click on the square when it is partially covered by the triangle, like the image and the Java code below : 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.)
}
public boolean contains(Point point){
// Implement logic to check if a point is inside the shape
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];
// Initialize your shapes here
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
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);
}
}
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions

Question

Choose an appropriate organizational pattern for your speech

Answered: 1 week ago

Question

Writing a Strong Conclusion

Answered: 1 week ago