Swing-不规则形状绘制(圆弧)
程序员文章站
2022-07-12 22:39:39
...
核心代码:
package swing0401;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.TextAttribute;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
public class MyPanel extends JPanel
{
public MyPanel()
{
}
@Override
protected void paintComponent(Graphics g)
{
int width=this.getWidth();
int height=this.getHeight();
Graphics2D g2d=(Graphics2D)g;
//平滑绘制
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
//指定一个外围矩形
Rectangle rect=new Rectangle(30,30,100,100);
g2d.setPaint(Color.DARK_GRAY);
g2d.draw(rect);
g2d.setPaint(Color.GREEN);
//创建一个Arc(弧)
//Arc2D.OPEN(圆弧) ,Arc2D.CHORD(弓形) ,Arc2D.PIE(扇形)
Shape shape=new Arc2D.Double(rect,0,120,Arc2D.PIE);//120代表跨度
g2d.draw(shape);
}
}