我正在尝试制作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());
}
}
答案 0 :(得分:2)
问题是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());