⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itemcanvas.java

📁 tRSS是MIDP的的RSS客户端。用户可以从一些新闻网站和个人数据库和feed阅读标题。
💻 JAVA
字号:
/*
 * ItemCanvas.java
 *
 * Created on January 13, 2006, 7:02 PM
 */

package com.dtw.midp.screen;

/**
 *
 * @author psaingtong
 * 
 */
import java.util.Vector;
import java.io.*; 

import javax.microedition.lcdui.*;

import com.dtw.midp.data.ItemDetail;
import com.dtw.midp.util.*;
import com.dtw.midp.Localization;

class ItemCanvas
    extends Canvas
    implements CommandListener {
  private Display tDisplay;
   
  // General information.
  private int width, height;
  private int headerHeight, footerHeight,logoHeight;
  private Font tFont;
  private int lineHeight;
  
  // Variables for paging.
  private int tScroll, lastVisible, pageSize;
  
  // The data we display.
  private Vector tWrappedTitle, tWrappedBody;
  
  //data
  private String title,description,link;
  
  private Command backCommand;
 
  
  private byte datain[]=null;
  
  private ItemDetail item;
  private Displayable prev;
  
  public ItemCanvas(Display display,Displayable _prev, ItemDetail _item) {
    tDisplay = display; 
    item=_item;
    prev=_prev;
    
    headerHeight = ListCanvas.kUpImage.getHeight();
    logoHeight=ListCanvas.logoImage.getHeight();
    headerHeight=headerHeight+logoHeight;
    
    footerHeight = ListCanvas.kDownImage.getHeight();

    width = getWidth();
    height = getHeight();
    tFont = Font.getFont(Font.FACE_PROPORTIONAL,
        Font.STYLE_PLAIN,Font.SIZE_SMALL);
    lineHeight = tFont.getHeight();

    wrap(item);
    tScroll = 0;
     
    backCommand = new Command(Localization.getMessage("command.back"), Command.BACK, 0);
    addCommand(backCommand);
 
    setCommandListener(this);
   }
  
  public void paint(Graphics g) {
    // Clear the screen.
    g.setColor(247, 247, 238);
    g.fillRect(0, 0, width, height);
    g.drawImage(ListCanvas.logoImage, width / 2, 0,
          Graphics.HCENTER | Graphics.TOP);
    // Set defaults.
    g.setColor(0, 0, 0);
    g.setFont(tFont);
    
    // Draw the header.
    paintHeader(g);
    
    // Draw as many lines as will fit between the header and the footer.
    paintBody(g);
    
    // Draw footer.
    paintFooter(g);
  }
  
  public void commandAction(Command c, Displayable s) {
      if(c==backCommand){
          back();
      } 
  }
   

  private void back() {
    tDisplay.setCurrent(prev);
  }
    
  private void paintHeader(Graphics g) {
    // Draw the title.
    int x = ListCanvas.kLeftImage.getWidth();
    int y = logoHeight;
    boolean trucking = true;
    boolean trucking_link=true;
    int index = 0;
    g.setColor(63, 124, 124);
    
    g.drawImage(ListCanvas.kLeftImage, 0, (lineHeight / 2)+y,
        Graphics.VCENTER | Graphics.LEFT);
    
        
    while (trucking) {
      if (index >= tWrappedTitle.size()) trucking = false;
      else {        
        String line =(String)tWrappedTitle.elementAt(index);
        g.drawString(line, x, y, Graphics.TOP | Graphics.LEFT);
        index++;
        y += lineHeight;
        x = 0;
        
      }
    }
      
    // Draw a separator line.
    g.setColor(128, 0, 0);
  
    g.setStrokeStyle(Graphics.SOLID);
    g.drawLine(0, y, width, y);
    g.setStrokeStyle(Graphics.SOLID);
    g.setColor(35, 71, 71);
    
 
    if (tScroll != 0) {
      g.drawImage(ListCanvas.kUpImage, width / 2, y + 1,
          Graphics.HCENTER | Graphics.TOP);
    }
    
    // Set HeaderHeight appropriately.
    headerHeight = y + ListCanvas.kUpImage.getHeight() + 1;
  }
  
  private void paintBody(Graphics g) {
    // paint the text of the description into the available space
    int y = headerHeight;
    boolean trucking = true;
    int index = tScroll;
    while (trucking) {
      if (index >= tWrappedBody.size()) trucking = false;
      else if (y + lineHeight > (height - footerHeight)) trucking = false;
      else {
        // Draw the line.
        String line = (String)tWrappedBody.elementAt(index);
        g.drawString(line, 0, y, Graphics.TOP | Graphics.LEFT);

        index++;
        y += lineHeight;
      }
    }
    
    // Set the last visible item index.
    lastVisible = index - 1;
  }
  
  private void wrap(ItemDetail item) {
    if (tWrappedTitle == null) tWrappedTitle = new Vector();
    if (tWrappedBody == null) tWrappedBody = new Vector();
   
    
    title=item.getItemTitle();
    description=item.getItemDescription();
    link=item.getItemLink(); 
    wrap(item.getItemTitle(), tWrappedTitle, true);
    wrap(item.getItemDescription(), tWrappedBody, false);
  }
  
  private void wrap(String s, Vector lines, boolean hasImage) {
    if (s == null) return;
    
    boolean trucking = true;
    String word;
    int index = 0, lastSpace = -1;
    while (trucking) {
      // Find the next space.
      int end = s.indexOf(' ', lastSpace + 1);
      if (end == -1)
        end = s.length() - 1;
      
      // Measure the string width.
      int lineWidth = tFont.substringWidth(s, index, end - index);
      
      // Compute image width;
      int imageWidth = 0;
      
      if (index == 0 && hasImage == true)
        imageWidth = ListCanvas.kLeftImage.getWidth();
      
      // Put a line away if it's too wide.
      if (lineWidth > (width - imageWidth)) {
        String line;
        if (lastSpace <= index) {
          line = s.substring(index, end);
          index = end + 1;
        }
        else if (lastSpace == -1) {
          line = s.substring(index);
          trucking = false;
        }
        else {
          line = s.substring(index, lastSpace);
          index = lastSpace + 1;
        }
        lines.addElement(line);
      }
      lastSpace = end;
      
      // Exit if we're done.
      if (end == s.length() - 1) {
        String line = s.substring(index);
        lines.addElement(line);
        trucking = false;
      }
    }
  }
  
  private void paintFooter(Graphics g) {
     
    if (lastVisible >= (tWrappedBody.size() - 1)) return;
    g.drawImage(ListCanvas.kDownImage,
        width / 2, height - footerHeight,
        Graphics.HCENTER | Graphics.TOP);
  }
  
  protected void keyPressed(int keyCode) {
    int gameAction = getGameAction(keyCode);
    
    switch(gameAction) {
      case UP:
        if (tScroll > 0) {
          tScroll -= pageSize;
          if (tScroll < 0) tScroll = 0;
          repaint();
        }
        break;
      case DOWN:
        if (lastVisible < (tWrappedBody.size() - 1)) {
          tScroll = lastVisible + 1;
          if (pageSize == 0) pageSize = lastVisible + 1;
          repaint();
        }
        break;
      case RIGHT:
        break;
      case LEFT:
        back();
        break;
    }
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -