学以致用——Java源码——使用随机图形制作屏保程序(Screen Saver with Shapes)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/86619860

程序功能:

使用随机输出的图形作为屏保程序,用户可随时指定屏幕上要显示的图形元素的数量。

运行示例:

 

源码:

1. 实体类

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.security.SecureRandom;

/**
 * 13.21 (Screen Saver with Shapes) Modify your solution to Exercise 13.20 such
 * that it uses random number generation to choose different shapes to display.
 * Use methods of class Graphics.
 * 
 * @author [email protected]
 * @Date Jan 24, 2019, 12:36:39 AM
 *
 */
public class RandomShapesJPanel extends JPanel implements ActionListener
{
	private int elements;	//随机显示的线段数
	public void paint(Graphics g)
	{
		   super.paint(g);
		   int width = getWidth(); // total width   
		   int height = getHeight(); // total height
		   int rR;
		   int rG;
		   int rB;
		   int rShape;
		   int rX;
		   int rY;
		   int rWidth;
		   int rHeight;
		   int rRaised; //是否凸起,用于draw3DRect方法
		   int rStartAngle;	//绘制圆弧时的起始角度
		   int rArcAngle;	//绘制圆弧时的终止角度
		   byte[] bytes = {65,66,67};	//绘制由字节数组表示的字符,适用于英文字符(ASCII 0-255)

		   final SecureRandom rn = new SecureRandom();
		   
		   for (int i = elements; i > 0;i--){

			   rR=rn.nextInt(256);
			   rG=rn.nextInt(256);
			   rB=rn.nextInt(256);
			   Color c=new Color(rR, rG, rB);
			   g.setColor(c);
			   
			   rShape=rn.nextInt(5);
			   rRaised=rn.nextInt(2);
			   rStartAngle = rn.nextInt(360);
			   rArcAngle = rn.nextInt(360);
			   rX=rn.nextInt(width);
			   rY=rn.nextInt(height);
			   rWidth=rn.nextInt(width/2);
			   rHeight=rn.nextInt(height/2);
			   
			   //定义随机图形的种类
			   switch(rShape){
			   case 0:
				   g.fillOval(rX,rY,rWidth,rHeight);
				   break;
			   case 1:
				   g.fillRect(rX,rY,rWidth,rHeight);
				   break;
			   case 2:
				   g.draw3DRect(rX, rY, rWidth, rHeight, (rRaised ==0? false:true));
				   break;
			   case 3:
				   g.drawArc(rX, rY, rWidth, rHeight, rStartAngle, rArcAngle);
				   break;
			   case 4:
				   g.drawBytes(bytes,0,bytes.length,rX,rY);
			   }
			  }

	}
	
	public RandomShapesJPanel(int elements) {
		Timer timer = new Timer(1000, this);
		timer.start(); 
		this.elements= elements; //初始化要显示的元素个数

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		repaint();
		
	}
	
	public int getElements() {
		return this.elements;
	}
	
	public void setElements(int elements) {
		this.elements = elements;
	}
} 

2. 测试类

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;



public class RandomShapesTest {

	
	public static void main(String[] args)
	{
	 // create a panel that contains our drawing
	 final int ELEMENTS = 20;
	 RandomShapesJPanel panel = new RandomShapesJPanel(ELEMENTS);
	 JTextField linesJTextField = new JTextField(String.valueOf(ELEMENTS),20);	//可输入要显示得随机图形数
	 linesJTextField.addActionListener(new ActionListener() {	//监听文本框,设置要显示的图形元素的数量
		@Override
		public void actionPerformed(ActionEvent e) {
			panel.setElements(Integer.parseInt(linesJTextField.getText()));
		 }
	  });

	 // 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(linesJTextField,BorderLayout.SOUTH);	
	 application.add(panel,BorderLayout.CENTER); // add the panel to the frame
	 application.setSize(460, 360); // set the size of the frame
	 application.setVisible(true); // make the frame visible    
	} 
	

	}

猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/86619860