Java6学习笔记48——制作Splash

1.方法一:

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;

public class AnomalyCanvas extends Canvas {
private Image splash;
private boolean splashMode;

AnomalyCanvas(Image image) {
splash = image;
splashMode = true;
}
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics;

if(splashMode) {
double w = (double)getWidth() / (double)splash.getWidth(this);
double h = (double)getHeight() / (double)splash.getHeight(this);
double scale = Math.max(w,h);//填充
AffineTransform splashAffine = new AffineTransform();//图像变换类
splashAffine.setToScale(scale,scale);
g.setTransform(splashAffine);//覆盖变换尺寸
g.drawImage(splash,0,0,this);//显示
return;
}
}
}

import java.awt.Container;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;

public class Anomaly extends JFrame {
private static final long serialVersionUID = 100L;
private AnomalyCanvas canvas;
private String splashFileName = "anomalysplash";

public static void main(String[] arg) {
new Anomaly();
}
public Anomaly() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );
setJMenuBar(buildMenuBar());
Image image = Toolkit.getDefaultToolkit().getImage(splashFileName);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,1);
try {
mt.waitForAll();
} catch(Exception e) {
System.err.println(e);
return;
}
if(image == null) {
System.err.println("Unable to load image from file: "
+ splashFileName);
return;
}
Container container = getContentPane();
BoxLayout layout = new BoxLayout(container,BoxLayout.Y_AXIS);
container.setLayout(layout);
canvas = new AnomalyCanvas(image);
container.add(canvas);
pack();
setExtendedState(MAXIMIZED_BOTH);
canvas.repaint();
setVisible(true);
}
private JMenuBar buildMenuBar() {
JCheckBoxMenuItem hideTemplateMenuItem;
JCheckBoxMenuItem verboseMenuItem;
JMenuItem runMenuItem;
JMenuItem stepMenuItem;
JMenuItem anomalySearchMenuItem;
JMenu menu;
JMenuItem menuItem;
JMenuBar menuBar = new JMenuBar();

menu = new JMenu("File");
menu.getPopupMenu().setLightWeightPopupEnabled(false);
menuBar.add(menu);
menuItem = new JMenuItem("Load Scanned Image");
menu.add(menuItem);
menuItem = new JMenuItem("Load Template Image");
menu.add(menuItem);
menuItem = new JMenuItem("Reset");
menu.add(menuItem);
menuItem = new JMenuItem("Exit");
menu.add(menuItem);

menu = new JMenu("View");
menu.getPopupMenu().setLightWeightPopupEnabled(false);
menuBar.add(menu);
menuItem = new JMenuItem("Toggle Annotation");
menu.add(menuItem);
menuItem = new JMenuItem("Memory Usage");
menu.add(menuItem);
hideTemplateMenuItem = new JCheckBoxMenuItem("Hide Template");
menu.add(hideTemplateMenuItem);
verboseMenuItem = new JCheckBoxMenuItem("Verbose");
verboseMenuItem.setSelected(true);
menu.add(verboseMenuItem);

menu = new JMenu("Anomaly");
menu.getPopupMenu().setLightWeightPopupEnabled(false);
menuBar.add(menu);
stepMenuItem = new JMenuItem("Step");
stepMenuItem.setEnabled(false);
menu.add(stepMenuItem);
runMenuItem = new JMenuItem("Run");
runMenuItem.setEnabled(false);
menu.add(runMenuItem);
menuItem = new JMenuItem("Clear");
menu.add(menuItem);
anomalySearchMenuItem = new JMenuItem("Anomaly Search");
anomalySearchMenuItem.setEnabled(false);
menu.add(anomalySearchMenuItem);
menuItem = new JMenuItem("Settings");
menu.add(menuItem);
JMenu submenu = new JMenu("Appearance");
submenu.getPopupMenu().setLightWeightPopupEnabled(false);
ButtonGroup bg = new ButtonGroup();
menuItem = new JRadioButtonMenuItem("Dashed Cyan Outline");
submenu.add(menuItem);
bg.add(menuItem);
menu.add(submenu);
menuItem = new JRadioButtonMenuItem("Solid Red");
submenu.add(menuItem);
bg.add(menuItem);
menuItem = new JRadioButtonMenuItem("Black Outline");
submenu.add(menuItem);
bg.add(menuItem);
menuItem = new JRadioButtonMenuItem("White Outline");
submenu.add(menuItem);
bg.add(menuItem);
submenu.add(menuItem);
menu.add(submenu);

menu = new JMenu("Help");
menu.getPopupMenu().setLightWeightPopupEnabled(false);
menuBar.add(menu);
menuItem = new JMenuItem("About");
menu.add(menuItem);

return(menuBar);
}
}

方法二:使用SplashScreen类

import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.AlphaComposite;
import java.awt.Container;
import java.awt.Component;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SplashDemo extends JFrame {
public static void main (String args[]) {
new SplashDemo();
}
public SplashDemo() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
buildFrame();
setLocation(250,150);

showSplash();
setVisible(true);
}
private void showSplash() {
SplashScreen splashscreen = SplashScreen.getSplashScreen();
if(splashscreen == null) {
System.out.println("Splash image not specified");
return;
}
Graphics2D g2 = splashscreen.createGraphics();
for(int i=0; i<60; i++) {
if(i < 30)
textOnSplash(g2,"Greasing poles");
else if(i < 45)
textOnSplash(g2,"Shoveling snow");
else
textOnSplash(g2,"Stacking plates");
splashscreen.update();
try {
Thread.sleep(90);
} catch(InterruptedException e) {
}
}
splashscreen.close();
}
private void buildFrame() {
Container pane = getContentPane();
BoxLayout boxlayout = new BoxLayout(pane,BoxLayout.Y_AXIS);
pane.setLayout(boxlayout);

JButton button = new JButton("Focus");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(button);

Marker marker = new Marker(200,200);
pane.add(marker);

pack();
}
private void textOnSplash (Graphics2D g2,String str) {
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(120,130,200,40);//清理出一个矩形区域
g2.setPaintMode();
g2.setColor(Color.BLACK);
g2.drawString(str,120,140);
}
}

运行:java -splash:splash.gif SplashDemo

你可能感兴趣的:(java,thread,swing)