源码:
Screen.java
import java.awt.*;
import javax.swing.*;
class Screen extends JFrame {
public Screen() {
DisplayMode displayMode = new DisplayMode(1024, 768, 16,
DisplayMode.REFRESH_RATE_UNKNOWN);
run(displayMode);
}
public void run(DisplayMode displayMode) {
setBackground(Color.black);
setForeground(Color.white);
setFont(new Font("Dialog", 0, 24));
ScreenManager screen = new ScreenManager();
try {
screen.setFullScreen(displayMode, this);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
diamond(g);
}
public void diamond(Graphics g) {
int x[] = new int[100];
int y[] = new int[100];
int r = 300, n = 20, x0 = 512, y0 = 384;
double t;
t = 2 * Math.PI / n;
for(int i = 0; i < n; i++) {
x[i] = x0 + (int)(r * Math.cos(t * i));
y[i] = y0 + (int)(r * Math.sin(t * i));
}
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
g.drawLine(x[i], y[i], x[j], y[j]);
}
}
}
}
ScreenManager.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScreenManager {
private GraphicsDevice device;
public ScreenManager() {
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode displayMode, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
device.setFullScreenWindow(window);
if (displayMode != null && device.isDisplayChangeSupported()) {
try {
device.setDisplayMode(displayMode);
}
catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
window.addMouseListener(new MouseL(window));
}
public Window getFullScreenWindow() {
return device.getFullScreenWindow();
}
public void restoreScreen() {
Window window = device.getFullScreenWindow();
if (window != null) {
window.dispose();
}
device.setFullScreenWindow(null);
}
class MouseL extends MouseAdapter {
JFrame window;
public MouseL(JFrame window) {
this.window = window;
}
public void mouseClicked(MouseEvent e) {
JPanel pwP = new JPanel();
JTextField pwTF = new JTextField(5);
pwP.add(new JLabel("PassWord"));
pwP.add(pwTF);
if(JOptionPane.showConfirmDialog(window, pwP, "PassWord",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)== JOptionPane.OK_OPTION) {
if((pwTF.getText()).equals("pluto")) {
window.dispose();
}
else {
JPanel errorP = new JPanel();
errorP.add(new JLabel("对不起,您输入的密码是错误的"));
JOptionPane.showMessageDialog(window, errorP, "Error", JOptionPane.YES_OPTION);
}
}
}
}
}
ScreenSaver.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScreenSaver {
JFrame mainF;
JMenuItem diamondMI;
JMenuItem infoMI;
public ScreenSaver() {
mainF = new JFrame();
addMenu();
addComponent();
showFrame();
}
private void addMenu() {
JMenuBar mainMB = new JMenuBar();
mainF.setJMenuBar(mainMB);
JMenu shapeM = new JMenu("Shape");
mainMB.add(shapeM);
diamondMI = new JMenuItem("金刚石");
diamondMI.addActionListener(new MenuL());
shapeM.add(diamondMI);
JMenu helpM = new JMenu("Help");
mainMB.add(helpM);
infoMI = new JMenuItem("Info");
infoMI.addActionListener(new MenuL());
helpM.add(infoMI);
}
private void addComponent() {
}
private void showFrame() {
mainF.setTitle("ScreenSaver");
mainF.setSize(400,400);
mainF.setResizable(false);
mainF.setLocation(200,100);
mainF.setVisible(true);
}
class MenuL implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object command = e.getSource();
if(command == diamondMI) {
startupDlg();
}
else if(command == infoMI) {
infoDlg();
}
}
public void startupDlg() {
new Screen();
}
public void infoDlg() {
JPanel msgP = new JPanel();
msgP.setLayout(new GridLayout(3, 1));
msgP.add(new JLabel("Author : pluto"));
msgP.add(new JLabel("Date : 2005.4"));
msgP.add(new JLabel("Site : BJTU15#"));
JOptionPane.showMessageDialog(null, msgP, "Info", JOptionPane.YES_OPTION);
}
}
public static void main(String[] args) {
new ScreenSaver();
}
}