⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawtest.java

📁 这是本人用java编写的一个印章自动生成程序。
💻 JAVA
字号:
/**
   @version 1.31 2004-05-03
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.image.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;

public class DrawTest
{  
   public static void main(String[] args)
   {  
      DrawFrame frame = new DrawFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A frame that contains a panel with drawings
*/
class DrawFrame extends JFrame
{
   public DrawFrame()
   {
      setTitle("DrawTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add panel to frame
      DrawPanel panel = new DrawPanel();
      panel.setBackground(Color.WHITE);
      add(panel, BorderLayout.CENTER);
   }

   public static final int DEFAULT_WIDTH = 400;
   public static final int DEFAULT_HEIGHT = 400;  
}

/**
   A panel that displays rectangles and ellipses. 
*/
class DrawPanel extends JPanel
{  
	private BufferedImage image;
	private String message = "青岛一新软件科技有限公司";
	public DrawPanel()
	{  
		final JTextField textField = new JTextField("青岛一新软件科技有限公司",20);
		add(textField);
		JButton pairuButton = new JButton("排入");
		add(pairuButton);
		pairuButton.addActionListener(new
			ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					message = textField.getText();
					repaint();
				}
			});
		
		// acquire the image
		try
		{
		 	image = ImageIO.read(new File("five.bmp"));
		}
		catch (IOException e)
		{
		 	e.printStackTrace();
		}
	}
	
	public void paintComponent(Graphics g)
	{  
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		//得到图片的宽度和高度
		//JPanel 实现了java.awt.image 接口 ImageObserver(用于在构造 Image 时,接收有关 Image 信息通知的异步更新接口)
		//java.awt.image.BufferedImage.getHeight(ImageObserver observer)
		int imageWidth = image.getWidth(this);
		int imageHeight = image.getHeight(this);

      	// draw the image in the upper-left corner
     	g2.drawImage(image, (int)(CENTERX - imageWidth/2), (int)(CENTERY - imageHeight/2), null);
      
		//绘制圆
		int radius = 150;
		Ellipse2D circle = new Ellipse2D.Double();
		circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
		g2.draw(circle);
		
		//根据输入字符串得到字符数组
		String[] messages2 = message.split("",0);
		String[] messages = new String[messages2.length-1];
		System.arraycopy(messages2,1,messages,0,messages2.length-1);
		//输入的字数
		int ilength = messages.length;
		
		//设置字体属性
		int fontsize = 24;
		Font f = new Font("Serif", Font.BOLD, fontsize);

		FontRenderContext context = g2.getFontRenderContext();
		Rectangle2D bounds = f.getStringBounds(message, context);
		
		//字符宽度=字符串长度/字符数
		double char_interval = (bounds.getWidth() / ilength);
		//上坡度
		double ascent = -bounds.getY();

		int first = 0,second = 0;
		boolean odd = false;
		if (ilength%2 == 1)
		{
			first = (ilength-1)/2;
			odd = true;
		}
		else
		{
			first = (ilength)/2-1;
			second = (ilength)/2;
			odd = false;
		}
		
		double radius2 = radius - ascent;
		double x0 = CENTERX;
		double y0 = CENTERY - radius + ascent;
		//旋转角度
		double a = 2*Math.asin(char_interval/(2*radius2));
		
		if (odd)
		{
			g2.setFont(f);
			g2.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0);
			
			//中心点的右边
			for (int i=first+1;i<ilength;i++)
			{
				double aa = (i - first) * a;
				double ax = radius2 * Math.sin(aa);
				double ay = radius2 - radius2 * Math.cos(aa);
				AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
				Font f2 = f.deriveFont(transform);
				g2.setFont(f2);
				g2.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
			}
			//中心点的左边
			for (int i=first-1;i>-1;i--)
			{
				double aa = (first - i) * a;
				double ax = radius2 * Math.sin(aa);
				double ay = radius2 - radius2 * Math.cos(aa);
				AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
				Font f2 = f.deriveFont(transform);
				g2.setFont(f2);
				g2.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
			}
			
		}
		else
		{
			//中心点的右边
			for (int i=second;i<ilength;i++)
			{
				double aa = (i - second + 0.5) * a;
				double ax = radius2 * Math.sin(aa);
				double ay = radius2 - radius2 * Math.cos(aa);
				AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
				Font f2 = f.deriveFont(transform);
				g2.setFont(f2);
				g2.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
			}
			
			//中心点的左边
			for (int i=first;i>-1;i--)
			{
				double aa = (first - i + 0.5) * a;
				double ax = radius2 * Math.sin(aa);
				double ay = radius2 - radius2 * Math.cos(aa);
				AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
				Font f2 = f.deriveFont(transform);
				g2.setFont(f2);
				g2.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
			}
		}
		
	}
	public static final int CENTERX = 200;
   	public static final int CENTERY = 200;  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -