counter.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  4. <title>Contador em código de Gray</title>
  5. </head>
  6. <body bgcolor="FFFFFF">
  7. <h1>Contador em código de Gray</h1>
  8. <p> Esta orientação sobre bibliotecas do Logisim apresenta um sofisticado
  9. contador em código de Gray, que permite ao usuário alterar seu valor atual
  10. usando a ferramenta Testar (Poke) e colocar um rótulo sobre o componente
  11. usando a ferramenta de texto (Text Tool). Também personaliza o ícone que
  12. aparece no explorador, associado à ferramenta. </p>
  13. <h2>GrayCounter</h2>
  14. <pre>
  15. package com.cburch.gray;
  16. import java.net.URL;
  17. import javax.swing.ImageIcon;
  18. import com.cburch.logisim.data.Attribute;
  19. import com.cburch.logisim.data.BitWidth;
  20. import com.cburch.logisim.data.Bounds;
  21. import com.cburch.logisim.data.Direction;
  22. import com.cburch.logisim.instance.Instance;
  23. import com.cburch.logisim.instance.InstanceFactory;
  24. import com.cburch.logisim.instance.InstancePainter;
  25. import com.cburch.logisim.instance.InstanceState;
  26. import com.cburch.logisim.instance.Port;
  27. import com.cburch.logisim.instance.StdAttr;
  28. import com.cburch.logisim.util.GraphicsUtil;
  29. import com.cburch.logisim.util.StringUtil;
  30. /** Manufactures a counter that iterates over Gray codes. This demonstrates
  31. * several additional features beyond the SimpleGrayCounter class. */
  32. class GrayCounter extends InstanceFactory {
  33. public GrayCounter() {
  34. super("Gray Counter");
  35. setOffsetBounds(Bounds.create(-30, -15, 30, 30));
  36. setPorts(new Port[] {
  37. new Port(-30, 0, Port.INPUT, 1),
  38. new Port( 0, 0, Port.OUTPUT, StdAttr.WIDTH),
  39. });
  40. // We'll have width, label, and label font attributes. The latter two
  41. // attributes allow us to associate a label with the component (though
  42. // we'll also need configureNewInstance to configure the label's
  43. // location).
  44. setAttributes(
  45. new Attribute[] { StdAttr.WIDTH, StdAttr.LABEL, StdAttr.LABEL_FONT },
  46. new Object[] { BitWidth.create(4), "", StdAttr.DEFAULT_LABEL_FONT });
  47. // The following method invocation sets things up so that the instance's
  48. // state can be manipulated using the Poke Tool.
  49. setInstancePoker(CounterPoker.class);
  50. // These next two lines set it up so that the explorer window shows a
  51. // customized icon representing the component type. This should be a
  52. // 16x16 image.
  53. URL url = getClass().getClassLoader().getResource("com/cburch/gray/counter.gif");
  54. if(url != null) setIcon(new ImageIcon(url));
  55. }
  56. /** The configureNewInstance method is invoked every time a new instance
  57. * is created. In the superclass, the method doesn't do anything, since
  58. * the new instance is pretty thoroughly configured already by default. But
  59. * sometimes you need to do something particular to each instance, so you
  60. * would override the method. In this case, we need to set up the location
  61. * for its label. */
  62. protected void configureNewInstance(Instance instance) {
  63. Bounds bds = instance.getBounds();
  64. instance.setTextField(StdAttr.LABEL, StdAttr.LABEL_FONT,
  65. bds.getX() + bds.getWidth() / 2, bds.getY() - 3,
  66. GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
  67. }
  68. public void propagate(InstanceState state) {
  69. // This is the same as with SimpleGrayCounter, except that we use the
  70. // StdAttr.WIDTH attribute to determine the bit width to work with.
  71. BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
  72. CounterData cur = CounterData.get(state, width);
  73. boolean trigger = cur.updateClock(state.getPort(0));
  74. if(trigger) cur.setValue(GrayIncrementer.nextGray(cur.getValue()));
  75. state.setPort(1, cur.getValue(), 9);
  76. }
  77. public void paintInstance(InstancePainter painter) {
  78. // This is essentially the same as with SimpleGrayCounter, except for
  79. // the invocation of painter.drawLabel to make the label be drawn.
  80. painter.drawBounds();
  81. painter.drawClock(0, Direction.EAST);
  82. painter.drawPort(1);
  83. painter.drawLabel();
  84. if(painter.getShowState()) {
  85. BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
  86. CounterData state = CounterData.get(painter, width);
  87. Bounds bds = painter.getBounds();
  88. GraphicsUtil.drawCenteredText(painter.getGraphics(),
  89. StringUtil.toHexString(width.getWidth(), state.getValue().toIntValue()),
  90. bds.getX() + bds.getWidth() / 2,
  91. bds.getY() + bds.getHeight() / 2);
  92. }
  93. }
  94. }
  95. </pre>
  96. <h2>CounterPoker</h2>
  97. <pre>
  98. package com.cburch.gray;
  99. import java.awt.Color;
  100. import java.awt.Graphics;
  101. import java.awt.event.KeyEvent;
  102. import java.awt.event.MouseEvent;
  103. import com.cburch.logisim.data.BitWidth;
  104. import com.cburch.logisim.data.Bounds;
  105. import com.cburch.logisim.data.Value;
  106. import com.cburch.logisim.instance.InstancePainter;
  107. import com.cburch.logisim.instance.InstancePoker;
  108. import com.cburch.logisim.instance.InstanceState;
  109. import com.cburch.logisim.instance.StdAttr;
  110. /** When the user clicks a counter using the Poke Tool, a CounterPoker object
  111. * is created, and that object will handle all user events. Note that
  112. * CounterPoker is a class specific to GrayCounter, and that it must be a
  113. * subclass of InstancePoker in the com.cburch.logisim.instance package. */
  114. public class CounterPoker extends InstancePoker {
  115. public CounterPoker() { }
  116. /** Determines whether the location the mouse was pressed should result
  117. * in initiating a poke.
  118. */
  119. public boolean init(InstanceState state, MouseEvent e) {
  120. return state.getInstance().getBounds().contains(e.getX(), e.getY());
  121. // Anywhere in the main rectangle initiates the poke. The user might
  122. // have clicked within a label, but that will be outside the bounds.
  123. }
  124. /** Draws an indicator that the caret is being selected. Here, we'll draw
  125. * a red rectangle around the value. */
  126. public void paint(InstancePainter painter) {
  127. Bounds bds = painter.getBounds();
  128. BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
  129. int len = (width.getWidth() + 3) / 4;
  130. Graphics g = painter.getGraphics();
  131. g.setColor(Color.RED);
  132. int wid = 7 * len + 2; // width of caret rectangle
  133. int ht = 16; // height of caret rectangle
  134. g.drawRect(bds.getX() + (bds.getWidth() - wid) / 2,
  135. bds.getY() + (bds.getHeight() - ht) / 2, wid, ht);
  136. g.setColor(Color.BLACK);
  137. }
  138. /** Processes a key by just adding it onto the end of the current value. */
  139. public void keyTyped(InstanceState state, KeyEvent e) {
  140. // convert it to a hex digit; if it isn't a hex digit, abort.
  141. int val = Character.digit(e.getKeyChar(), 16);
  142. BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
  143. if(val < 0 || (val & width.getMask()) != val) return;
  144. // compute the next value
  145. CounterData cur = CounterData.get(state, width);
  146. int newVal = (cur.getValue().toIntValue() * 16 + val) & width.getMask();
  147. Value newValue = Value.createKnown(width, newVal);
  148. cur.setValue(newValue);
  149. state.fireInvalidated();
  150. // You might be tempted to propagate the value immediately here, using
  151. // state.setPort. However, the circuit may currently be propagating in
  152. // another thread, and invoking setPort directly could interfere with
  153. // that. Using fireInvalidated notifies the propagation thread to
  154. // invoke propagate on the counter at its next opportunity.
  155. }
  156. }
  157. </pre>
  158. <p><strong>Próximo:</strong> <a href="guide.html">Orientações</a>.</p>
  159. </body>
  160. </html>