信号灯

作者在 2011-12-21 18:32:32 发布以下内容
//SignalCanvas
import java.awt.*;

public class SignalCanvas extends Canvas{
    int red,green,yellow,x,y,r;
    SignalCanvas(){
        setBackground(Color.BLUE);
    }
    public void setRed(int red){
        this.red=red;
    }
    public void setGreen(int green){
        this.green=green;
    }
    public void setYellow(int yellow){
        this.yellow=yellow;
    }
    public void setPosition(int x,int y){
        this.x=x;
        this.y=y;
    }
    
    public void setRadius(int r){
        this.r=r;
    }
    public void paint(Graphics g){
        if(red==1){
            g.setColor(Color.red);
        }else if(green==1){
            g.setColor(Color.green);
        }else if(yellow==1){
            g.setColor(Color.yellow);
        }
        g.fillOval(x,y,2*r,2*r);
    }
}
 
//SignalFrame
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SignalFrame extends Frame implements ItemListener{
    Choice choice;
    SignalCanvas signal=null;
    String itemRed="红灯",itemYellow="黄灯",itemGreen="绿灯";
    public SignalFrame(){
        choice = new Choice();
        choice.add(itemRed);
        choice.add(itemYellow);
        choice.add(itemGreen);
        choice.addItemListener(this);
        
        add(choice,BorderLayout.NORTH);
        try {
            Class cs =Class.forName("SignalCanvas");
            signal=(SignalCanvas)cs.newInstance();
            add(signal,BorderLayout.CENTER);
        }
        catch (Exception e) {
            add(new Label("您还没有编写SignalCanvas类"),BorderLayout.CENTER);
        
        }
        setBounds(100,100,360,300);
        setVisible(true);
        validate();
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

    }
    
    public void itemStateChanged(ItemEvent e){
        String item =choice.getSelectedItem(); //choice返回被选中的条目
        int w=signal.getBounds().width;
        int h=signal.getBounds().height;
        
        int m=Math.min(w,h);
        signal.setRadius(m/6);
        if(item.equals(itemRed)){
            if(signal!=null){
                signal.setRed(1);
                signal.setYellow(0);
                signal.setGreen(0);
                signal.setPosition(w/3,0);
                signal.repaint();
            }
        }
        else if(item.equals(itemYellow)){
            if(signal!=null){
                signal.setRed(0);
                signal.setYellow(1);
                signal.setGreen(0);
                signal.setPosition(w/3,h/3);
                signal.repaint();
            }
        }else if(item.equals(itemGreen)){
            if(signal!=null){
                signal.setRed(0);
                signal.setYellow(0);
                signal.setGreen(1);
                signal.setPosition(w/3,2*h/3);
                signal.repaint();
            }
        }
    }
}
 
//主程序
public class SignalMainClass{
    
    public static void main(String[] agrs){
        SignalFrame frame;
        frame=new SignalFrame();
        frame.setTitle("信号灯");
    }
}
 
java codes | 阅读 828 次
文章评论,共0条
游客请输入验证码
浏览18062次
最新评论