简体   繁体   English

java.awt.Graphics绘制后更改颜色

[英]java.awt.Graphics change color after drawing

I have asked a similar question a while ago here, but didn't get an answer. 我之前在这里问过类似的问题,但没有得到答案。 The original question was about changing the color of a shape after clicking on it. 最初的问题是关于在单击形状后更改形状的颜色。 But I am puzzled on how to access the shape at all after it is drawn. 但是我对绘制后如何访问形状感到困惑。

This is my paintComponent method 这是我的paintComponent方法

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

    // moving to the middle of the panel
    g.translate(this.getWidth()/2, this.getHeight()/2);

    // painting colored arcs
    for(int i = 0; i < 4; i++) {
        g.setColor(dimColors[i]);
        g.fill(arcs[i]);            
    }

    // painting borders
    g.setColor(Color.BLACK);
    g.setStroke(new BasicStroke(5F));
    g.drawLine(-98, 0, 98, 0);
    g.drawLine(0, -98, 0, 98);      
    g.draw(circle);     

    // painting central white circle
    g.setColor(Color.WHITE);
    g.fill(smallCircle);        
    g.setColor(Color.BLACK);
    g.draw(smallCircle);    

}

the arcs[] array contains a bunch of Arc2D's that are drawn on the panel. arcs []数组包含一堆在面板上绘制的Arc2D。 My question is now, if I want to change the color of, for example arcs[0], how do I do that? 现在的问题是,如果我想更改例如arcs [0]的颜色,该怎么做?

Thanks! 谢谢!

EDIT: I now have this MouseAdapter event 编辑:我现在有此MouseAdapter事件

     private class MyMouseAdapter extends MouseAdapter {
     public void mousePressed(MouseEvent e) {

         Point p = e.getPoint();
         Component c = getComponentAt(p);

         Graphics g = c.getGraphics();

         dimColors[1] = Color.RED;

         paintComponent(g);

     }
 }

And it works, it changes the color of arc[1] because arcs[1] has dimColors[1] set as color when drawing it. 它的工作原理是更改arc [1]的颜色,因为arcs [1]在绘制时将dimColors [1]设置为颜色。

However, I still can't figure out how to check wether the right arc was clicked. 但是,我仍然不知道如何检查是否单击了正确的弧。 Right now you just click anywhere on the graphics panel and it changes the color of that specific arc 现在,您只需单击图形面板上的任意位置,它就会更改该特定弧的颜色

This doesn't answer your earlier question, however it does answer your question of click detection. 这不会回答您先前的问题,但是会回答您的点击检测问题。 To do this it is best to use Graphics2D because it is a lot easier to write than most other options. 为此,最好使用Graphics2D,因为它比大多数其他选项更容易编写。 Here is an example: 这是一个例子:

     public class GraphicsPanel extends JPanel implements MouseListener
    {   
            private Rectangle2D rect;

First we create our Graphics2D rectangle rect. 首先,我们创建Graphics2D矩形rect。

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)(g);
            g2d.setColor(Color.GREEN);
            rect = new Rectangle2D.Double(70, 70, 100, 100);
            g2d.fill(rect);
            this.addMouseListener(this);
        }

And then we override the paintComponent method and create our new Rectangle2D.Double object. 然后,我们重写paintComponent方法并创建新的Rectangle2D.Double对象。 We then fill the rectangle with g2d.fill() and then add a mouse listener to the JPanel. 然后,我们用g2d.fill()填充矩形,然后向JPanel添加鼠标侦听器。

        public void mousePressed(MouseEvent e) 
            {

               if(rect.contains(e.getX(), e.getY()))
                    System.out.println("Rectangle clicked");
            }
    }

Finally, we need to see if that rectangle contains the point where the user clicked. 最后,我们需要查看该矩形是否包含用户单击的点。 To do this, simply see if the rectangle we created contains the user's click location by using the Rectangle2D.double's contains(int x, int y) method. 为此,只需使用Rectangle2D.double的contains(int x,int y)方法查看我们创建的矩形是否包含用户的点击位置。 That's it! 而已!

if I want to change the color of, for example arcs[0], how do I do that? 如果我想更改例如arcs [0]的颜色,该怎么做?

A line (or whatever) only exists as a bunch of pixels that were painted in the original color. 一条线(或其他任何东西)仅以一堆以原始颜色绘制的像素存在。 To change its color you must change the current color and draw it again. 要更改其颜色,必须更改当前颜色并再次绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM