Java Tutorial/J2ME/Arc

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

Draw arc

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class DrawArcGraphicsMIDlet 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.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);
        g.drawArc(0, 0, width/2, height/2, 0, 90);

  }
}





Fill an arc, change new color and fill a rectangle

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 J2MEArcExample extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public J2MEArcExample() {
    display = Display.getDisplay(this);
    canvas = new MyCanvas(this);
  }
  protected void startApp() {
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
}
class MyCanvas extends Canvas implements CommandListener {
  private Command exit;
  private J2MEArcExample arcExample;
  public MyCanvas(J2MEArcExample arcExample) {
    this.arcExample = arcExample;
    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
  }
  protected void paint(Graphics graphics) {
    graphics.setColor(255, 255, 255);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.setColor(255, 0, 0);
    graphics.drawArc(0, 0, getWidth(), getHeight(), 180, 180);
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      arcExample.exitMIDlet();
    }
  }
}





Fill arc

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FillArcGraphicsMIDlet 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.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);
    g.fillArc(0, 0, width/2, height/2, 0, 90);

  }
}





Pie chart

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 PieChartMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  public void startApp() {
    Display display = Display.getDisplay(this);
    int[] data = { 10, 20, 30, 40, 20 };
    Displayable d = new PieChartCanvas(data);
    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 PieChartCanvas extends Canvas {
  int[] data;
  int colors[] = { 0xFF0000, 0xA9E969, 0x00FFFF, 0xC675EC, 0x008800, 0x00C400 };
  public PieChartCanvas(int[] data) {
    this.data = data;
  }
  public void paint(Graphics g) {
    int width = this.getWidth();
    int height = this.getHeight();
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, width, height);
    int sum = 0;
    for (int i = 0; i < data.length; i++) {
      sum += data[i];
    }
    int deltaAngle = 360 * 100 / sum / 100;
    int x = 4;
    int y = 4;
    int diameter;
    if (width > height)
      diameter = height - y * 2;
    else
      diameter = width - x * 2;
    int startAngle = 0;
    for (int i = 0; i < data.length; i++) {
      g.setColor(colors[i]);
      g.fillArc(x, y, diameter, diameter, startAngle, deltaAngle * data[i]);
      startAngle += deltaAngle * data[i];
    }
  }
}