开发者

How to draw rectangular in J2ME by canvas through drawline method

开发者 https://www.devze.com 2023-04-06 19:18 出处:网络
This is first time I at a question in here. Im new in J2ME, and now im developing a small application, but i get problem when i wanna show data into table. But in J2me not support table there for tha

This is first time I at a question in here.

Im new in J2ME, and now im developing a small application, but i get problem when i wanna show data into table. But in J2me not support table there for that i know another way can represent for table such as create table by Canvas or CustomItem.

In Canvas i can draw 2 lines something like:

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

but i dont know how can get coordinate of 2 lines remain such as like:

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

two draw a rectangular in whole screen,

i know drawline method has 4 factors x1,y1,x2,y2.

but i can not calculate x point and y point to draw two lines above

I need you help me explain or give me example开发者_如何转开发

My Code:

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: the vertical line doesn't full size i dont know why ???

Thank you!


too much same-looking zeroes in your code - try using descriptive names instead:

    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

if rectangle drawn doesn't look like you expect find where there is wrong semantic in code above

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号