J2ME中canvas如何通过drawline方法绘制矩形

发布于 2024-12-06 04:40:19 字数 1624 浏览 0 评论 0原文

这是我第一次在这里提问。

我是 J2ME 新手,现在正在开发一个小型应用程序,但是当我想将数据显示到表中时遇到问题。但在 J2me 中不支持表,因为我知道另一种方式可以表示表,例如通过 Canvas 或 CustomItem 创建表。

在画布中,我可以画2条线,例如:

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

但我不知道如何获得2条线的坐标,例如:

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

两条线在整个屏幕上绘制一个矩形,

我知道画线方法有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:垂直线不是完整尺寸我不知道为什么???

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-12-13 04:40:19

代码中有太多看起来相同的零 - 尝试使用描述性名称来代替:

    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

如果绘制的矩形看起来不像您期望的那样,请找到上面代码中存在错误语义的地方

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文