English 中文(简体)
java.awt。 制图后图形变化色体
原标题:java.awt.Graphics change color after drawing

我曾在这里问过一个类似的问题,但没有得到答复。 最初的问题是在点击后改变形状的颜色。 但是,我对如何在选取后完全进入形状感到困惑。

这是我的油漆对应方法。

    @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);    

}

弧阵阵阵阵列载有小组抽取的Arc2D小块。 现在的问题是,如果我想要改变例如弧[0]的颜色,我如何这样做?

感谢!

EDIT:我现在请我参加这次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);

     }
 }

而且,它改变了北极圈子的颜色,因为北极圈(北极圈)在绘画时将北极圈的颜色划为颜色。

然而,我仍然无法说明如何检查右弧是否被点击。 现在,你刚刚在制图小组的任何地方点击,这改变了该特定弧的颜色。

问题回答

这没有回答你先前的问题,但它确实回答了你点击探测的问题。 要做到这一点,最好使用图表2D,因为比大多数其他选择更容易书写。 例如:

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

首先,我们创立了我们的图西2D直径。

        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");
            }
    }

最后,我们需要看到,这一试金星是否含有用户点点。 为此,简单地看看,我们所创建的试金星是否含有用户点点,即使用Reginle2D.double 含有(int x, int y)方法。 !!

如果我想要改变例如弧形的颜色,我如何这样做?

一条线(或任何东西)只能作为原颜色涂料的粉碎板。 为了改变其肤色,你必须改变目前的颜色,并再次取而代之。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签