-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from Divyeshhhh/reflectionmode
Implement reflection mode
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/com/github/creme332/controller/canvas/transform/Reflector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.github.creme332.controller.canvas.transform; | ||
|
||
import javax.swing.JLabel; | ||
import javax.swing.JOptionPane; | ||
import javax.swing.JPanel; | ||
import javax.swing.JTextField; | ||
|
||
import com.github.creme332.model.AppState; | ||
import com.github.creme332.model.Mode; | ||
import com.github.creme332.model.ShapeWrapper; | ||
import com.github.creme332.view.Canvas; | ||
|
||
/** | ||
* Controller responsible for reflection mode. | ||
*/ | ||
public class Reflector extends AbstractTransformer { | ||
|
||
public Reflector(AppState app, Canvas canvas) { | ||
super(app, canvas); | ||
} | ||
|
||
@Override | ||
public void handleShapeSelection(int shapeIndex) { | ||
// A copy of the shape selected | ||
final ShapeWrapper selectedWrapperCopy = canvasModel.getShapeManager().getShapeByIndex(shapeIndex); | ||
|
||
// Request user for line of reflection (gradient and y-intercept) | ||
final double[] data = requestReflectionLine(); | ||
|
||
if (data.length != 2) { | ||
return; // Cancel operation if user input is invalid | ||
} | ||
|
||
double gradient = data[0]; | ||
double yIntercept = data[1]; | ||
|
||
// Reflect the shape using the gradient and y-intercept | ||
selectedWrapperCopy.reflect(gradient, yIntercept); | ||
|
||
// Replace old shape with the new one | ||
canvasModel.getShapeManager().editShape(shapeIndex, selectedWrapperCopy); | ||
|
||
// Repaint canvas | ||
canvas.repaint(); | ||
} | ||
|
||
@Override | ||
public boolean shouldDraw() { | ||
return getCanvasMode() == Mode.REFLECT_ABOUT_LINE; | ||
} | ||
|
||
/** | ||
* Asks user to enter the gradient and y-intercept of the line of reflection. | ||
* If input values are invalid or if the operation is canceled, null is | ||
* returned. | ||
* | ||
* @return array with gradient and y-intercept [m, b] | ||
*/ | ||
private double[] requestReflectionLine() { | ||
JTextField gradientField = new JTextField(5); | ||
JTextField yInterceptField = new JTextField(5); | ||
JPanel panel = new JPanel(); | ||
panel.add(new JLabel("Gradient (m):")); | ||
panel.add(gradientField); | ||
panel.add(new JLabel("Y-Intercept (b):")); | ||
panel.add(yInterceptField); | ||
|
||
int result = JOptionPane.showConfirmDialog(canvas, panel, "Enter Line of Reflection", | ||
JOptionPane.OK_CANCEL_OPTION, | ||
JOptionPane.PLAIN_MESSAGE); | ||
|
||
// Request focus again otherwise keyboard shortcuts will not work | ||
canvas.getTopLevelAncestor().requestFocus(); | ||
|
||
if (result == JOptionPane.OK_OPTION) { | ||
try { | ||
double m = Double.parseDouble(gradientField.getText()); | ||
double b = Double.parseDouble(yInterceptField.getText()); | ||
return new double[] { m, b }; | ||
} catch (NumberFormatException e) { | ||
JOptionPane.showMessageDialog(null, "Invalid input. Please enter numeric values."); | ||
} | ||
} | ||
return new double[] {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters