[英]Unexpected circle border drawn with AWT
使用AWT時,我使用java.awt.Graphics#drawOval
和java.awt.Graphics2D#setStroke
繪制邊框。 對於設定的筆觸尺寸大於橢圓形直徑的情況,所產生的邊框與預期的不同。 在這種情況下,筆畫與圓的另一側的筆畫重疊:圓的北筆畫與南筆畫重疊。 AWT以XOR方式呈現此重疊,如下圖所示。
相反,我期望的是筆觸重疊是以“或”方式繪制的,因此在所有情況下,當stroke width > circle diameter
,中心都是黑色。
即使圓的寬度或高度(然后是橢圓)不相等,我是否可以通過簡單的方法將行為更改為“或”重疊模式?
直徑相同(10px),且筆划寬度增加:
根據Marco13在他的評論中提到的解決方案,我drawOval
了這個自定義的drawOval
函數。 一旦筆划寬度大於直徑,它將基本上從drawOval
切換到fillOval
。 計算fillOval
函數的位置和尺寸以匹配drawOval
輸出。
public static void drawOval(Graphics2D g2d, int strokeWidth, int x, int y, int width, int height) {
int minLength = Math.min(width, height);
int maxLength = Math.max(width, height);
if (minLength >= strokeWidth) {
g2d.drawOval(x, y, width, height);
} else {
int x1 = x - (strokeWidth - maxLength) / 2 - (maxLength / 2);
int y1 = y - (strokeWidth - maxLength) / 2 - (maxLength / 2);
int width1 = width + strokeWidth;
int height1 = height + strokeWidth;
g2d.fillOval(x1, y1, width1, height1);
}
}
看起來像這樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.