- /*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /*
- * Created on 2007-2-14
- *
- */
- package peaklau.eaglefund;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Graphics;
- /**
- * Display stock index by image<br>
- *
- * <br>
- * First, get the latest stock index data from http://www.peaklau.com/fund/stockIndex.php. <br>
- * The requezt is the standard HTTP request.<br>
- * eg:http://www.peaklau.com/fund/stockIndex.php?days=30<br>
- * Then, use class StockImageData to parse the XML formated data.<br>
- * Next, parse the data and show the important data.<br>
- *
- * @author peaklau <br>
- * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
- * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
- * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
- */
- public class StockImage extends TitleScreen implements CommandListener{
- private int days=0;
- private int[]values=null;
- private int[]tvalues=null;
- private int[]importantValues=null;
- private int importantCount=0;
- private int max=0;
- private int min=Integer.MAX_VALUE;
- private int maxIndex=days-1;
- private int minIndex=days-1;
- private boolean completed=false;
- public StockImage(int days,String title,String backDisplayable,INavigator navigator){
- super(title,backDisplayable,navigator);
- this.days=days;
- values=new int[days];
- tvalues=new int[days];
- importantValues=new int[days];
- setCommandListener(this);
- }
- protected void execute(){
- setStatus("正在同步...");
- HttpClient httpClient=navigator.getHttpClient();
- try{
- httpClient.httpGet("http://"+host+"/fund/stockIndex.php?days="+days);
- StockImageData foo = new StockImageData(values);
- foo.init();
- byte[]buf=httpClient.getData();
- int size=httpClient.getSize();
- if(size>0){
- foo.parseArray(buf,0,size);
- }
- }catch(Exception e){
- e.printStackTrace();
- setStatus("网络错误");
- return;
- }
- setStatus("正在分析数据");
- for(int i=0;i<days;i++){
- //System.out.println(values[i]);
- if(max<=values[i]){
- max=values[i];
- maxIndex=i;
- }
- if((min>values[i])&&(values[i]>=0)){
- min=values[i];
- minIndex=i;
- }
- }
- importantValues[minIndex]=1;
- importantValues[maxIndex]=1;
- if(max!=min){
- for(int i=0;i<days;i++){
- tvalues[i]=(values[i]-min)*100/(max-min);
- }
- }
- importantValues[days-1]=1;
- //local max and min
- for(int span=days10;span<days-span-1;span++){
- for(int i=0;i<days-span-1;i++){
- int localMax=0;
- int localMin=Integer.MAX_VALUE;
- int localMaxIndex=i;
- int localMinIndex=i;
- for(int j=i;j<i+span;j++){
- if(localMax<values[j]){
- localMax=values[j];
- localMaxIndex=j;
- }
- if((localMin>values[j])&&(values[j]>0)){
- localMin=values[j];
- localMinIndex=j;
- }
- }
- if((localMaxIndex>i+span4)&&(localMaxIndex<i+span-1-span4)){
- importantValues[localMaxIndex]=1;
- }
- if((localMinIndex>i+span4)&&(localMinIndex<i+span-1-span4)){
- importantValues[localMinIndex]=1;
- }
- }
- }
- importantCount=0;
- for(int i=0;i<days;i++){
- importantCount+=importantValues[i];
- }
- importantIndex=importantCount;
- //
- completed=true;
- setStatus("同步完成时间 "+getCurrentTime());
- }
- protected void paint(Graphics g){
- super.paint(g);
- //image
- if(completed){
- int dx=1;
- int dy=Line_HIGHT+(FONT_HEIGHT+1)/2+2;
- int w=width-dx-2;
- int h=height-dy-Line_HIGHT-2-(FONT_HEIGHT+1)/2;
- g.setColor(0x00D4D0C8);
- g.setStrokeStyle(Graphics.SOLID);
- g.drawRect(dx,dy,w,h);
- g.setColor(0x00FF0000);
- int m=wdays;
- int k=1;
- if(m==0){
- m=1;
- k=daysw+1;
- }
- int offset=(days-1-daysk*k+k)%k;
- //System.out.println("days="+days+" w="+w+" m="+m+" k="+k+" offset="+offset);
- for(int i=offset;(i+1)*k+offset<days;i++){
- if(values[i*k]>=0){
- g.drawLine(dx+i*m,dy+h-tvalues[i*k+offset]*h100,dx+(i+1)*m,dy+h-tvalues[(i+1)*k+offset]*h100);
- }
- }
- g.setColor(0x00000000);
- //g.drawString(""+values[maxIndex]/100,dx,dy+h-tvalues[maxIndex]*h/100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
- //g.drawString(""+values[minIndex]/100,dx,dy+h-tvalues[minIndex]*h/100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
- importantIndex=(importantIndex%(importantCount+1)+importantCount+1)%(importantCount+1);
- int tmp=0;
- for(int i=0;i<days;i++){
- tmp+=importantValues[i];
- if(tmp==importantIndex){
- g.drawString(""+values[i]/100,dx,dy+h-tvalues[i]*h100-(FONT_HEIGHT+1)/2,Graphics.LEFT|Graphics.TOP);
- break;
- }
- }
- g.setColor(0x00D4D0C8);
- g.setStrokeStyle(Graphics.DOTTED);
- for(int i=0;i<days;i++){
- if(importantValues[i]==1){
- g.drawLine(dx,dy+h-tvalues[i]*h100,dx+i*m,dy+h-tvalues[i]*h100);
- }
- }
- }
- }
- private int importantIndex=0;
- protected void keyPressed(int keyCode) {
- super.keyPressed(keyCode);
- switch (getGameAction(keyCode)) {
- case LEFT:
- case UP:
- importantIndex++;
- repaint();
- break;
- case RIGHT:
- case DOWN:
- importantIndex--;
- repaint();
- break;
- default:
- break;
- }
- }
- }