image thumbnail

Object oriented program demo: animated traffic lights on road intersection

by

 

24 Sep 2012 (Updated )

simple classes and superclass are used to arrange flexible object oriented programming environment

trafficLamp
classdef trafficLamp
    
    properties
        diametr=[0.8, 0.8]
        g5=[0.5 0.5 0.5] %gray color / szary
        Lg, Ly, Lr
    end
    
    methods
        function obj=trafficLamp(x,y)
            obj.Lr= rectangle('Position',[x,y+2,obj.diametr],'Curvature',[1,1],'FaceColor',obj.g5,'LineWidth',4,'LineStyle','-');
            obj.Ly= rectangle('Position',[x,y+1,obj.diametr],'Curvature',[1,1],'FaceColor',obj.g5,'LineWidth',4,'LineStyle','-');
            obj.Lg= rectangle('Position',[x,y  ,obj.diametr],'Curvature',[1,1],'FaceColor',obj.g5,'LineWidth',4,'LineStyle','-');
        end
        function obj=red(obj)
            obj=gray3(obj);
            set(obj.Lr,'FaceColor','r');
        end
        function obj=green(obj)
            obj=gray3(obj);
            set(obj.Lg,'FaceColor','g');
        end
        function obj=yellow(obj)
            obj=gray3(obj);
            set(obj.Ly,'FaceColor','y');
        end
        function obj=gray3(obj)
            set(obj.Lr,'FaceColor',obj.g5);
            set(obj.Lg,'FaceColor',obj.g5);
            set(obj.Ly,'FaceColor',obj.g5);
        end
        
    end
    
end

Contact us