Shape resizing almost works

I am trying to resize a form task, I almost got the result that I wanted, only dragging after changing the mouse to the resize cursor first draws another shape of the same type and resizes this second shape. The result of this picture:

enter image description here

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 could happen?

 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); } } 
+4
source share
2 answers

Without seeing this part of the code, this is just a guess, but I think that you are creating a new form in your ShapetoResize.resizeSE(int x, int y); method ShapetoResize.resizeSE(int x, int y); creating two forms.

I assume this is your own method, because there is no resizeSE available on the Rectangle or in the Shape interface (two built-in forms). Of course, there is no paint method on the Shape interface, so you probably also use your own interface (which would be confusing since you did not provide code for this Shape interface). If you post code for these methods, we can confirm.

Here's an example of how your code (and SSCCE) works together. I limited this to only the Rectangles (since they were the only ones built into Shape ) and removed a lot of things not related to resizing. If you still have problems, try to recreate 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 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(px - rx, py - ry); } } public void drawShape(Graphics g, Shape s){ if(s instanceof Rectangle){ Rectangle r = (Rectangle)s; g.drawRect(rx, ry, 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); } } 
+3
source

What is the logic of adding a form? Is a shape left-clicked on a drawing canvas? If so, your problem is probably that when you click, a shape is added and it changes. You can debug this by breaking execution after dragging an object. Your list will contain two forms instead of one.

+1
source

All Articles