I have a frame with different shapes that I would like to move, but only one moves so far

Kristina Cordova :

My intensions is to be able to move three different shapes in my frame. But I can move only square so far. Any suggestions how I can make program move all my shapes, not just one? I have some methods and classes below

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseMoveScale extends JPanel {
private Rectangle2D.Float myRect = new Rectangle2D.Float(50, 50, 100, 100);
private Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
private Rectangle2D.Float myTr = new Rectangle2D.Float(50, 50, 50, 50);

MovingAdapter ma = new MovingAdapter();

public MouseMoveScale() {
    addMouseMotionListener(ma);
    addMouseListener(ma);
    addMouseWheelListener(new ScaleHandler());
}

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D square = (Graphics2D) g;
    Graphics2D triangle = (Graphics2D) g;
    Graphics2D circle = (Graphics2D) g;

    square.setRenderingHint(RenderingHints.KEY_ANTIALIASING,   RenderingHints.VALUE_ANTIALIAS_ON);
    square.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    square.setColor(new Color(0, 0, 200));
    square.fill(myRect);

    triangle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    triangle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    triangle.setColor(new Color(139, 89, 255));
    int xPoints[] = {110,180,30,110};
    int yPoints[] = {30,100,100,30};
    g.fillPolygon(xPoints, yPoints, 4);

    circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    circle.setColor(new Color(0, 0, 117));
    circle.fillOval( 10,10, 100, 100 );
}

class MovingAdapter extends MouseAdapter {

    private int x;
    private int y;

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseDragged(MouseEvent e) {

        int dx = e.getX() - x;
        int dy = e.getY() - y;


        if (myRect.getBounds2D().contains(x, y)) {
            myRect.x += dx;
            myRect.y += dy;
            repaint();
        }
        if (myTr.getBounds2D().contains(x, y)) {
            myTr.x += dx;
            myTr.y += dy;
            repaint();
        }
        if (myCr.getBounds2D().contains(x, y)) {
            myCr.x += dx;
            myCr.y += dy;
            repaint();
        }

        x += dx;
        y += dy;
    }
}

class ScaleHandler implements MouseWheelListener {
    public void mouseWheelMoved(MouseWheelEvent e) {

        int x = e.getX();
        int y = e.getY();

        if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

            if (myRect.getBounds2D().contains(x, y)) {
                float amount = e.getWheelRotation() * 5f;
                myRect.width += amount;
                myRect.height += amount;
                repaint();
            }
        }

    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Shapes World");
    MouseMoveScale m = new MouseMoveScale();
    m.setDoubleBuffered(true);
    frame.add(m);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

I probably need to create new objects? Or is it enough to fix the problem with if statement?

RR_IL :

I think this is your error

triangle.setColor(new Color(139, 89, 255));
int xPoints[] = {110,180,30,110};
int yPoints[] = {30,100,100,30};
g.fillPolygon(xPoints, yPoints, 4); ---> Notice this.

You are trying to move the rec without changing the xpoints/ypoints you have used.

   if (myTr.getBounds2D().contains(x, y)) {
        myTr.x += dx; ---> not related to xPoints
        myTr.y += dy; ---> not related to yPoints
        repaint();
    }

You can change - g.fillPolygon for e.x with -

 triangle.fill(myCr);

Our result

enter image description here

So now you can move 2 objects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291777&siteId=1