View Javadoc

1   package com.jpeterson.virtlcd.swing.ui;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Container;
6   import java.awt.Dimension;
7   import java.awt.Graphics;
8   import java.awt.Insets;
9   import java.awt.LayoutManager;
10  import java.awt.Point;
11  
12  import javax.swing.JComponent;
13  import javax.swing.event.ChangeEvent;
14  import javax.swing.event.ChangeListener;
15  import javax.swing.plaf.ComponentUI;
16  
17  import com.jpeterson.virtlcd.swing.DotMatrixModel;
18  import com.jpeterson.virtlcd.swing.JDotMatrixSixByEight;
19  
20  /**
21   * Implementation of the "view" for actually displaying the basic LCD character.
22   * 
23   * @author Jesse Peterson
24   */
25  public class BasicDotMatrixSixByEightUI extends DotMatrixSixByEightUI {
26  	/**
27  	 * The associated dot matrix character.
28  	 */
29  	protected JDotMatrixSixByEight dotMatrixSixByEight;
30  
31  	protected ChangeListener dotMatrixSixByEightChangeListener;
32  
33  	protected static final int PIXEL_WIDTH = 2;
34  
35  	protected static final int PIXEL_HEIGHT = 2;
36  
37  	protected static final int PIXEL_INNER_WIDTH = 1;
38  
39  	protected static final int PIXEL_INNER_HEIGHT = 1;
40  
41  	protected static final int NUM_PIXELS_WIDE = 6;
42  
43  	protected static final int NUM_PIXELS_HIGH = 8;
44  
45  	protected static final int WIDTH = (NUM_PIXELS_WIDE * PIXEL_WIDTH)
46  			+ ((NUM_PIXELS_WIDE - 1) * PIXEL_INNER_WIDTH);
47  
48  	protected static final int HEIGHT = (NUM_PIXELS_HIGH * PIXEL_HEIGHT)
49  			+ ((NUM_PIXELS_HIGH - 1) * PIXEL_INNER_HEIGHT);
50  
51  	protected static Point coords[][] = new Point[6][8];
52  
53  	static {
54  		// initialize the x,y coordinates for each pixel in the 6x8 pixel array
55  		for (int i = 0; i < NUM_PIXELS_WIDE; i++) {
56  			for (int j = 0; j < NUM_PIXELS_HIGH; j++) {
57  				coords[i][j] = new Point();
58  				coords[i][j].x = (PIXEL_WIDTH * i) + (PIXEL_INNER_WIDTH * i);
59  				coords[i][j].y = (PIXEL_HEIGHT * j) + (PIXEL_INNER_HEIGHT * j);
60  			}
61  		}
62  	}
63  
64  	/*
65  	 * (non-Javadoc)
66  	 * 
67  	 * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
68  	 */
69  	public static ComponentUI createUI(JComponent c) {
70  		return new BasicDotMatrixSixByEightUI();
71  	}
72  
73  	/*
74  	 * (non-Javadoc)
75  	 * 
76  	 * @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
77  	 */
78  	public void installUI(JComponent c) {
79  		this.dotMatrixSixByEight = (JDotMatrixSixByEight) c;
80  		installDefaults();
81  		installComponents();
82  		installListeners();
83  
84  		c.setLayout(createLayoutManager());
85  		// c.setBorder(new EmptyBorder(1, 1, 1, 1));
86  	}
87  
88  	/*
89  	 * (non-Javadoc)
90  	 * 
91  	 * @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
92  	 */
93  	public void uninstallUI(JComponent c) {
94  		c.setLayout(null);
95  		uninstallListeners();
96  		uninstallComponents();
97  		uninstallDefaults();
98  
99  		this.dotMatrixSixByEight = null;
100 	}
101 
102 	public void installDefaults() {
103 
104 	}
105 
106 	public void installComponents() {
107 	}
108 
109 	public void installListeners() {
110 		this.dotMatrixSixByEightChangeListener = new ChangeListener() {
111 			public void stateChanged(ChangeEvent e) {
112 				dotMatrixSixByEight.repaint();
113 			}
114 		};
115 		this.dotMatrixSixByEight.getModel().addChangeListener(
116 				this.dotMatrixSixByEightChangeListener);
117 	}
118 
119 	public void uninstallDefaults() {
120 	}
121 
122 	public void uninstallComponents() {
123 
124 	}
125 
126 	public void uninstallListeners() {
127 		this.dotMatrixSixByEight.getModel().removeChangeListener(
128 				this.dotMatrixSixByEightChangeListener);
129 		this.dotMatrixSixByEightChangeListener = null;
130 	}
131 
132 	@Override
133 	public void paint(Graphics g, JComponent c) {
134 		super.paint(g, c);
135 		this.paintPixels(g);
136 	}
137 
138 	protected void paintPixels(Graphics g) {
139 		DotMatrixModel model = dotMatrixSixByEight.getModel();
140 		char c = model.getChar(); // character to display
141 		Color origColor = g.getColor();
142 
143 		// fill background
144 		g.setColor(dotMatrixSixByEight.getBackground());
145 		g.fillRect(0, 0, WIDTH, HEIGHT);
146 
147 		// paint character
148 		g.setColor(dotMatrixSixByEight.getForeground());
149 
150 		try {
151 			for (int i = 0; i < NUM_PIXELS_WIDE; i++) {
152 				for (int j = 0; j < NUM_PIXELS_HIGH; j++) {
153 					if (CharSet.BITMAP[c][i][j] == 1) {
154 						g.fillRect(coords[i][j].x, coords[i][j].y, PIXEL_WIDTH,
155 								PIXEL_HEIGHT);
156 					}
157 				}
158 			}
159 		} catch (ArrayIndexOutOfBoundsException e) {
160 			// no character in char set
161 			System.err.println("No character defined for (" + c
162 					+ ") decimal value [" + (int) c + "]");
163 		}
164 
165 		g.setColor(origColor);
166 	}
167 
168 	/**
169 	 * Invoked by <code>installUI</code> to create a layout manager object to
170 	 * manage the dot matrix display.
171 	 * 
172 	 * @return a layout manager object
173 	 */
174 	protected LayoutManager createLayoutManager() {
175 		return new DotMatrixSixByEightLayout();
176 	}
177 
178 	/**
179 	 * Layout for the dot matrix display.
180 	 */
181 	protected class DotMatrixSixByEightLayout implements LayoutManager {
182 		/*
183 		 * (non-Javadoc)
184 		 * 
185 		 * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String,
186 		 *      java.awt.Component)
187 		 */
188 		public void addLayoutComponent(String name, Component c) {
189 		}
190 
191 		/*
192 		 * (non-Javadoc)
193 		 * 
194 		 * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
195 		 */
196 		public void removeLayoutComponent(Component c) {
197 		}
198 
199 		/*
200 		 * (non-Javadoc)
201 		 * 
202 		 * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
203 		 */
204 		public Dimension preferredLayoutSize(Container c) {
205 			int width = 0;
206 			int height = 0;
207 			Insets ins = c.getInsets();
208 
209 			width = WIDTH;
210 			// added 1 because it looks like the bottom row of dot matrix pixels
211 			// has the bottom screen pixel chopped off
212 			height = HEIGHT + 1;
213 
214 			return new Dimension(width + ins.left + ins.right, height + ins.top
215 					+ ins.bottom);
216 		}
217 
218 		/*
219 		 * (non-Javadoc)
220 		 * 
221 		 * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
222 		 */
223 		public Dimension minimumLayoutSize(Container c) {
224 			return this.preferredLayoutSize(c);
225 		}
226 
227 		/*
228 		 * (non-Javadoc)
229 		 * 
230 		 * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
231 		 */
232 		public void layoutContainer(Container c) {
233 			// Insets ins = c.getInsets();
234 			// int width = c.getWidth();
235 			// int height = c.getHeight();
236 
237 		}
238 	}
239 }