最近、私は自分のアプリケーションのプレゼンテーションについていくらか確信していたので、このトピックをより詳しく見るための小さなテスターアプリを作成しました。長方形が期待どおりに描画されない
私は、自分自身の外側の境界線を描画するよりも、何もしなかった独自のコンポーネントクラスを派生し、完全には表示されなかったことに驚いた。シミュレータもiPhone 5Sでもない。
私の質問に:aGraphics.drawRect(getX() + 2, getY() + 2, 10, 10)
として描画されたコンポーネントは、シミュレータとiPhone 5Sで11ピクセル幅と高さで表示されるのはなぜですか?ここで
これを実証するために使用することができますいくつかのコードです:
public class FormRectangleComponent extends Form {
private class RectangleComponent extends Component {
private int color;
public RectangleComponent(int aColor) {
color = aColor;
}
@Override
public void paint(Graphics aGraphics) {
aGraphics.setColor(color);
aGraphics.drawRect(getX(), getY(), getWidth(), getHeight());
Map<String,Object> map = new LinkedHashMap<String,Object>() {{
put("Display.displayWidth", Display.getInstance().getDisplayWidth());
put("rectangleComponent.x", getX());
put("rectangleComponent.y", getY());
put("rectangleComponent.width", getWidth());
put("rectangleComponent.height", getHeight());
}};
aGraphics.setColor(0xffffff);
aGraphics.drawRect(getX() + 2, getY() + 2, 10, 10);
report(map);
}
@Override
protected Dimension calcPreferredSize() {
int side = Display.getInstance().getDisplayHeight()/10;
return new Dimension(side, side);
}
}
public FormRectangleComponent() {
Display.getInstance().areMutableImagesFast();
setTitle("FormRectangleComponent");
setScrollable(false);
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
contentPane.setScrollableY(true);
contentPane.getAllStyles().setBgPainter((Graphics aGraphics, Rectangle aRectangle) -> {
aGraphics.setColor(0x00c0c0);
aGraphics.fillRect(aRectangle.getX(), aRectangle.getY(), aRectangle.getWidth(), aRectangle.getHeight());
});
contentPane.add(new SpanLabel(
"Below should be some frames showing all sides."));
contentPane.add(new RectangleComponent(0xff0000));
contentPane.add(new RectangleComponent(0x0000ff));
contentPane.add(new RectangleComponent(0x00ff00));
}
private void report(Map<String, Object> map) {
int maximumLength = 0;
for (Entry<String, Object> entry: map.entrySet()) {
maximumLength = Math.max(maximumLength, entry.getKey().length());
}
for (Entry<String, Object> entry: map.entrySet()) {
StringBuilder stringBuilder = new StringBuilder();
for (int tally = 0; tally < (maximumLength - entry.getKey().length()); tally ++) {
stringBuilder.append(' ');
}
stringBuilder
.append(entry.getKey())
.append(": ")
.append(entry.getValue());
Log.p(stringBuilder.toString());
}
}
}
私は、Javaで1年間働いており、それに気づいたことはありません。より良い説明は、ペンの類推です。幅のない四角形を描くと、定義された幅を持つペンが移動します。 [http://math.hws.edu/graphicsbook/c2/s5.html]を参照してください。 –