Graphics: translate(int x, int y) : Graphics « javax.microedition.lcdui « Java by API






Graphics: translate(int x, int y)

 



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.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class DisplayBigImageTest extends MIDlet implements CommandListener {
  private Display display;

  private TestCanvas tc;

  private Command leftCommand = new Command("Left", Command.SCREEN, 1);

  private Command rightCommand = new Command("Right", Command.SCREEN, 1);

  public DisplayBigImageTest() {
    Image img = null;
    try {
      img = Image.createImage("/10.png");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    tc = new TestCanvas(img);
    tc.addCommand(rightCommand);
    tc.addCommand(leftCommand);
    tc.setCommandListener(this);
    display = Display.getDisplay(this);
  }

  public void startApp() throws MIDletStateChangeException {
    display.setCurrent(tc);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable d) {
    int stepX = tc.getWidth() / 4;
    int stepY = tc.getHeight() / 4;
    if (d == tc && c == leftCommand) {
      tc.increaseXY(stepX, 0);
      tc.repaint();
    } else if (d == tc && c == rightCommand) {
      tc.increaseXY(-stepX, 0);
      tc.repaint();
    }
  }

  class TestCanvas extends Canvas {
    private Image img;

    private int transX = 0;

    private int transY = 0;

    public TestCanvas(Image img) {
      this.img = img;
      transX = 0;
      transY = 0;
    }

    public void increaseXY(int x, int y) {
      transX += x;
      transY += y;
    }

    public void paint(Graphics g) {
      int width = this.getWidth();
      int height = this.getHeight();

      g.setGrayScale(255);
      g.fillRect(0, 0, width - 1, height - 1);
      g.setGrayScale(0);
      g.drawRect(0, 0, width - 1, height - 1);

      g.translate(transX, transY);
      g.drawImage(img, 0, 0, g.TOP | g.LEFT);
    }
  }
}

   
  








Related examples in the same category

1.Graphics.BASELINE
2.Graphics.BOTTOM
3.Graphics.DOTTED
4.Graphics.HCENTER
5.Graphics.LEFT
6.Graphics.SOLID
7.Graphics.TOP
8.Graphics.VCENTER
9.Graphics: drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
10.Graphics: drawLine(int x1, int y1, int x2, int y2)
11.Graphics: drawString(String str, int x, int y, int anchor)
12.Graphics: fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
13.Graphics: fillRect(int x, int y, int width, int height)
14.Graphics: fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
15.Graphics: setClip(int x, int y, int width, int height)
16.Graphics: setColor(int RGB)
17.Graphics: setGrayScale(int value)
18.Graphics: setStrokeStyle(int style)