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

发布于 2024-12-17 05:40:39 字数 1528 浏览 2 评论 0原文

我不久前在这里问过类似的问题,但没有得到答案。最初的问题是关于单击形状后更改形状的颜色。但我对绘制后如何访问形状感到困惑。

这是我的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);    

}

arcs[] 数组包含一堆在面板上绘制的Arc2D。现在我的问题是,如果我想改变 arcs[0] 的颜色,我该怎么做?

谢谢!

编辑:我现在有这个 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);

     }
 }

并且它有效,它改变了 arc[1] 的颜色,因为 arcs[1] 在绘制它时将 dimColors[1] 设置为颜色。

但是,我仍然不知道如何检查是否单击了右侧弧线。现在,您只需单击图形面板上的任意位置,它就会更改该特定弧的颜色

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

    @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. My question is now, if I want to change the color of, for example arcs[0], how do I do that?

Thanks!

EDIT: I now have this MouseAdapter event

     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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-12-24 05:40:39

这并没有回答您之前的问题,但它确实回答了您的点击检测问题。为此,最好使用 Graphics2D,因为它比大多数其他选项更容易编写。这是一个例子:

     public class GraphicsPanel extends JPanel implements MouseListener
    {   
            private Rectangle2D 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);
        }

然后我们重写 PaintComponent 方法并创建新的 Rectangle2D.Double 对象。
然后,我们使用 g2d.fill() 填充矩形,然后向 JPanel 添加鼠标侦听器。

        public void mousePressed(MouseEvent e) 
            {

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

最后,我们需要查看该矩形是否包含用户单击的点。为此,只需使用 Rectangle2D.double 的 contains(int x, int y) 方法查看我们创建的矩形是否包含用户的单击位置。就是这样!

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. Here is an example:

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

First we create our Graphics2D rectangle 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.
We then fill the rectangle with g2d.fill() and then add a mouse listener to the 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. That's it!

停顿的约定 2024-12-24 05:40:39

如果我想改变弧线[0]等的颜色,我该怎么做?

一条线(或其他任何东西)仅作为一堆以原始颜色绘制的像素存在。要更改其颜色,您必须更改当前颜色并再次绘制。

if I want to change the color of, for example arcs[0], how do I do that?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文