仅在鼠标悬停时填充圆点

时间:2015-12-07 22:03:33

标签: java graphics2d

我有以下两段代码,目前允许我突出显示多边形各点之间的圆圈。问题是在我的鼠标离开圆圈之后它们仍然被填满。这里有什么简单的东西吗?谢谢!

    public void mouseMoved(MouseEvent e)
    {
    Graphics2D g2d = (Graphics2D) getGraphics();
    if (mode == MODIFY)
    // in modify mode only
    {
    x1 = e.getX();
    y1 = e.getY();
    shapeList.get(selindex).fillPoint(x1,y1,g2d);
    x2 = e.getX();
    y2 = e.getY();
    if (x1 == x2 && y1 == y2)
    {}
    else
    repaint();
    }
    } 





    public void fillPoint(int x, int y, Graphics2D g)
    {
    for (int t =0; t < npoints;t++)
    {
    if (thePoints.get(t).contains(x,y))
    g.fill(thePoints.get(t));
    }
    }

    public void draw(Graphics2D g)
        {
            // Implement this method to draw the MyPoly onto the Graphics2D argument g.
            // See MyRectangle2D.java for a simple example of doing this.  In the case of
            // this MyPoly class the method is more complex, since you must handle the
            // special cases of 1 point (draw only the point circle), 2 points (draw the
            // line) and the case where the MyPoly is selected.  You must also use the
            // color of the MyPoly in this method.

            /*if(highlighted) // this method fills all the circles when selected - a backup piece of code if I couldnt get the proper implimentation to work
            {
                for (int t =0; t < thePoints.size(); t++)
                {
                    g.fill(thePoints.get(t));
                }
            }*/



            if (thePoints.size() <=2)
            {
                g.draw(this);
                for (int i =0; i <thePoints.size();i++ )
                {
                    g.draw(thePoints.get(i));
                }
            }

            g.setColor(myColor);
            if (highlighted)
            {
                g.draw(this);
                for (int i =0; i <thePoints.size();i++ )
                {
                    g.draw(thePoints.get(i));
                }
            }
            else if (!highlighted)
            {
                if (thePoints.size()>2)
                    g.fill(this);
                else
                    g.draw(this);
                g.fill(this);
            }
        }

public void paintComponent (Graphics g) // Method to paint contents of panel
        {
            super.paintComponent(g);  // super call needed here
            Graphics2D g2d = (Graphics2D) g;
            for (int i = 0; i < shapeList.size(); i++)
            {
                shapeList.get(i).draw(g2d);  // IMPLEMENT: draw().  This method will utilize
                        // the predefined Graphics2D methods draw() (for the outline only,
                        // when the object is first being drawn or it is selected by the user) 
                        // and fill() (for the filled in shape) for the "basic" Polygon
                        // but will require additional code to draw the enhancements added
                        // in MyPoly (ex: the circles indicating the points in the polygon
                        // and the color).  Also special cases for MyPoly objects with only
                        // 1 or 2 points must be handled as well. For some help with this see
                        // handout MyRectangle2D
            }
        }

1 个答案:

答案 0 :(得分:1)

建议:

  • 从MouseMotionListener中获取图形。
  • 相反,你想要在MouseMotionListener中做的所有事情都是:
    • 取消突出显示所有要点
    • 然后标记为突出显示(不确定根据您的代码如何执行此操作)任何选定的点或包含鼠标点的点。
    • 然后调用repaint()。 - 始终在mouselistener中调用重绘。

我建议你有几个列表,包括可以容纳你的椭圆的点,以及用于保存你的线的线。你还需要一个Shape变量来保存突出显示的椭圆,比如叫highlightedOval

private List<Shape> thePoints = new ArrayList<>();
private List<Shape> lines = new ArrayList<>();
private Shape highlightedOval = null;

然后在MouseMotionListener中,您可以保持简单,并且&#34;取消选择&#34;首先是突出显示的椭圆,然后在for循环中选择它,如果椭圆包含MouseEvent的点。然后拨打repaint()

    @Override
    public void mouseMoved(MouseEvent e) {
        highlightedOval = null;
        for (Shape oval : thePoints) {
            if (oval.contains(e.getPoint())) {
                highlightedOval = oval;
            }
        }
        repaint();
    }

例如:

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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class HighlightPolygon extends JPanel {
    private static final Color LINE_COLOR = Color.green;
    private static final double OVAL_RAD = 12;
    private static final Color HIGHLIGHTED_OVAL_COLOR = Color.RED;
    private static final Color OVAL_COLOR = Color.PINK;
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;

    private List<Shape> thePoints = new ArrayList<>();
    private List<Shape> lines = new ArrayList<>();
    private Shape highlightedOval = null;

    public HighlightPolygon(List<Point> points) {
        double w = 2 * OVAL_RAD;
        double h = w;
        for (int i = 0; i < points.size(); i++) {
            int x1 = points.get(i).x;
            int y1 = points.get(i).y;
            double x = x1 - OVAL_RAD;
            double y = y1 - OVAL_RAD;
            thePoints.add(new Ellipse2D.Double(x, y, w, h));

            int i2 = i + 1;
            i2 %= points.size();
            int x2 = points.get(i2).x;
            int y2 = points.get(i2).y;

            lines.add(new Line2D.Double(x1, y1, x2, y2));
        }

        MyMouse myMouse = new MyMouse();
        addMouseMotionListener(myMouse);
        // addMouseListener(myMouse);
    }

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

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        // to give smooth graphics
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // draw all the ovals (if we want them under the lines
        for (Shape oval : thePoints) {

            // if our oval is the selected one, fill it with the highlighted color, 
            // otherwise the regular
            Color c = oval == highlightedOval ? HIGHLIGHTED_OVAL_COLOR : OVAL_COLOR;
            g2.setColor(c);
            g2.fill(oval);
        }
        g2.setColor(LINE_COLOR);
        for (Shape line : lines) {
            g2.draw(line);
        }
    }

    private class MyMouse extends MouseAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {
            highlightedOval = null;
            for (Shape oval : thePoints) {
                if (oval.contains(e.getPoint())) {
                    highlightedOval = oval;
                }
            }
            repaint();
        }
    }

    private static void createAndShowGui() {
        List<Point> points = new ArrayList<>();
        points.add(new Point(100, 100));
        points.add(new Point(300, 200));
        points.add(new Point(500, 100));
        points.add(new Point(400, 300));
        points.add(new Point(500, 500));
        points.add(new Point(300, 400));
        points.add(new Point(100, 500));
        points.add(new Point(200, 300));

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}