1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Created on 2007-2-14
  18. *
  19. */
  20. package peaklau.eaglefund;
  21. import javax.microedition.lcdui.CommandListener;
  22. import javax.microedition.lcdui.Graphics;
  23. /**
  24. * Display stock index by image<br>
  25. *
  26. * <br>
  27. * First, get the latest stock index data from http://www.peaklau.com/fund/stockIndex.php. <br>
  28. *   The requezt is the standard HTTP request.<br>
  29. *   eg:http://www.peaklau.com/fund/stockIndex.php?days=30<br>
  30. * Then, use class StockImageData to parse the XML formated data.<br>
  31. * Next, parse the data and show the important data.<br>
  32. *
  33. * @author peaklau <br>
  34. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  35. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  36. * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
  37. */
  38. public class StockImage extends TitleScreen implements CommandListener{
  39. private int days=0;
  40. private int[]values=null;
  41. private int[]tvalues=null;
  42. private int[]importantValues=null;
  43. private int importantCount=0;
  44. private int max=0;
  45. private int min=Integer.MAX_VALUE;
  46. private int maxIndex=days-1;
  47. private int minIndex=days-1;
  48. private boolean completed=false;
  49. public StockImage(int days,String title,String backDisplayable,INavigator navigator){
  50. super(title,backDisplayable,navigator);
  51. this.days=days;
  52. values=new int[days];
  53. tvalues=new int[days];
  54. importantValues=new int[days];
  55. setCommandListener(this);
  56. }
  57. protected void execute(){
  58. setStatus("正在同步...");
  59. HttpClient httpClient=navigator.getHttpClient();
  60. try{
  61. httpClient.httpGet("http://"+host+"/fund/stockIndex.php?days="+days);
  62. StockImageData foo = new StockImageData(values);
  63. foo.init();
  64. byte[]buf=httpClient.getData();
  65. int size=httpClient.getSize();
  66. if(size>0){
  67. foo.parseArray(buf,0,size);
  68. }
  69. }catch(Exception e){
  70. e.printStackTrace();
  71. setStatus("网络错误");
  72. return;
  73. }
  74. setStatus("正在分析数据");
  75. for(int i=0;i<days;i++){
  76. //System.out.println(values[i]);
  77. if(max<=values[i]){
  78. max=values[i];
  79. maxIndex=i;
  80. }
  81. if((min>values[i])&&(values[i]>=0)){
  82. min=values[i];
  83. minIndex=i;
  84. }
  85. }
  86. importantValues[minIndex]=1;
  87. importantValues[maxIndex]=1;
  88. if(max!=min){
  89. for(int i=0;i<days;i++){
  90. tvalues[i]=(values[i]-min)*100/(max-min);
  91. }
  92. }
  93. importantValues[days-1]=1;
  94. //local max and min
  95. for(int span=days10;span<days-span-1;span++){
  96. for(int i=0;i<days-span-1;i++){
  97. int localMax=0;
  98. int localMin=Integer.MAX_VALUE;
  99. int localMaxIndex=i;
  100. int localMinIndex=i;
  101. for(int j=i;j<i+span;j++){
  102. if(localMax<values[j]){
  103. localMax=values[j];
  104. localMaxIndex=j;
  105. }
  106. if((localMin>values[j])&&(values[j]>0)){
  107. localMin=values[j];
  108. localMinIndex=j;
  109. }
  110. }
  111. if((localMaxIndex>i+span4)&&(localMaxIndex<i+span-1-span4)){
  112. importantValues[localMaxIndex]=1;
  113. }
  114. if((localMinIndex>i+span4)&&(localMinIndex<i+span-1-span4)){
  115. importantValues[localMinIndex]=1;
  116. }
  117. }
  118. }
  119. importantCount=0;
  120. for(int i=0;i<days;i++){
  121. importantCount+=importantValues[i];
  122. }
  123. importantIndex=importantCount;
  124. //
  125. completed=true;
  126. setStatus("同步完成时间 "+getCurrentTime());
  127. }
  128. protected void paint(Graphics g){
  129. super.paint(g);
  130. //image
  131. if(completed){
  132. int dx=1;
  133. int dy=Line_HIGHT+(FONT_HEIGHT+1)/2+2;
  134. int w=width-dx-2;
  135. int h=height-dy-Line_HIGHT-2-(FONT_HEIGHT+1)/2;
  136. g.setColor(0x00D4D0C8);
  137. g.setStrokeStyle(Graphics.SOLID);
  138. g.drawRect(dx,dy,w,h);
  139. g.setColor(0x00FF0000);
  140. int m=wdays;
  141. int k=1;
  142. if(m==0){
  143. m=1;
  144. k=daysw+1;
  145. }
  146. int offset=(days-1-daysk*k+k)%k;
  147. //System.out.println("days="+days+" w="+w+" m="+m+" k="+k+" offset="+offset);
  148. for(int i=offset;(i+1)*k+offset<days;i++){
  149. if(values[i*k]>=0){
  150. g.drawLine(dx+i*m,dy+h-tvalues[i*k+offset]*h100,dx+(i+1)*m,dy+h-tvalues[(i+1)*k+offset]*h100);
  151. }
  152. }
  153. g.setColor(0x00000000);
  154. //g.drawString(""+values[maxIndex]/100,dx,dy+h-tvalues[maxIndex]*h/100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
  155. //g.drawString(""+values[minIndex]/100,dx,dy+h-tvalues[minIndex]*h/100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
  156. importantIndex=(importantIndex%(importantCount+1)+importantCount+1)%(importantCount+1);
  157. int tmp=0;
  158. for(int i=0;i<days;i++){
  159. tmp+=importantValues[i];
  160. if(tmp==importantIndex){
  161. g.drawString(""+values[i]/100,dx,dy+h-tvalues[i]*h100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
  162. break;
  163. }
  164. }
  165. g.setColor(0x00D4D0C8);
  166. g.setStrokeStyle(Graphics.DOTTED);
  167. for(int i=0;i<days;i++){
  168. if(importantValues[i]==1){
  169. g.drawLine(dx,dy+h-tvalues[i]*h100,dx+i*m,dy+h-tvalues[i]*h100);
  170. }
  171. }
  172. }
  173. }
  174. private int importantIndex=0;
  175. protected void keyPressed(int keyCode) {
  176. super.keyPressed(keyCode);
  177. switch (getGameAction(keyCode)) {
  178. case LEFT:
  179. case UP:
  180. importantIndex++;
  181. repaint();
  182. break;
  183. case RIGHT:
  184. case DOWN:
  185. importantIndex--;
  186. repaint();
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. }