java绘制矩形的方法不是两种方式

时间:2014-03-25 20:07:58

标签: java

嗨我在java中有一些绘制矩形的代码,但是它只会向右拖动,即使我向左拖动也向右拖动,这里的代码我有什么帮助吗?

  public void mouseDragged(MouseEvent e) {
                    Point p = e.getPoint();
                    int width = Math.max(selection.x - e.getX(), e.getX() - selection.x);
                    int height = Math.max(selection.y - e.getY(), e.getY() - selection.y);
                    selection.setSize(width, height);
                    repaint();

2 个答案:

答案 0 :(得分:3)

请注意,Rectangle(以及Graphics#fillRectGraphics#drawRect)不会呈现负宽度/高度的矩形

你需要两件事......

  1. 当前鼠标点(或您的案例中的拖动点)
  2. 首次按下鼠标的位置(锚点或原点)
  3. 你应该从mousePressed事件......

    获得锚点
    public void mousePressed(MouseEvent e) {
        clickPoint = new Point(e.getPoint());
    }
    

    然后,您需要确定哪个点最小,并将其用作起点,哪个点最大,并将其用于维度。

    public void mouseDragged(MouseEvent e) {
        int minX = Math.min(e.getX(), clickPoint.x);
        int minY = Math.min(e.getY(), clickPoint.y);
        int maxX = Math.max(e.getX(), clickPoint.x);
        int maxY = Math.max(e.getY(), clickPoint.y);
    
        selection.x = minX;
        selection.y = minY;
        selection.width = maxX - minX;
        selection.height = maxY - minY;
        repaint();
    }
    

    Selection Drag

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class SelectionExample {
    
        public static void main(String[] args) {
            new SelectionExample();
        }
    
        public SelectionExample() {
            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 Rectangle selection = new Rectangle();
            private Point clickPoint;
    
            public TestPane() {
                MouseAdapter ma = new MouseAdapter() {
    
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        int minX = Math.min(e.getX(), clickPoint.x);
                        int minY = Math.min(e.getY(), clickPoint.y);
                        int maxX = Math.max(e.getX(), clickPoint.x);
                        int maxY = Math.max(e.getY(), clickPoint.y);
    
                        selection.x = minX;
                        selection.y = minY;
                        selection.width = maxX - minX;
                        selection.height = maxY - minY;
                        repaint();
                    }
    
                    @Override
                    public void mousePressed(MouseEvent e) {
                        clickPoint = new Point(e.getPoint());
                    }
    
                };
    
                addMouseListener(ma);
                addMouseMotionListener(ma);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (selection.width > 0 && selection.height > 0) {
                    g2d.setColor(new Color(0, 0, 255, 64));
                    g2d.fill(selection);
                    g2d.setColor(Color.BLUE);
                    g2d.draw(selection);
                }
                g2d.dispose();
            }
        }
    
    }
    

答案 1 :(得分:0)

让我们做一些简单的代数。

selection.x - e.getX()

相同
- (e.getX() - selection.x)

所以你的表达是这样说的:

Math.max(- (e.getX() - selection.x), e.getX() - selection.x)

由于正面版本总是会更大,所以它总会选择正面答案,因此你永远不会得到一个负宽度/高度的矩形。