【问题标题】:java.awt.Graphics change color after drawingjava.awt.Graphics 绘制后改变颜色
【发布时间】:2011-11-20 13:09:35
【问题描述】:

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

这是我的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] 设置为颜色。

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

【问题讨论】:

    标签: java swing awt java-2d paintcomponent


    【解决方案1】:

    这并不能回答您之前的问题,但它确实回答了您的点击检测问题。为此,最好使用 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) 方法查看我们创建的矩形是否包含用户的点击位置。就是这样!

    【讨论】:

      【解决方案2】:

      如果我想改变例如 arcs[0] 的颜色,我该怎么做?

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

      【讨论】:

      • 我编辑了我的主要帖子。谢谢我现在可以重绘弧线,但我不知道如何检查我点击了哪个...你知道吗?谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多