如何使用setcolor随机设置每个矩形的颜色

vql8enpb  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(621)

我正在试着做三种不同的颜色,红色,蓝色和绿色。我的目标是在创建矩形时,我想调用color函数。有颜色的盒子应该向下移动,但当它放置其他盒子时,颜色保持原来的颜色。颜色不应该混合或改变所有的盒子颜色与新的盒子颜色。

package proje2p;

    import java.awt.Color;
    import java.awt.Rectangle;
    import java.util.Random;

    public class DropObject {

  private int yukseklik;
  private int block;
  public static Color r;
  private static int sayac=-1;
  private Rectangle object;

  public DropObject(int yukseklik,int x,int y,int size,Color r)
  {   

    EventRect.RColor();
        block=control();
    this.yukseklik=yukseklik-block;
    this.object=new Rectangle(x,y,size,size);

   }

public int getYukseklik(){
     return yukseklik;
}
public void setYukseklik(int yukseklik){
    this.yukseklik=yukseklik;
}
public Rectangle getObject(){
    return object;
}
public void setObject(Rectangle object){
    this.object=object;
}

private int control(){
    return (++sayac)*20;
}

    }
package proje2p;

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D; 
    import java.awt.GridLayout;
    import java.awt.Rectangle;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    import java.util.Random;

    public class EventRect extends JPanel {

private static int yukseklik=140;
private  static Color BlockC;
private DropObject koordinat;
private List<DropObject> objects=new LinkedList<DropObject>();
private LinkedList<TimerTask> tasklist=new LinkedList<TimerTask>();

public EventRect(){
    //RColor();
    System.out.println("Drop Kurucu fonksiyon");
    koordinat=new DropObject(yukseklik,0,-20,20,BlockC);
    objects.add(koordinat); 
    startSampling();
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d=(Graphics2D)g;
    System.out.println("paintcomponent");
    for(DropObject o : objects){
        Rectangle r=o.getObject();
        g2d.setColor(BlockC);
        g2d.fillRect(r.x, r.y, r.width+1, r.height+1);

    }

}

public void moveRectangle(){
    for(int i=(objects.size()-1);i>=0;i--){
        DropObject o=objects.get(i);
        Rectangle r=o.getObject();
        if(o.getYukseklik()>=0){
            if(r.y<o.getYukseklik()){
                r.y+=r.height;
                o.setObject(r);
            }else{
                o.setYukseklik(o.getYukseklik()-r.height);
            }

        }

        if(r.y ==o.getYukseklik()){
            objects.add(new DropObject(yukseklik,0,-20,20,BlockC));

        }
    }

}

void startSampling(){
    TimerTask task=new TimerTask(){
        public void run(){
            moveRectangle();
            repaint();

        }
    };
    Timer timer=new Timer();
    timer.scheduleAtFixedRate(task,0,500);
    tasklist.add(task);

}

void stopSampling(){
    if(tasklist.isEmpty()){
        return;
    }
    tasklist.removeFirst().cancel();
}

public static Color RColor(){
    Color [] Array={Color.red,Color.green,Color.blue};
    Random random=new Random();
    BlockC=Array[random.nextInt(Array.length)];
     return BlockC;
} 

    }
package proje2p;

    import java.awt.Container;
    import java.awt.GridLayout;

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class Main implements Runnable{

    @Override 
    public void run(){

JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container=frame.getContentPane();
container.setLayout(new GridLayout(0,1));
container.add(new EventRect());

frame.setSize(100,200);
frame.setVisible(true);

    }
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Main());

}

    }
ncgqoxb0

ncgqoxb01#

问题是 g2d.setColor(BlockC); 这将每次调用随机颜色,而不是指定给 dropObject 在施工中。您的dropobject需要一个getter:

//public static Color r; //Should not be static, all DropObjects will have the same color
private Color r;

public DropObject(int yukseklik,int x,int y,int size,Color r){   

  this.r =EventRect.RColor();
  block=control();
  this.yukseklik=yukseklik-block;
  this.object=new Rectangle(x,y,size,size);

  public Color getColor(){
    return this.r;
  }

然后

g2d.setColor(o.getColor());

相关问题