使用MouseListener绘制圆圈?

时间:2015-01-20 04:50:50

标签: java

我正在尝试编写一个程序,允许用户指定一个按下2个鼠标的圆圈,第一个在中心,第二个在外围的一个点上,我编写的程序就是这个。

import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;

public class CircleFrame extends JFrame
{
    public CircleFrame()
    {
       circle = new Ellipse2D.Double();
       hasCenter = false;

       createComponent();
       setSize(400, 400);
     }

    private void createComponent()
    {

        class CircleComponent extends JComponent
        {
            public void paintComponent(Graphics g)
            {
                Graphics2D g2 = (Graphics2D) g;
                circle.setFrame(xTop, yTop, radius*2, radius*2);
                g2.draw(circle);
            }
        }

        class MousePressListener1 extends MouseAdapter
        {
            public void mousePressed(MouseEvent event)
            {
                if(!hasCenter)
                {

                    xCenter = event.getX();
                    yCenter = event.getY();
                    hasCenter = true;
                }
            }
        }

        class MousePressListener2 extends MouseAdapter
        {
            public void mousePressed(MouseEvent event)
            {

                if (hasCenter)
                { 
                   xOut = event.getX();
                    yOut = event.getY();

                    xTop = xCenter - Math.abs(xOut - xCenter);
                    yTop = yCenter - Math.abs(yOut - yCenter);

                    radius = 
                 Math.sqrt((xOut - xCenter)*(xOut - xCenter) + (yOut - yCenter)*(yOut - yCenter));

                    hasCenter = false;

                }
            }
        }

        addMouseListener(new MousePressListener1());
        addMouseListener(new MousePressListener2());

        CircleComponent component = new CircleComponent();
        add(component);
    }

    private double xTop;
    private double yTop;
    private int xCenter;
    private int yCenter;
    private int xOut;
    private int yOut;
    private Ellipse2D.Double circle;
    private double radius;
    private boolean hasCenter;
}

这是主要的课程

import javax.swing.JFrame;

public class CircleFrameViewer
{
    public static void main(String[] args)
    {
        JFrame frame = new CircleFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

不知何故,它只显示一个空白框架,没有任何点击可以发生任何事情。

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:1)

不要使用两个侦听器,使用一个侦听器,只要发生鼠标事件,就会调用两个侦听器。

MouseEvent是生成它们的组件的上下文。这意味着您应该将您的监听器添加到CircleComponent

确保在希望更新用户界面时调用repaint

像...那样的东西

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.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test2 {

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

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

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

    public class TestPane extends JPanel {

        private Point centerPoint;
        private Shape circle;

        public TestPane() {
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (centerPoint == null) {
                        centerPoint = e.getPoint();
                        repaint();
                    } else {
                        double xTop = Math.min(e.getPoint().x, centerPoint.x);
                        double yTop = Math.min(e.getPoint().y, centerPoint.y);
                        double xBottom = Math.max(e.getPoint().x, centerPoint.x);
                        double yBottom = Math.max(e.getPoint().y, centerPoint.y);

                        double radius = Math.max(xBottom - xTop, yBottom - yTop);
                        xTop = centerPoint.x - radius;
                        yTop = centerPoint.y - radius;

                        radius *= 2;

                        circle = new Ellipse2D.Double(xTop, yTop, radius, radius);
                        repaint();
                    }
                }

            });
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (circle != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.draw(circle);
                g2d.dispose();
            }
        }

    }

}