0

赤、青、緑の3色を作ってみました。私の目標は、長方形が作成されたときです。カラー関数を呼び出したいです。色付きのボックスは下に移動する必要がありますが、別のボックスを配置すると、色は元の色を保持します。すべてのボックスの色を新しいボックスの色と混ぜたり、変更したりしないでください。

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

}

    }
4

1 に答える 1