2016-06-13 5 views
1

내 코드에서 정확한 좌표로 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); 


} 

결과, H = 0 (명백한 오프셋 없음) : no offset

결과가 w = 841, h = 610 (양쪽 축에 오프셋이 나타남 : 프런트 유리의 Mercedes M 근처의 파란색 점과 헬로 월드 문자열) with offset

EDIT1 : 나는 또한이 픽셀로 dpi로 변환하는 것이 좋습니다 안드로이드에 대한이 SO question 읽어 보시기 바랍니다. Codename One에도 적용됩니까? 그렇다면 어떻게해야합니까? 나는 어떤 도움을 주시면 감사하겠습니다

,

건배

답변

0

영감을 받아 Gabriel Hass' answer 마지막으로 다른 중간 이미지를 사용하여 (0; 0)에서 문자열을 작성한 다음이 이미지를 오른쪽 좌표의 imageBuffer Image에 그립니다. 그것은 작동하지만 내 마음에 drawString (Image, Coordinates)는 주어진 좌표에서 직접 그려야합니다. @Shai가 아닌가요?

// Full quality 
    float saveQuality = 1.0f; 

    String mess = "HelloWorld"; 

    // 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); 


    // Create an intermediate image just with the message string (will be moved to the right coordinates later) 
    Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(150, Font.STYLE_BOLD); 
    // Get the message dimensions 
    int messWidth = f.stringWidth(mess); 
    int messHeight = f.getHeight(); 

    Image messageImageBuffer = Image.createImage(messWidth, messHeight, 0xffffff); 
    Graphics messageImageGraphics = messageImageBuffer.getGraphics(); 

    messageImageGraphics.setColor(0xFF0000); 
    messageImageGraphics.setFont(f); 

    // Write the string at (0; 0) 
    messageImageGraphics.drawString(mess, 0, 0); 

    // Move the string to its final location right below the M from Mercedes on the car windscreen (measured in Gimp) 
    int w = 841, h = 610; 
    imageGraphics.drawImage(messageImageBuffer, w, h); 


    // This "point" is expected to be on the lower left corner of the M letter from Mercedes and on the upper left corner of the message string 
    imageGraphics.setColor(0x0000FF); 
    imageGraphics.fillRect(w, h, 20, 20); 

    // Draw the complete image on your Graphics object g 
    g.drawImage(imageBuffer, 0, 0); 
+0

이 해결 방법은 CN1 lib 버전 115 이후 버전에서는 더 이상 필요하지 않을 것 같습니다 (http://stackoverflow.com/questions/37986722/why- 세부 사항을 위해 진짜와 andro에보다는 on-a-get-a-different-behieour-in-codename 이름 하나 시뮬레이터). 실제로 시뮬레이터 만의 문제였습니다! – HelloWorld

1

모두 gimageGraphics 있습니다 (javadoc에이 dpi의 대략 1mm는 것을 알려주기 때문에 내가 mm를 사용) 성공하지

Display.getInstance().convertToPixel(measureInMillimeterFromGimp) 

시도 동일한 그래픽이 두 번 생성되어 몇 가지 의미가있을 수 있습니다 (실제로는 알 수 없음).

그리기를 끝내기 전에 스타일 배경에 이미지를 배치하십시오. 나는 이것이 당신이보고있는 기이 한 일의 이유가 될지 모르지만 그 코드를 의심 할 것입니다.

+0

imageGraphics는 HTTP (참조 전체 해상도로 이미지를 저장하는 트릭 : 여기

String eventually at the righ coordinates (blue point expected to be on the top left corner of String HelloWorld

내가 (beforeShow 코드가 변경되지 않은) 내 문제를 해결하는 데 사용 paintAll하는 방법입니다 : //stackoverflow.com/a/37748864/6351897). 네가 지적한 의심스러운 부분을 바꿀거야! – HelloWorld

+0

불행하게도 변경할 수있는 이미지를 그리기 후에 배경으로 설정하면 아무 것도 변경되지 않았습니다 (비록 의미가 있지만). 어쨌든 고마워. 파란색 재현 숫자가 좌표를 따르는 이유는 아직도 이해할 수 없지만 문자열은 그렇지 않습니다! – HelloWorld

+0

이 스크린 샷은 시뮬레이터 또는 장치의 크기 조정 모드에 있습니까? 시뮬레이터 메뉴에서 "스크롤 가능"을 선택하지 않으면 일부 아티팩트가 발생할 수 있습니다. –