简体   繁体   English

Java ME绘制矩形

[英]Java ME draw rectangle

How can I draw rectangles with Java ME? 如何使用Java ME绘制矩形? Sorry, but I am new to Java and have to use Java ME for this project. 抱歉,但是我是Java新手,必须为此项目使用Java ME。

It's hard to know exactly how much detail to go into because you don't mention what you've done with ME so far, but the basic idea is: 很难确切知道要涉及多少细节,因为到目前为止您还没有提到您对ME所做的工作,但是基本思想是:

  • Override the Canvas class with your own class 用您自己的类覆盖Canvas类
  • Override the paint() method 重写paint()方法
  • Inside the paint() method, you can call drawRect() or fillRect() on the Graphics object that's passed in 在paint()方法内部,可以在传入的Graphics对象上调用drawRect()或fillRect()
  • Elsewhere (eg in the startApp() method of your MIDlet class), set an instance of your canvas to be the current display 在其他地方(例如,在MIDlet类的startApp()方法中),将画布实例设置为当前显示

So example code looks roughy as follows. 因此,示例代码如下所示。 Create a Canvas class something like this: 创建一个Canvas类,如下所示:

public class MyCanvas extends Canvas {
  public void paint(Graphics g) {
    g.drawRect(20, 20, 50, 50);
  }
}

Then something like this in your MIDlet class: 然后在您的MIDlet类中这样:

public class MyMIDlet extends MIDlet {
  public void startApp() {
    Canvas c = new MyCanvas();
    Display.getDisplay(this).setCurrent(c);
  }

  ...
}

Good guides to Java ME should give you an overview of other methods available on Graphics, other code you'll need in your MIDlet class, how to handle Commands (for handling button presses) etc. 良好的Java ME指南应概述图形上可用的其他方法,MIDlet类中需要的其他代码,如何处理命令(用于处理按钮按下)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM