Graphics 提供基礎幾何繪製方法,Graphics2D 繼承並增強(筆觸、填充、變換、抗鋸齒)。常用方法分類:
drawLine(x1,y1,x2,y2)drawRect/fillRect、圓角 drawRoundRect/fillRoundRect、3D draw3DRect/fill3DRectdrawOval/fillOval (寬高相等為圓)drawArc/fillArc(x,y,w,h,start,extent) 角度單位度,0度在3點鐘方向,逆時針為正drawPolygon/fillPolygon(xPoints[], yPoints[], nPoints) 或 Polygon 對象clearRect(x,y,w,h) 用背景色填充clipRect(x,y,w,h) 限制繪製範圍,多次調用取交集copyArea(x,y,w,h,dx,dy) 偏移複製像素// 繪製填充圓角矩形
g.setColor(Color.BLUE);
g.fillRoundRect(50, 50, 200, 100, 20, 20);
// 繪製多邊形 (三角形)
int[] xs = {150, 100, 200};
int[] ys = {100, 200, 200};
g.fillPolygon(xs, ys, 3);
實際開發建議轉型為 Graphics2D 使用 setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON) 開啟抗鋸齒,配合 Stroke、Paint 實現虛線、漸變、紋理填充。