以鼠标单击为中心的故障图圆

时间:2018-11-03 14:14:18

标签: java swing draw

我正在尝试编写一个程序,该程序将在鼠标单击时绘制一个圆,并且圆以居中位置为中心。我的圆圈将是50 x50。在我的鼠标侦听器类中,我将x-25和y-25偏移,但是,该圆圈不会以我的点击为中心。它显示在点击下方。

public class CircleDraw extends JPanel 


private JFrame frame;
private JPanel P1;

private int circlecount;
private int x, y;
MouseClicks minney;
ArrayList<Circle> circles = new ArrayList<Circle>();

public CircleDraw()
{
    frame = new JFrame("CircleDraw");
    frame.setSize(800,800);
    minney = new MouseClicks();

    //circles.add(new Circle(x,y));//This may be the original circle being added
    this.setBackground(Color.BLACK);
    this.setPreferredSize(new Dimension(800,800));
    frame.add(this);

    frame.pack();
    frame.setVisible(true);
    frame.addMouseListener(minney);

} 

public class Circle
{
    int x, y;

    public Circle(int x, int y) 
    {
        this.x = x; this.y = y;

    }

    public void draw(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.GREEN);
        g2d.fillOval(x,y,50,50);
    }

}
@Override
protected void paintComponent(Graphics g)
{

    {
        super.paintComponent(g);
        for (Circle c : circles)
            c.draw(g);
    }
}

public class MouseClicks implements MouseListener
{
    int x, y, b;

    public void mouseClicked(MouseEvent m) 
    {
        int x = m.getX(), y = m.getY();  b = m.getButton();
        this.x = x; 
        this.y = y;


        {
            circles.add(new Circle(x-25, y-25)); 
            CircleDraw.this.repaint();


        }

    }

    public void mouseEntered(MouseEvent m) {}

    public void mouseExited(MouseEvent m) {}

    public void mousePressed(MouseEvent m) {}

    public void mouseReleased(MouseEvent m) {}
}
}

1 个答案:

答案 0 :(得分:1)

您正在将MouseListener添加到错误的组件。在这里:

frame.addMouseListener(minney);

您要将其添加到JFrame中,当您需要将鼠标坐标添加到实际绘制图形的组件中时,它将将鼠标坐标向下转换JFrame标题栏的距离。 },以便鼠标按下的坐标与图形坐标相匹配:

this

或明确

addMouseListener(minney);

例如,使用可拖动的圆圈与您的代码类似:

this.addMouseListener(minney);

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class CircleDraw2 extends JPanel {
    private static final int CIRCLE_RADIUS = 25;
    private static final Color BACKGROUND = Color.BLACK;
    private int prefW;
    private int prefH;
    List<Circle2> circles = new ArrayList<>();

    public CircleDraw2(int prefW, int prefH) {
        this.prefW = prefW;
        this.prefH = prefH;
        setBackground(BACKGROUND);
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(prefW, prefH);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        for (Circle2 circle : circles) {
            circle.draw(g2);
        }
    }

    private class MyMouse extends MouseAdapter {
        private Point p0 = null;
        private Circle2 selection = null;

        @Override
        public void mousePressed(MouseEvent e) {
            for (int i = circles.size() - 1; i >= 0; i--) {
                if (circles.get(i).contains(e.getPoint())) {
                    // if mouse clicks on a circle that already exists
                    p0 = e.getPoint(); 
                    selection = circles.get(i); // select that pressed circle

                    // move it to the top
                    circles.remove(selection);
                    circles.add(selection);
                    repaint();
                    return;
                }
            }

            // no circle selected, so create a new one
            int centerX = e.getX();
            int centerY = e.getY();
            Color c = createRandomColor();
            selection = new Circle2(centerX, centerY, CIRCLE_RADIUS, c);
            circles.add(selection);
            p0 = e.getPoint();
            repaint();
        }

        private Color createRandomColor() {
            double minRand = 0.7; // min saturation and brightness
            float hue = (float) Math.random();
            float saturation = (float)((1 - minRand) * Math.random() + minRand);
            float brightness = (float)((1 - minRand) * Math.random() + minRand);
            Color c = Color.getHSBColor(hue, saturation, brightness);
            return c;
        }

        private void drag(Point p1) {
            if (p0 == null) {
                return;
            }
            selection.move(p0, p1);
            p0 = p1;
            repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            drag(e.getPoint());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            drag(e.getPoint());
            p0 = null;
            selection = null;
        }
    }

    private static void createAndShowGui() {
        int width = 800;
        int height = width;
        CircleDraw2 mainPanel = new CircleDraw2(width, height);

        JFrame frame = new JFrame("CircleDraw2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}