学以致用——Java源码——随机绘制字符串(Random Characters)

程序功能:

绘制8个字符串(内容、字体颜色、字体大小、显示位置均为随机设置)。

运行示例:

学以致用——Java源码——随机绘制字符串(Random Characters)

学以致用——Java源码——随机绘制字符串(Random Characters)

源码:

1. 实体类

import java.security.SecureRandom;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/**
 * 13.10 (Random Characters) Write an application that randomly draws characters
 * in different fonts, sizes and colors.
 * 
 * @author [email protected]
 * @Date Jan 22, 2019, 17:16:03 PM
 *
 */
public class RandomCharactersJPanel extends JPanel
{
	final SecureRandom rn = new SecureRandom();
	public void paint(Graphics g)
	{
		   super.paintComponents(g);
		   int width = getWidth(); // total width   
		   int height = getHeight(); // total height
		   int rRed;
		   int rGreen;
		   int rBlue;
		   String[] texts = {"Life is limited, but art is long!","生命有限,艺术永恒!","吾生也有涯,而知也无涯"};
		   int xCordinate;
		   int yCordinate;
		   String[] fontFamilyNames = {"Serif","SansSerif", "宋体","黑体"};
		   int fontNameNum;
		   int fontStyleNum;
		   int fontSize;
		   int textNum;
		   
		   //随机显示字符
		   for (int i = 8; i > 0;i--){
			   //Color represented in RGB mode
			   rRed = rn.nextInt(256);
			   rGreen = rn.nextInt(256);
			   rBlue = rn.nextInt(256);
			   Color color=new Color(rRed, rGreen, rBlue);
			   g.setColor(color);	//设置颜色
			   xCordinate = rn.nextInt(width);
			   yCordinate = rn.nextInt(height);
			   fontNameNum = rn.nextInt(fontFamilyNames.length);	//随机设置字体类型
			   fontStyleNum = rn.nextInt(3);	//随机设置字体风格(FONT.PLAIN, FONT.BOLD, FONT.ITALIC)
			   fontSize = 10+rn.nextInt(36);	//随机设置字体大小
			   textNum = rn.nextInt(texts.length);	//随机设置要绘制的字符串
			   g.setFont(new Font(fontFamilyNames[fontNameNum],
					   fontStyleNum,fontSize));
			   g.drawString(texts[textNum],xCordinate,yCordinate);

			   }	   

	}
		
}

2. 测试类

 

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JTextArea;



public class DrawRandomCharacters {
	
	static JTextArea statusBar = new JTextArea();
	
	public static void main(String[] args)
	{
	 // create a panel that contains our drawing
		RandomCharactersJPanel panel = new RandomCharactersJPanel();
		
		MouseHandler handler = new MouseHandler(); 
		panel.addMouseMotionListener(handler);

		
	 // create a new frame to hold the panel
	 JFrame application = new JFrame();
	 application.setTitle("绘制随机字符");
	 
	 // set the frame to exit when it is closed
	 application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	 application.add(panel,BorderLayout.CENTER); // add the panel to the frame
	 application.add(statusBar,BorderLayout.SOUTH); // add the statusBar to the frame
	 application.setSize(460, 360); // set the size of the frame
	 application.setVisible(true); // make the frame visible    
	} 
	
	static class MouseHandler extends  MouseMotionAdapter 
	{
	   
	   // handle event when mouse enters area
		@Override
	   public void mouseMoved(MouseEvent event)
	   {  
			 statusBar.setText(String.format("光标当前坐标:[%d, %d]", 
		 	            event.getX(), event.getY()));;
	   }

	}
	

}