Search code examples
javaswinguser-interfacepaintdrag

Drag a shape from right to left in java swing


Hi I'm working now on a paint shapes program which you can select the shape then drag it to make a circle for example with the size you want . However I face one problem which is that I just can drag from left to right not from right to left .

see picture enter image description here

and this my panel draw I think the problem is in mouseDragged

class DrawPanel extends JPanel {

    MouseMotionListener m2 = new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseDragged(MouseEvent e) {

            Point newPoint = new Point(e.getX(), e.getY());

            if (shape == "1") {

                image = cloneImage(originalImage);
                int width = newPoint.x - oldPoint.x;
                int hieght = newPoint.y - oldPoint.y;

                drawCir(oldPoint.x, oldPoint.y, width, width);

                repaint();
            }

            else if (shape == "2") {
                image = cloneImage(originalImage);
                int width = newPoint.x - oldPoint.x;
                int hieght = newPoint.y - oldPoint.y;

                drawOval(oldPoint.x, oldPoint.y, width, hieght);
            }

            else if (shape == "3") {
                image = cloneImage(originalImage);
                int width = newPoint.x - oldPoint.x;
                int hieght = newPoint.y - oldPoint.y;

                drawRec(oldPoint.x, oldPoint.y, width, hieght);
            }

            else if (shape == "4") {
                image = cloneImage(originalImage);
                int width = newPoint.x - oldPoint.x;
                int hieght = newPoint.y - oldPoint.y;

                drawSqu(oldPoint.x, oldPoint.y, width, width);
            }


            else if (shape == "5") {
                image = cloneImage(originalImage);


                drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y);
            }

        }
    };
    {

        this.addMouseMotionListener(m2);
    }

    MouseListener m1 = new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {

            oldPoint = new Point(e.getX(), e.getY());

            originalImage = cloneImage(image);

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    };
    {

        this.addMouseListener(m1);
    }

    public DrawPanel() {

        setBackground(Color.white);

    }

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

        Graphics2D g2d = (Graphics2D) g;

        if (image == null) {
            image = new BufferedImage(super.getWidth(), super.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);
        }

        g2d.drawImage(image, 0, 0, null);

    }

    public void drawCir(int x, int y, int w, int h) {

        Graphics2D g = image.createGraphics();

        g.setColor(Color.black);

        if (drawSetting.getColor().equals("Black")) {
            g.setColor(Color.black);

        }

        else if (drawSetting.getColor().equals("Blue")) {
            g.setColor(Color.BLUE);

        }

        else if (drawSetting.getColor().equals("Red")) {
            g.setColor(Color.RED);

        }

        else if (drawSetting.getColor().equals("Green")) {
            g.setColor(Color.GREEN);

        }

        if (drawSetting.getFilled() == false) {
            g.drawOval(x, y, w, w);
        } else {
            g.fillOval(x, y, w, w);
        }

        repaint();
    }

    public void drawOval(int x, int y, int w, int h) {

        Graphics2D g = image.createGraphics();

        g.setColor(Color.black);

        if (drawSetting.getColor().equals("Black")) {
            g.setColor(Color.black);

        }

        else if (drawSetting.getColor().equals("Blue")) {
            g.setColor(Color.BLUE);

        }

        else if (drawSetting.getColor().equals("Red")) {
            g.setColor(Color.RED);

        }

        else if (drawSetting.getColor().equals("Green")) {
            g.setColor(Color.GREEN);

        }

        if (drawSetting.getFilled() == false) {
            g.drawOval(x, y, w, h);
        } else {
            g.fillOval(x, y, w, h);
        }

        repaint();
    }

    public void drawRec(int x, int y, int w, int h) {

        Graphics2D g = image.createGraphics();
        if (drawSetting.getColor().equals("Black")) {
            g.setColor(Color.black);

        }

        else if (drawSetting.getColor().equals("Blue")) {
            g.setColor(Color.BLUE);

        }

        else if (drawSetting.getColor().equals("Red")) {
            g.setColor(Color.RED);

        }

        else if (drawSetting.getColor().equals("Green")) {
            g.setColor(Color.GREEN);

        }

        if (drawSetting.getFilled() == false) {
            g.drawRect(x, y, w, h);
        } else {
            g.fillRect(x, y, w, h);
        }

        repaint();
    }

    public void drawSqu(int x, int y, int w, int h) {

        Graphics2D g = image.createGraphics();
        if (drawSetting.getColor().equals("Black")) {
            g.setColor(Color.black);

        }

        else if (drawSetting.getColor().equals("Blue")) {
            g.setColor(Color.BLUE);

        }

        else if (drawSetting.getColor().equals("Red")) {
            g.setColor(Color.RED);

        }

        else if (drawSetting.getColor().equals("Green")) {
            g.setColor(Color.GREEN);

        }

        if (drawSetting.getFilled() == false) {
            g.drawRect(x, y, w, w);
        } else {
            g.fillRect(x, y, w, w);
        }

        repaint();
    }

    public void drawLine(int x1, int y1, int x2, int y2) {

        Graphics2D g = image.createGraphics();
        if (drawSetting.getColor().equals("Black")) {
            g.setColor(Color.black);

        }

        else if (drawSetting.getColor().equals("Blue")) {
            g.setColor(Color.BLUE);

        }

        else if (drawSetting.getColor().equals("Red")) {
            g.setColor(Color.RED);

        }

        else if (drawSetting.getColor().equals("Green")) {
            g.setColor(Color.GREEN);

        }

        if (drawSetting.getFilled() == false) {
            g.drawLine(x1, y1, x2, y2);
        } else {
            g.drawLine(x1, y1, x2, y2);
        }

        repaint();
    }

    public void setShape(String s) {
        shape = s;
    }

    private BufferedImage cloneImage(BufferedImage image2) {

        if (image2 == null) {
            return null;
        }

        ColorModel cm = image2.getColorModel();
        boolean isAplpha = cm.isAlphaPremultiplied();
        WritableRaster raster = image2.copyData(null);

        return new BufferedImage(cm, raster, isAplpha, null);

    }

}

Solution

  • You cannot use negative width and height values with those drawing functions.

    Instead, you must detect a negative width, and adjust the starting coordinate to keep the width non-negative.

    public void mouseDragged(MouseEvent e) {
    
        Point newPoint = new Point(e.getX(), e.getY());
        int xStart = oldPoint.x;
        int yStart = oldPointy;
        int width = newPoint.x - xStart;
        int height = newPoint.y - yStart;
    
        if (width < 0) {
            width = -width;
            xStart -= width;
        }
        if (height < 0) {
            height = -height;
            yStart -= height;
        }
    
        if (shape.equals("1")) {
            image = cloneImage(originalImage);
            drawCir(xStart, yStart, width, height);
            repaint();
        }
        ...etc...