2012-11-08 31 views
0

的颜色这里是我的代码有问题:如何交换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的颜色?我认为我正在实现它的正确轨道,但没有必须像以前那样用颜色做这样的事情。

回答

1

您将需要使用setColor(),但在此之前,您需要创建其中一种颜色的温度。

public void swapColors(Block g1, Block g2) { 
    Color c = g1.getColor(); 
    g1.setColor(g2.getColor()); 
    g2.setColor(c); 
    repaint(); 
} 

也是用这个方法的标题,你可以交换从Block对象两种颜色,而不需要不同的方法,每一种组合,只要通过要交换作为参数两种。

编辑:

看来你需要一个getter和setter添加为color你的格挡类,所以只需添加:

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

public void setColor(Color c) { 
    this.color = c; 
} 
+0

我不断得到的错误是声称我需要getColor在我的Block类中,但这不应该是必要的,对吗? –

+0

所以也许像'私人色温= 0,0,0;'? –

+0

你的'Block'类是什么样的? – siame

1
public void swapTopRowColors() 
{ 
    Color temp = topLeft.getColor(topRight); 
    topLeft.setColor(topRight.getColor()); //<-- line you're missing 
    topRight.setColor(temp); 
    repaint(); 
} 

===以下注释===

您需要在您的Block课程中添加吸气剂和吸气剂:

public Color getColor() { 
    return color; 
} 

public void setColor(Color color) { 
    this.color = color; 
} 
+0

我得到这个'说明\t资源\t路径\t位置\t类型 方法的getColor()是未定义的类型块\t板.java \t/Lab10_boolean/10f \t line 75 \t Java问题当我使用你的解决方案时(它对我最有意义) –

+0

你的'Block'类似乎没有'getColor()' –

+0

的方法,我只是没有编辑我的OP –

0

你有2个几乎张玉峰构造局()和董事会(H,W),苏中默认的构造函数调用:

public Board() //constructor 
{ 
    Board(200,200); 
} 

如果您使用此方法,在未来你将只需要编辑一个构造函数, 不是都。

+0

谢谢,我的方式是由于我了解它的方式 –