2011-09-23 91 views
1

這是我第一次在這裏的問題。如何在畫布上通過畫線方法在J2ME中繪製矩形

即時通訊新的J2ME,現在即時通訊開發一個小應用程序,但我遇到問題時,我想顯示數據到表中。但在J2ME不支持表,因爲我知道另一種方式可以表示如Canvas或CustomItem創建表等表。

在畫布我能畫兩行是這樣的:

----------------------- 
| 
| 
| 
| 

,但我不知道如何可以在2線得到協調仍然如象:

      | 
         | 
         | 
         | 
         | 
-------------------------- 

兩個全畫一個矩形screen,

我知道drawline方法有4個因子x1,y1,x2,y2。

,但我不能算出X點和Y點來繪製上述

我需要兩行,你幫我解釋一下,或給我舉例

我的代碼:

package test; 

import javax.microedition.lcdui.Canvas; 
import javax.microedition.lcdui.Graphics; 

/** 
* 
* @author J2MENewBie 
*/ 
public class TableCanvasExample extends Canvas { 
    private int cols=3; 
    private int rows =50; 
    protected void paint(Graphics g) { 
     g.setColor(0x94b2ff); 
     g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     //draw two lines 
     g.setColor(0xf8011e); 
     g.drawLine(0, 0, 0, this.getWidth()); 
     g.drawLine(0, 0, this.getHeight(), 0); 

    } 

} 

package test; 

import javax.microedition.lcdui.Display; 
import javax.microedition.midlet.*; 

/** 
* @author J2ME NewBie 
*/ 
public class TableCanvasMidlet extends MIDlet { 
    private TableCanvasExample tbcve; 

    public TableCanvasMidlet(){ 
     tbcve = new TableCanvasExample(); 
    } 
    public void startApp() { 
     Display.getDisplay(this).setCurrent(tbcve); 
    } 

    public void pauseApp() { 
    } 

    public void destroyApp(boolean unconditional) { 
    } 
} 

P/S :垂直線不全尺寸我不知道爲什麼?

謝謝!

+0

請添加到您的問題1)您使用的導入語句和2)您使用的代碼畫兩條線,你說你「可以畫」 – gnat

+0

我做到了,謝謝 – Shen

回答

0

太多相同的前瞻性零在你的代碼 - 嘗試使用描述性的名稱,而不是:

int w = getWidth(), h = getHeight(); // I think this way it's easier to read 

    int xLeft = 0, yTop = 0; // descriptive names for zeroes 
    // below, replace w - 1 -> w and h - 1 -> h if lines drawn are off-by-one 
    int xRight = w - 1, yBottom = h - 1; // names for top - right coordinates 

    g.drawLine(xLeft, yTop, xLeft, yBottom); // your left vertical 
    g.drawLine(xLeft, yTop, xRight, yTop); // your top horizontal 

    g.drawLine(xRight, yTop, xRight, yBottom); // add right vertical 
    g.drawLine(xLeft, yBottom, xRight, yBottom); // add bottom horizontal 

如果繪製的矩形看起來並不像你期望發現其中存在錯誤代碼語義以上

+0

我非常感謝你很多! 謝謝你幫助我,J2ME中的新手! – Shen