colorimage.java

来自「源码为科学出版社出版的英文<java设计模式>(影印版)所用的所有例」· Java 代码 · 共 55 行

JAVA
55
字号
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.accessibility.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ColorImage extends JPanel
implements Chain      {
    private Chain nextChain;
//-----------------------------------
    public ColorImage() {
        super();
        setBorder(new LineBorder(Color.black));
    }
//-----------------------------------
    public void addChain(Chain c) {
        nextChain = c;
    }
//-----------------------------------
    public void sendToChain(String mesg) {
        Color c = getColor(mesg);
        if (c != null) {
            setBackground(c);
            repaint();
        } else {
            if (nextChain != null)
                nextChain.sendToChain(mesg);
        }
    }
//-----------------------------------
    private Color getColor(String mesg) {
        String lmesg = mesg.toLowerCase();
        Color c = null;

        if (lmesg.equals("red"))
            c = Color.red;
        if (lmesg.equals("blue"))
            c = Color.blue;
        if (lmesg.equals("green"))
            c= Color.green;
        return c;
    }
//-----------------------------------
    public Chain getChain() {
        return nextChain;
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?