0 Replies - 1196 Views - Last Post: 06 June 2009 - 08:49 AM

#1 garytkm89  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 28-February 09

Drawing line on image

Posted 06 June 2009 - 08:49 AM

Hi, i would like to draw a line on the image i loaded from my computer to my midlet. However, when i use the drawLine() method, it is the screen that being draw and not the picture that being draw. I'm facing a problem because my image is larger than the screen and I'm scrolling the picture to view. Whenever i scroll, the line is still at the coordinate of my screen and not the coordinate of my image. So, is there any mistake in my code because even when I'm scrolling the image, the line that being draw is still there but I intended to draw according to the coordinate of my image. Any help would be kindly appreciated

import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import java.io.IOException;
import java.io.InputStream;

public class MyMapCanvas extends GameCanvas implements Runnable {
	// the couple image
	private Image map;

	// the couple image coordinates
	private int mapX;
	private int mapY;
	// the distance to move in the x axis
	private int dx = 3;
	private int dy = 3;

	public MyMapCanvas() {
	  super(true);
	}

	public void start() {
	  try {
		map = Image.createImage("/ice.jpg");
		mapX = getWidth();
		mapY = getHeight();
		
	  }catch(IOException ioex) { System.err.println(ioex); }

	  Thread runner = new Thread(this);
	  runner.start();
	}

	public void run() {
	  draw(getGraphics()); // <- here is the problem i guess.

	  while(true) {
		checkUserInput(getKeyStates());
		  updateGameScreen(getGraphics());
	  }
	}

	private void draw(Graphics g){
		g.setColor(0xff0000);
		g.drawLine(0, 0, 200, 200);
	}

	private void checkUserInput(int keyState) {
	  if((keyState & LEFT_PRESSED) != 0) {
		mapX += dx;
	  }
	  else if((keyState & RIGHT_PRESSED) != 0) {
		mapX -= dx;
	  }
	  else if((keyState & UP_PRESSED) != 0) {
		mapY += dy;
	  }
	  else if((keyState & DOWN_PRESSED) != 0) {
		mapY -= dy;
	  }
	}

	private void updateGameScreen(Graphics g) {
	  g.setColor(0xffffff);// make the background white
		  g.fillRect(0, 0, getWidth(), getHeight());
		  g.drawImage( map, mapX, mapY, Graphics.HCENTER | Graphics.BOTTOM);
		  flushGraphics();
	}

}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1