【问题标题】:How to draw rectangular in J2ME by canvas through drawline method如何通过drawline方法在J2ME中通过canvas绘制矩形
【发布时间】:2011-09-24 00:22:31
【问题描述】:

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

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

在 Canvas 中,我可以画两条线,例如:

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

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

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

两个在整个屏幕上画一个矩形,

我知道 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:垂直线没有全尺寸我不知道为什么???

谢谢!

【问题讨论】:

  • 请在您的问题中添加 1) 您使用的导入语句和 2) 用于绘制两条您说“可以绘制”的线的代码

标签: java-me midp lcdui


【解决方案1】:

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

    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

如果绘制的矩形看起来不像你期望的那样,那么在上面的代码中找出错误的语义

【讨论】:

  • 非常感谢您!谢谢你帮助我,J2ME 的新手!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2018-08-20
  • 2015-01-11
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多