移动绘制的椭圆 [英] Moving an Ellipse that has been drawn

查看:74
本文介绍了移动绘制的椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,单击时我将Ellipse2D.Float添加到JPanel(和ArrayList).我想知道完成此操作后是否可以移动形状(例如,按住Shift键并单击).目前,它似乎是静态的,我无法将其拖动到任何地方.

So I add an Ellipse2D.Float to a JPanel (and to an ArrayList) when clicked. I want to know if after this has been done I can then move the shape (say, through shift-click). At the moment it seems to be static and I cannot drag it anywhere.

此外,是否可以将两条圆与一条直线永久地连接在一起,从而将两条圆与一条直线连接起来;当圆移动时,线会更改为跟随圆.

Furthermore, is it possible to connect two circles with a line in such a fashion that the line connects the two circles permenantly; when a circle is move the line is altered to follow the circles.

我只是想看看这是否可行,我已经开始编码,但是找不到如何做这两件事.任何答案,链接,代码段均表示赞赏!

I'm just looking to see if this is feasible, I have started coding but can't find out how to do these two things. Any answers, links, code snippets appreciated!

推荐答案

您已经有了基本的想法.检测鼠标单击,绘制新形状.

You've got the basic idea. Detect mouse click, paint new shape.

下一步是知道何时按下Shift键,知道最后一个(或选定的)形状是什么,然后能够更新其位置.

The next step is to know when the shift key is pressed and knowing what the last (or selected) shape was and then be able to update it's position.

对我来说,最简单的解决方案是以某种方式维护有关形状及其位置的信息.在此示例中,我使用了一个简单的Drawable类,该类不仅结合了位置和形状,还具有一个简单的draw方法来简化生活.

For me, the easiest solution would be to maintain information about the shape and it's position in some manner. In this example, I've used a simple Drawable class which not only combines the position and the shape, but also has a simple draw method to make life easier.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShiftShape {

    public static void main(String[] args) {
        new ShiftShape();
    }

    public ShiftShape() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Drawable> drawables;

        public TestPane() {
            drawables = new ArrayList<Drawable>(25);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    Drawable drawable = null;

                    if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) == MouseEvent.SHIFT_DOWN_MASK) {

                        if (!drawables.isEmpty()) {

                            drawable = drawables.get(drawables.size() - 1);

                        }

                    } else {

                        drawable = new Drawable();
                        drawables.add(drawable);

                    }

                    if (drawable != null) {

                        drawable.setLocation(e.getPoint());
                        repaint();

                    }
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Drawable drawable : drawables) {
                drawable.draw(g2d);
            }
            g2d.dispose();
        }

        public class Drawable {

            private Point location;
            private Shape shape;

            public Drawable() {

                shape = new Ellipse2D.Float(0, 0, 20, 20);

            }

            public void setLocation(Point location) {
                this.location = location;
            }

            public Point getLocation() {
                return location;
            }

            public void draw(Graphics2D g2d) {

                Point p = getLocation();
                int x = p.x - 10;
                int y = p.y - 10;
                g2d.translate(x, y);
                g2d.draw(shape);
                g2d.translate(-x, -y);

            }

        }
    }
}

这篇关于移动绘制的椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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