Plot 2D : Line « J2ME « Java Tutorial






Plot 2D
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
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]);
    }
  }
}








31.29.Line
31.29.1.Draw linesDraw lines
31.29.2.Plot 2DPlot 2D