日期:2014-05-20 浏览次数:20747 次
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import javax.swing.JFrame;
/**
* http://bbs.csdn.net/topics/390325800
* @author Administrator
*
*/
public class ChangeColor extends JFrame{
int x;
int y;
Color aColor=Color.blue;
Color bColor=new Color(255,0,255);//紫色的RGB值
public static void main(String[] args){
new ChangeColor();
}
public void paint(Graphics g){
Graphics2D gr=(Graphics2D) g;
gr.setColor(Color.blue);
Ellipse2D.Double a=new Ellipse2D.Double(50,50,50,50); //可以 用的椅子
Ellipse2D.Double b=new Ellipse2D.Double(150,50,50,50);//不能用的,已经占用的椅子
paintAvailable(gr, a,Color.red);
paintTaken(gr,b);
}
/**
* 画已经占用的位子
* @param gr
* @param b
*/
private void paintTaken(Graphics2D gr, Double b) {
gr.setColor(bColor);
gr.draw(b);
gr.fill(b);//涂色
}
/**
* 画未被占用的位子
* @param gr
* @param a
* @param fin
*/
private void paintAvailable(Graphics2D gr, Double a,Color fin) {
if(a.contains(x,y)
------解决方案--------------------
aColor.equals(fin)){//如果可以使用
gr.setColor(fin);
aColor=fin;
}
gr.draw(a);
gr.fill(a);//涂色
}
public ChangeColor(){
setSize(500,500);
setVisible(true);
addMouseListener(new MouseListener() {//鼠标捕捉
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}