2012-11-08 2 views
0

의 색상을 교환하기 위해 다음 질문에 내 코드입니다 :방법이 객체

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 객체에서 두 가지 색상을 교체 할 수 있습니다.

편집 : 당신이 그래서 그냥 추가 color에 대한 블록 클래스에 getter 및 setter를 추가해야 할 것 같다

:

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

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

계속 오류가 발생하면 Block 클래스에 getColor가 있어야한다고 주장하고 있지만 그럴 필요는 없습니다. –

+0

그래서'private Color temp = 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 클래스에 getter 및 setter를 추가해야합니다.

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 자바 문제'해결책이 가장 나을 때 –

+0

'Block' 클래스가'getColor()'메소드를 가지고 있지 않은 것처럼 보입니다. –

+0

, 방금 내 OP를 편집하지 않았다 –

0

당신이 거의 indentical 생성자위원회() 및 이사회 (시간, w), 스와 기본 생성자 호출이 : 하나의 생성자를 편집 할이 방법을 사용하는 경우

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

는, 미래에 필요한 것 둘 다 아닙니다.

+0

덕분에, 내가 가진 방식은 내가 배운 방식 때문이다. –

관련 문제