在java swing中从右向左拖动形状 [英] Drag a shape from right to left in java swing

查看:41
本文介绍了在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 .

看图在此处输入图片描述

这是我的面板画我认为问题出在 mouseDragged

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);

    }

}

推荐答案

您不能在这些绘图函数中使用负的宽度和高度值.

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...

这篇关于在java swing中从右向左拖动形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆