📄 intro.java
字号:
/* * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * -Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduct the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended for * use in the design, construction, operation or maintenance of any nuclear * facility. */package java2d;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.image.BufferedImage;import java.awt.image.DataBuffer;import java.awt.font.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.table.*;import javax.swing.event.*;import java.util.Vector;import java.util.List;import java.util.Arrays;/** * Introduction to the Java2Demo. * * @version @(#)Intro.java 1.19 03/06/26 * @author Brian Lichtenwalter */public class Intro extends JPanel { static Color black = new Color(20, 20, 20); static Color white = new Color(240, 240, 255); static Color red = new Color(149, 43, 42); static Color blue = new Color(94, 105, 176); static Color yellow = new Color(255, 255, 140); static Surface surface; private ScenesTable scenesTable; private boolean doTable; public Intro() { EmptyBorder eb = new EmptyBorder(80,110,80,110); BevelBorder bb = new BevelBorder(BevelBorder.LOWERED); setBorder(new CompoundBorder(eb,bb)); setLayout(new BorderLayout()); setBackground(Color.gray); setToolTipText("click for scene table"); add(surface = new Surface()); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { removeAll(); if ((doTable = !doTable)) { setToolTipText("click for animation"); surface.stop(); if (scenesTable == null) { scenesTable = new ScenesTable(); } add(scenesTable); } else { setToolTipText("click for scene table"); surface.start(); add(surface); } revalidate(); repaint(); } }); } public void start() { if (!doTable) { surface.start(); } } public void stop() { if (!doTable) { surface.stop(); } } public static void main(String argv[]) { final Intro intro = new Intro(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { intro.start(); } public void windowIconified(WindowEvent e) { intro.stop(); } }; JFrame f = new JFrame("Java2D Demo - Intro"); f.addWindowListener(l); f.getContentPane().add("Center", intro); f.pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int w = 720; int h = 510; f.setLocation(screenSize.width/2 - w/2, screenSize.height/2 - h/2); f.setSize(w, h); f.setVisible(true); intro.start(); } /** * ScenesTable is the list of scenes known to the Director. * Scene participation, scene name and scene pause amount columns. * Global animation delay for scene's steps. */ static class ScenesTable extends JPanel implements ActionListener, ChangeListener { private JTable table; private TableModel dataModel; public ScenesTable() { setBackground(Color.white); setLayout(new BorderLayout()); final String[] names = { "", "Scenes", "Pause" }; dataModel = new AbstractTableModel() { public int getColumnCount() { return names.length; } public int getRowCount() { return surface.director.size();} public Object getValueAt(int row, int col) { Surface.Scene scene = (Surface.Scene) surface.director.get(row); if (col == 0) { return scene.participate; } else if (col == 1) { return scene.name; } else { return scene.pauseAmt; } } public String getColumnName(int col) {return names[col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { return col != 1 ? true : false; } public void setValueAt(Object aValue, int row, int col) { Surface.Scene scene = (Surface.Scene) surface.director.get(row); if (col == 0) { scene.participate = aValue; } else if (col == 1) { scene.name = aValue; } else { scene.pauseAmt = aValue; } } }; table = new JTable(dataModel); TableColumn col = table.getColumn(""); col.setWidth(16); col.setMinWidth(16); col.setMaxWidth(20); col = table.getColumn("Pause"); col.setWidth(60); col.setMinWidth(60); col.setMaxWidth(60); table.sizeColumnsToFit(0); JScrollPane scrollpane = new JScrollPane(table); add(scrollpane); JPanel panel = new JPanel(new BorderLayout()); JButton b = new JButton("Unselect All"); b.setHorizontalAlignment(JButton.LEFT); Font font = new Font("serif", Font.PLAIN, 10); b.setFont(font); b.addActionListener(this); panel.add("West", b); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 200, (int) surface.sleepAmt); slider.addChangeListener(this); TitledBorder tb = new TitledBorder(new EtchedBorder()); tb.setTitleFont(font); tb.setTitle("Anim delay = " + String.valueOf(surface.sleepAmt) + " ms"); slider.setBorder(tb); slider.setPreferredSize(new Dimension(140,40)); slider.setMinimumSize(new Dimension(100,40)); slider.setMaximumSize(new Dimension(180,40)); panel.add("East", slider); add("South", panel); } public void actionPerformed(ActionEvent e) { JButton b = (JButton) e.getSource(); b.setSelected(!b.isSelected()); b.setText(b.isSelected() ? "Select All" : "Unselect All"); for (int i = 0; i < surface.director.size(); i++) { Surface.Scene scene = (Surface.Scene) surface.director.get(i); scene.participate = new Boolean(!b.isSelected()); } table.tableChanged(new TableModelEvent(dataModel)); } public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider) e.getSource(); int value = slider.getValue(); TitledBorder tb = (TitledBorder) slider.getBorder(); tb.setTitle("Anim delay = " + String.valueOf(value) + " ms"); surface.sleepAmt = (long) value; slider.repaint(); } } // End ScenesTable class /** * Surface is the stage where the Director plays its scenes. */ static class Surface extends JPanel implements Runnable { static Surface surf; static Image cupanim, java_logo; static BufferedImage bimg; public Director director; public int index; public long sleepAmt = 30; private Thread thread; public Surface() { surf = this; setBackground(black); setLayout(new BorderLayout()); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); cupanim = DemoImages.getImage("cupanim.gif", this); java_logo = DemoImages.getImage("java_logo.png", this); director = new Director(); } static FontMetrics getMetrics(Font font) { return surf.getFontMetrics(font); } public void paint(Graphics g) { Dimension d = getSize(); if (d.width <= 0 || d.height <= 0) { return; } if (bimg == null || bimg.getWidth() != d.width || bimg.getHeight() != d.height) { bimg = getGraphicsConfiguration().createCompatibleImage(d.width, d.height); // reset future scenes for (int i = index+1; i < director.size(); i++) { ((Scene) director.get(i)).reset(d.width, d.height); } } Scene scene = (Scene) director.get(index); if (scene.index <= scene.length) { if (thread != null) { scene.step(d.width, d.height); } Graphics2D g2 = bimg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setBackground(getBackground()); g2.clearRect(0, 0, d.width, d.height); scene.render(d.width, d.height, g2); if (thread != null) { // increment scene.index after scene.render scene.index++; } g2.dispose(); } g.drawImage(bimg, 0, 0, this); } public void start() { if (thread == null) { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Intro"); thread.start(); } } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -