Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff3d40fc1116b46324cbc94cdf3f153e7ccc4d3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2012 tieto deutschland gmbh (http://www.tieto.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Thomas Jung (initial contribution)
 *    Thomas Schuetz
 *******************************************************************************/ 


package org.eclipse.etrice.tutorials.simulators.trafficlight;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;


public class TrafficLight2 extends Canvas {
	private static final long serialVersionUID = 1L;
	// Traffic light has 4 states
	public final int RED = 0;
	public final int GREEN = 1;
	public final int OFF = 2;
	
	private int state;
	
	public TrafficLight2(int state){
		this.state = state;
	}
	public TrafficLight2(){
		this.state = 0;
	}
	
	private int singleLightSize = 20;
	private int xPosition = 5;
	private int gap = 5;
	
	public void paint(Graphics g){
		Color lightRed = new Color(255,0,0);
		Color darkRed = new Color (127,0,0);
		Color lightGreen = new Color (0,255,0);
		Color darkGreen = new Color (0,127,0);
		
		Color colors [][] = {
				{lightRed,darkGreen},
				{darkRed,lightGreen},
				{darkRed,darkGreen}
		};
		
		// set Background
		g.setColor(Color.lightGray);
		g.fillRect(0,0,getSize().width,getSize().height);
		// draw boarder
		g.setColor(Color.BLACK);
		g.drawRect(0,0,getSize().width-1,getSize().height-1);
		
		for (int i = 0; i<2; i++) {
			g.setColor(colors[state][i]);
			g.fillOval(xPosition,i*singleLightSize+gap,singleLightSize,singleLightSize);
			g.setColor(Color.black);
			g.drawOval(xPosition,i*singleLightSize+gap,singleLightSize-1,singleLightSize-1);
		}
	}

	 public Dimension getPreferredSize(){
	     return new Dimension(singleLightSize + 2 * gap, singleLightSize * 2 + 2 * gap);
	   }
	 
	   public Dimension getMinimumSize()
	   {
	     return new Dimension(singleLightSize + 2 * gap, singleLightSize * 2 + 2 * gap);
	   }	

	   public void setState(int state)
	   {
		   // avoid array error
		   if (state < 3){
			   this.state = state;
			   repaint();
		   }
	   }	

}

Back to the top