Java Tutorial/J2ME/Line

Материал из Java эксперт
Версия от 04:58, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Draw lines

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class GraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}
class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.drawLine(0, 0, width - 1, height - 1);
  }
}





Plot 2D

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class Plot2DMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  Display display;
  int[] line1 = { 80, 20, 60, 70, 50, 20 };
  int[] line2 = { 20, 10, 50, 60, 90, 60 };
  public void startApp() {
    int maxX = 10;
    int maxY = 100;
    Display display = Display.getDisplay(this);
    Displayable d = new Plot2DCanvas(line1, line2, maxX, maxY);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class Plot2DCanvas extends Canvas {
  int line1[];
  int line2[];
  int maxX, maxY;
  Plot2DCanvas(int[] line1, int[] line2, int maxX, int maxY) {
    this.line1 = line1;
    this.line2 = line2;
    this.maxX = maxX;
    this.maxY = maxY;
  }
  public void paint(Graphics g) {
    int width = this.getWidth();
    int height = this.getHeight();
    g.setGrayScale(0);
    g.drawLine(0, height, 0, 0);
    g.drawLine(0, height - 1, width, height - 1);
    int deltaX = width / maxX;
    int deltaY = height / maxY;
    g.setStrokeStyle(Graphics.DOTTED);
    for (int i = 0; i < line1.length - 1; i++) {
      g.drawLine(deltaX * i, height - deltaY * line1[i], deltaX * (i + 1), height - deltaY
          * line1[i + 1]);
    }
  }
}