java - 更改 JPanel java 中形状列表的颜色

标签 java swing shapes paintcomponent

我在 JPanel 上绘制了一个形状列表,我需要更改包含通过鼠标单击获得的点的形状的颜色,但我真的不明白如何使用 repaint() 来做到这一点。

这是测试类:

public class TestShapeViewer
{    
    public static void main(String[] args)
    {
        List<Shape> figure = new ArrayList<Shape>();
        Utils util = new Utils();            

        figure.add(new Rect(new P2d(200,200), new P2d(90,40)));
        figure.add(new Circle(new P2d (150,150), new P2d (300,150)));

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowWindow(figure);
            }
        });
    }

    private static void createAndShowWindow(List<Shape> figure)
    {
        JFrame window = new JFrame("TestShapeViewer");
        window.setSize(500,500);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.add(new Viewer(figure));
        window.pack();

        window.setVisible(true);
    }    
}

这是我的 JPanel 类

public class Viewer extends JPanel implements ShapeViewer
{
    private List<Shape> shapesToDraw;
    private List<Shape> shapeToColor;
    private Utils utility = new Utils();
    private Color colorToUse;

    public Viewer(List<Shape> shapes)
    {
        this.shapesToDraw = shapes;
        this.colorToUse = Color.BLACK;

        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e) {
                repaintShapes(e.getX(), e.getY());
            }
        });
    }

    public void update(List<Shape> shapes)
    {
        this.shapesToDraw = shapes;
    }

    public Dimension getPreferredSize()
    {
        return new Dimension(500,500);
    }

    public void paintComponent(Graphics shape)
    {
        super.paintComponent(shape);
        shape.setColor(colorToUse);

        shapesToDraw.stream().forEach(x -> DrawShape(shape,x));
    }

    public void DrawShape(Graphics shape, Shape s)
    {
        BBox box = s.getBBox();

        if (s instanceof Line)
        {
            shape.drawLine(box.getUpperLeft().getX(), 
                           box.getUpperLeft().getY(), 
                           box.getBottomRight().getX(),
                           box.getBottomRight().getY()); 
        }
        else if(s instanceof Rect)
            {
                int[] xpoints = {box.getUpperLeft().getX(),
                                 box.getBottomRight().getX(),
                                 box.getBottomRight().getX(),
                                 box.getUpperLeft().getX()};
                int[] ypoints = {box.getUpperLeft().getY(),
                                 box.getUpperLeft().getY(),
                                 box.getBottomRight().getY(),
                                 box.getBottomRight().getY()};
                Polygon rect = new Polygon(xpoints, ypoints, 4);
                shape.drawPolygon(rect);
            }
            else if(s instanceof Circle)
                {
                    int raggio = (int)(new Line(box.getUpperLeft(),
                                       new P2d(box.getBottomRight().getX(),
                                           box.getUpperLeft().getY())).getPerim())/2;

                    shape.drawOval(box.getUpperLeft().getX(),
                                   box.getUpperLeft().getY(),
                                   raggio*2, raggio*2);
                }
                else if(s instanceof Combo)
                {
                    Combo co = (Combo) s;
                    co.getComp().stream().forEach(x -> DrawShape(shape,x));
                }
    }

    private void repaintShapes(int x, int y)
    {
        shapeToColor = utility.getContaining(shapesToDraw,new P2d(x,y));
        this.colorToUse = Color.RED;
        repaint();
    }
}

我的目的是单击一个点,检查哪些形状包含该点并仅更改其颜色。

为了清楚起见:

  • P2d 是一个标识点 (x,y) 的类
  • Utils.getContaining() 是一种返回包含点的形状列表的方法
  • BBox 是包含形状的最小边界框

最佳答案

I have a list of Shape drawn on a JPanel and I need to change the color of those which contain a point

而不是保留形状列表。您可以保留“ColoredShapes”列表。您将创建一个包含两个属性的自定义类:1) 形状,2) 颜色,并将该对象添加到列表中。当然,在绘制Shape之前,您还需要修改绘制代码以获取Color信息。

然后,当您使用鼠标单击某个点时,您将遍历列表以查找包含鼠标点的任何形状,并更新颜色。然后,您只需在自定义绘画面板上调用 repaint() 即可。

查看 Custom Painting Approaches 中的 DrawOnComponent 示例。此方法使用“ColoredRectangle”来跟踪这两个属性,因此该方法与您需要的非常相似。

关于java - 更改 JPanel java 中形状列表的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29602091/

相关文章:

java - 同步无 volatile

java - 在java中更改系统托盘图标

Java - [Var Type] 无法解析

java - 从 2 个特定点创建一个矩形

JavaFX : how to design a chess table with event handler on each square

html - 带有条纹背景和顶部三 Angular 形切口的形状

java - 我需要在 arraylist 中找到一个整数数据?

java - 当 jar 文件被删除时优雅地关闭 jvm

java - 如何制作重叠在任务栏上的 Windows 10 应用程序?

触发 addMouseListener 事件后 Java GUI 未完全加载