程序功能:
使用随机输出的线段作为屏保程序,用户可随时指定屏幕上要显示的线段数量。
运行示例:
源码:
1. 实体类
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.security.SecureRandom;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
/**
* 13.18 (Screen Saver) Write an application that simulates a screen saver. The
* application should randomly draw lines using method drawLine of class
* Graphics. After drawing 100 lines, the application should clear itself and
* start drawing lines again. To allow the program to draw continuously, place a
* call to repaint as the last line in method paintComponent. Do you notice any
* problems with this on your system?
*
* 13.19 (Screen Saver Using Timer) Package javax.swing contains a class called
* Timer that is capable of calling method actionPerformed of interface
* ActionListener at a fixed time interval (specified in milliseconds). Modify
* your solution to Exercise 13.18 to remove the call to repaint from method
* paintComponent. Declare your class to implement ActionListener. (The
* actionPerformed method should simply call repaint.) Declare an instance
* variable of type Timer called timer in your class. In the constructor for
* your class, write the following statements: timer = new Timer(1000, this);
* timer.start(); This creates an instance of class Timer that will call this
* object’s actionPerformed method every 1000 milliseconds (i.e., every second).
*
* 13.20 (Screen Saver for a Random Number of Lines) Modify your solution to
* Exercise 13.19 to enable the user to enter the number of random lines that
* should be drawn before the application clears itself and starts drawing lines
* again. Use a JTextField to obtain the value. The user should be able to type
* a new number into the JTextField at any time during the program’s execution.
* Use an inner class to perform event handling for the JTextField.
*
* @author Pandenghuang@163.com
* @Date Jan 23, 2019, 11:26:48 PM
*
*/
public class ScreenSaverRandomLinesJPanel extends JPanel implements ActionListener
{
private int lines; //随机显示的线段数
public ScreenSaverRandomLinesJPanel(int lines) {
Timer timer = new Timer(1000, this);
timer.start();
this.lines = lines; //初始化
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paint(g);
double width = getWidth(); // total width
double height = getHeight(); // total height
int rRed;
int rGreen;
int rBlue;
final SecureRandom rn = new SecureRandom();
//画8个同心圆,每个同心圆相距10个像素
for (int i = lines; 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);
g2d.setColor(color); //设置线段颜色
double startX = 1+width*rn.nextDouble();
double startY = 1+height*rn.nextDouble();
double endX = 1+width*rn.nextDouble();
double endY = 1+height*rn.nextDouble();
float thickness = (float) 1 + rn.nextInt(20);
g2d.setStroke(new BasicStroke(thickness)); //设置线段的粗细
g2d.draw(new Line2D.Double(startX, startY,
endX, endY)); //绘制线段
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
repaint();
}
public int getLines() {
return this.lines;
}
public void setLines(int lines) {
this.lines = lines;
}
}
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 ScreenSaverRandomLinesTest {
public static void main(String[] args)
{
// create a panel that contains our drawing
final int LINES = 100;
ScreenSaverRandomLinesJPanel panel = new ScreenSaverRandomLinesJPanel(LINES);
JTextField linesJTextField = new JTextField(String.valueOf(LINES),20); //要显示的随机线段数
linesJTextField.addActionListener(new ActionListener() { //监听文本框,设置要显示的线段数量
@Override
public void actionPerformed(ActionEvent e) {
panel.setLines(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
}
}