Search code examples
javacolorsawtpaintcomponent

How to set each rectangle color randomly with setcolor


I'm trying to make 3 different colors which are red,blue and green. My goal is when a rectangle created, i want to call the color function. The colored box should moving down, but when it placed other box, the colors preserve the orginal color. Colors shouldn't mix or change all boxes color with new box 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());

}

    }

Solution

  • The problem is g2d.setColor(BlockC); This will call a random color every time and not the color that was assigned to your dropObject at construction. You need a getter for your dropobject:

    //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;
      }
    

    Then

    g2d.setColor(o.getColor());