在Java中动态更改区域的颜色

在Java中动态更改区域的颜色,java,graphics,Java,Graphics,早上好, 我需要动态地改变一些区域的颜色。基本上我创建了一个类Ovals,并在该类中创建了一个区域。在视图类中,我创建了一个椭圆形的ArrayList,并在列表中添加了几个区域,然后在不同的位置绘制它们。 以下是我的意思的源代码: public class Objects{ Area shape; public Objects(int x,int y){ this.shape= new Area (new Ellipse2D.Float(x, y, 70, 70)

早上好, 我需要动态地改变一些区域的颜色。基本上我创建了一个类Ovals,并在该类中创建了一个区域。在视图类中,我创建了一个椭圆形的ArrayList,并在列表中添加了几个区域,然后在不同的位置绘制它们。 以下是我的意思的源代码:

public class Objects{
    Area shape;
    public Objects(int x,int y){
        this.shape= new Area (new Ellipse2D.Float(x, y, 70, 70));
    }

    public Color randColor(){
        Color[]color = {
                Color.red,
                Color.BLUE,
                Color.cyan,
                Color.green,
                Color.pink,
                Color.black,
                Color.LIGHT_GRAY,
                Color.magenta,Color.orange,
                Color.white,Color.yellow,
                Color.darkGray
        };
     return color[randomPosition(color.length)];
    }

}

public class View extends JComponent{
        ArrayList<Objects> ob= new ArrayList<Object>();
        //stuff...

        public View()
        {
            addObjects();  
            //other stuff...
        }

        public void paintComponent(Graphics2D g2)
        {
            //if I set the color here with  the classical g2.setColor(Color.red) every object will be red
            drawOvals(g2);
        }


        public void addObjects(){
           for(int i=2;i<10;i++)
                ob.add(new Objects(i+10,100));
        }


        public void drawOvals(Graphics2D g2)
        {
            if(ob!=null){
                for(Objects o:op)
                {
                    //I waant to know for example if there is a way to set the color indipendently for each object
                    //I tried to put here: *g2.setColor(o.randColor())* but the paintComponent method is called every 10ms so the color changes very rapidly
                    g2.draw(o.shape);
                    g2.fill(o.shape);            
                }
            }
        } 
}
公共类对象{
面积形状;
公共对象(整数x,整数y){
这个形状=新区域(新椭圆2d浮动(x,y,70,70));
}
公共颜色randColor(){
颜色[]颜色={
颜色,红色,
颜色,蓝色,
颜色,青色,
颜色,绿色,
颜色,粉红色,
颜色,黑色,
颜色。浅灰色,
颜色。洋红,颜色。橙色,
颜色,白色,黄色,
彩色暗光
};
返回颜色[随机位置(颜色.长度)];
}
}
公共类视图扩展了JComponent{
ArrayList ob=新的ArrayList();
//东西。。。
公众观点()
{
addObjects();
//其他东西。。。
}
公共组件(图形2D g2)
{
//如果我在这里用经典的g2.setColor(color.red)设置颜色,每个对象都将是红色的
牵引卵形(g2);
}
public void addObjects(){

对于(int i=2;i您可以拥有与每个椭圆形相关的颜色:

public class Objects{
    Area shape;
    Color color;

    public Objects(int x,int y){
        this.shape= new Area (new Ellipse2D.Float(x, y, 70, 70));
        color = randColor();
    }
...
}
然后在你画椭圆之前:

 for(Objects o:op)
 {
     g2.setColor(o.color);
     g2.draw(o.shape);
     g2.fill(o.shape);            
 }

您可以拥有与每个椭圆形关联的颜色:

public class Objects{
    Area shape;
    Color color;

    public Objects(int x,int y){
        this.shape= new Area (new Ellipse2D.Float(x, y, 70, 70));
        color = randColor();
    }
...
}
然后在你画椭圆之前:

 for(Objects o:op)
 {
     g2.setColor(o.color);
     g2.draw(o.shape);
     g2.fill(o.shape);            
 }