的顏色這裏是我的代碼有問題:如何交換2個對象
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import sun.java2d.loops.DrawRect;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Board extends JPanel implements MouseListener
{
//instance variables
private int width;
private int height;
private Block topLeft;
private Block topRight;
private Block botLeft;
private Block botRight;
public Board() //constructor
{
width = 200;
height = 200;
topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
setBackground(Color.WHITE);
setVisible(true);
//start trapping for mouse clicks
addMouseListener(this);
}
//initialization constructor
public Board(int w, int h) //constructor
{
width = w;
height = h;
topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
setBackground(Color.WHITE);
setVisible(true);
//start trapping for mouse clicks
addMouseListener(this);
}
public void update(Graphics window)
{
paint(window);
}
public void paintComponent(Graphics window)
{
super.paintComponent(window);
topRight.draw(window);
topLeft.draw(window);
botRight.draw(window);
botLeft.draw(window);
}
public void swapTopRowColors()
{
Color temp = topLeft.getColor(topRight);
topRight.setColor(temp);
repaint();
}
public void swapBottomRowColors()
{
}
public void swapLeftColumnColors()
{
}
public void swapRightColumnColors()
{
}
我怎麼會掉這些「方塊」使用.getColor()
方法2的顏色?我認爲我正在實現它的正確軌道,但沒有必須像以前那樣用顏色做這樣的事情。
我不斷得到的錯誤是聲稱我需要getColor在我的Block類中,但這不應該是必要的,對嗎? –
所以也許像'私人色溫= 0,0,0;'? –
你的'Block'類是什麼樣的? – siame