내 코드에서 정확한 좌표로 String을 그릴 수는 없습니다. 그 좌상 구석은 주어진 좌표에서 시작하지 않고 다른 곳에서 시작됩니다. 그러나 동일한 주어진 좌표에서 사각형을 그릴 경우 잘 배치됩니다. 이 행동은 어떻게 가능합니까? 여기 왜 drawString 메서드가 항상 주어진 좌표에서 시작하지 않는 것 같습니까?
내가 beforeShow() 메소드를 호출 내 코드 :Image photoBase = fetchResourceFile().getImage("Voiture_4_3.jpg");
Image watermark = fetchResourceFile().getImage("Watermark.png");
f.setLayout(new LayeredLayout());
final Label drawing = new Label();
f.addComponent(drawing);
// Image mutable dans laquelle on va dessiner (fond blancpar défaut)
Image mutableImage = Image.createImage(photoBase.getWidth(), photoBase.getHeight());
// Paint all the stuff
paintAll(mutableImage.getGraphics(), photoBase, watermark, photoBase.getWidth(), photoBase.getHeight());
drawing.getUnselectedStyle().setBgImage(mutableImage);
drawing.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
// Save the graphics
// Save the image with the ImageIO class
long time = new Date().getTime();
OutputStream os;
try {
os = Storage.getInstance().createOutputStream("screenshot_" + Long.toString(time) + ".png");
ImageIO.getImageIO().save(mutableImage, os, ImageIO.FORMAT_PNG, 1.0f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
그리고 w = 0에 대한 paintAll 방법
public void paintAll(Graphics g, Image background, Image watermark, int width, int height) {
// Full quality
float saveQuality = 1.0f;
// Create image as buffer
Image imageBuffer = Image.createImage(width, height, 0xffffff);
// Create graphics out of image object
Graphics imageGraphics = imageBuffer.getGraphics();
// Do your drawing operations on the graphics from the image
imageGraphics.drawImage(background, 0, 0);
imageGraphics.drawImage(watermark, 0, 0);
imageGraphics.setColor(0xFF0000);
// Upper left corner
imageGraphics.fillRect(0, 0, 10, 10);
// Lower right corner
imageGraphics.setColor(0x00FF00);
imageGraphics.fillRect(width - 10, height - 10, 10, 10);
imageGraphics.setColor(0xFF0000);
Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(220, Font.STYLE_BOLD);
imageGraphics.setFont(f);
// Draw a string right below the M from Mercedes on the car windscreen (measured in Gimp)
int w = 0, h = 0;
imageGraphics.drawString("HelloWorld", w, h);
// Coin haut droit de la string
imageGraphics.setColor(0x0000FF);
imageGraphics.fillRect(w, h, 20, 20);
// Draw the complete image on your Graphics object g (the screen I guess)
g.drawImage(imageBuffer, 0, 0);
}
결과가 w = 841, h = 610 (양쪽 축에 오프셋이 나타남 : 프런트 유리의 Mercedes M 근처의 파란색 점과 헬로 월드 문자열)
EDIT1 : 나는 또한이 픽셀로 dpi로 변환하는 것이 좋습니다 안드로이드에 대한이 SO question 읽어 보시기 바랍니다. Codename One에도 적용됩니까? 그렇다면 어떻게해야합니까? 나는 어떤 도움을 주시면 감사하겠습니다
,
건배
이 해결 방법은 CN1 lib 버전 115 이후 버전에서는 더 이상 필요하지 않을 것 같습니다 (http://stackoverflow.com/questions/37986722/why- 세부 사항을 위해 진짜와 andro에보다는 on-a-get-a-different-behieour-in-codename 이름 하나 시뮬레이터). 실제로 시뮬레이터 만의 문제였습니다! – HelloWorld