Reputation: 51
I am trying to complete the resizing of a shape task I almost got the result I wanted, only dragging after the mouse changes to a resize cursor first draws another shape of the same type and resizes this Second drawn shape. Resulting in such a picture :
This is my related code:
@Override
public void mouseMoved(MouseEvent e) {
if (e.isControlDown()) {
updateShapeUnderMouse(e.getX(), e.getY());
} // deals with drawing shape if control button held
//deals with identifying shape to resize
int x = e.getX();
int y = e.getY();
for (int i = myShapes.size() - 1; i >= 0; i--) {
Shape s = (Shape) myShapes.get(i);
if (s.isedgePoint(x, y)) {
ShapetoResize = s;
setCursor(crnw);
prevDragX = x;
prevDragY = y;
return;
}
}
@Override
public void mouseDragged(MouseEvent event) {
if (event.isControlDown()) {
if (shapeUnderMouse != null) {
shapeUnderMouse.setXPos(event.getX());
shapeUnderMouse.setYPos(event.getY());
repaint();
}
} // deals with moving the shape
//deals with identifying and resizing shape
int x = event.getX();
int y = event.getY();
if (ShapetoResize != null) {
if (ShapetoResize instanceof Square) {
ShapetoResize.resizeSE(x - prevDragX, y - prevDragY);
} else if (ShapetoResize instanceof Rectangle) { // SAME CODE FOR EACH SHAPE
}
repaint();
}
}
Any idea what might be happening?
public ArrayList<Shape> myShapes = new ArrayList();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int length = myShapes.size();
for (int i = 0; i < length; i++) {
myShapes.get(i).paint(g);
}
}
Upvotes: 4
Views: 2818
Reputation: 3589
What is the logic behind adding a shape? Is a shape added by left clicking the drawing canvas? If so, your problem is probably that a shape is being added on click and that's the one that's being resized. You can debug this by breaking execution after you drag an object. Your list would contain two shapes instead of one.
Upvotes: 1
Reputation: 6475
Without seeing this portion of the code, it's just a guess, but I'm thinking you're creating a new shape in your ShapetoResize.resizeSE(int x, int y);
method, thus creating two shapes.
I'm assuming it's your own method, because there's no resizeSE
available on the Rectangle
or the interface Shape
(the two built in shapes). Of course there's no paint
method on the interface Shape
, so you're probably using a custom interface as well (which would be confusing since you didn't provide code for this Shape interface). If you post the code for these methods we can confirm.
Here's an example of putting your code together in a working manner (and a SSCCE). I limited this only to Rectangles (since they're the only built in Shape
) and stripped out a lot of stuff not related to resizing. If you're still having trouble, try recreating the problem using this example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class ShapeResizer extends Box{
Dimension preferredSize = new Dimension(400,300);
public ArrayList<Shape> myShapes = new ArrayList();
//Shape that's targeted for editing
Shape currentShape;
public ShapeResizer(){
super(BoxLayout.X_AXIS);
//Shapes (because I don't want to make write code for the user to make shapes)
myShapes.add(new Rectangle(100, 100, 20, 20));
myShapes.add(new Rectangle(200, 200, 30, 30));
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
//deals with identifying shape to resize
int x = e.getX();
int y = e.getY();
boolean foundShape = false;
for (int i = myShapes.size() - 1; i >= 0; i--) {
Shape s = (Shape) myShapes.get(i);
if (s.contains(e.getPoint())) {
//We found a shape to target
currentShape = s;
foundShape = true;
}
}
if(!foundShape){
//Reset the shape and cursor only if needed
if(currentShape != null){
currentShape = null;
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
} else {
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent event) {
if (currentShape != null) {
resizeShape(currentShape, event.getPoint());
}
repaint();
}
});
}
public void resizeShape(Shape s, Point p){
if(s instanceof Rectangle){
Rectangle r = (Rectangle)s;
r.setSize(p.x - r.x, p.y - r.y);
}
}
public void drawShape(Graphics g, Shape s){
if(s instanceof Rectangle){
Rectangle r = (Rectangle)s;
g.drawRect(r.x, r.y, r.width, r.height);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int length = myShapes.size();
for (int i = 0; i < length; i++) {
drawShape(g, myShapes.get(i));
}
}
public Dimension getPreferredSize(){
return preferredSize;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ShapeResizer());
frame.validate();
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 3