代码实现:
packagecn.hnpi;
importjava.awt.BasicStroke;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
public class DrawcachetPanel extends JPanel {
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
Graphics2D g2 = (Graphics2D) g;
g2.translate(170, 100);// 平移坐标轴
BasicStroke stroke = new BasicStroke(6);
g2.setStroke(stroke);
Ellipse2D.Float ellipse = new Ellipse2D.Float(-80, -80, 160, 160);
Color color = new Color(255, 0, 0);
g2.setColor(color);
g2.draw(ellipse);
// 绘制五星
int[] x1 = { 0, 8, 30, 16, 25, 0, -25, -16, -30, -8 };
int[] y1 = { -35, -10, -15, 5, 28, 10, 28, 5, -15, -10 };
int n1 = 10;
g2.fillPolygon(x1, y1, n1);
// 绘制文本
g2.scale(1.8, 1.8);
Font font = new Font("宋体", Font.BOLD, 12);
g2.setFont(font);
g2.drawString("专用章", -20, 35);
int width = getWidth();
intheigtht = getHeight();
// 将字符串转换为字符数组
char[] array = "**职业技术学院".toCharArray();
intlen = array.length * 3;
font = new Font("宋体", Font.BOLD, 10);
g2.setFont(font);
double angle = 0;
for (int i = 0; i <array.length; i++) {
int x = (int) (len * Math.sin(Math.toRadians(angle + 270)));
int y = (int) (len * Math.cos(Math.toRadians(angle + 270)));
g2.drawString(array[i] + "", width / 2 + x - 148, heigtht / 2 - y
- 94);
angle = angle + 360d / array.length;
}
}
public static void main(String[] args) {
// new DrawcachetPanel();
JFrame f = new JFrame();
f.getContentPane().add(new DrawcachetPanel());
f.setSize(100, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}