📄 movie.java
字号:
/** Framework for movies.
To define and show a movie,
define a class that extends Movie.
It must include a scene(t) method that returns
the Picture for any given time t.
Create an object of this class,
and call its 'show' method.
(5 Feb 00)
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
abstract class Movie implements ActionListener {
private Timer timer;
// Animation timer.
private double movieTime;
// Current time measured in movie time units.
private Picture picture;
// Current frame of the movie.
private AffineTransform convert;
// Transformation that converts from
// picture coordinates to user coordinates.
private JPanel panel;
// Panel for displaying the movie.
private double frameRate = 20;
// Number of pictures displayed per second.
private double movieRate = 20;
// Number of units of movie time per second.
private boolean antialiasOn = true;
/** Create screen and start the movie.
(x,y) = bottom-left corner of area of pictures
to be displayed.
(w,h) = width and height of area to be displayed.
s = Number of pixels per unit distance in picture.
b = background colour. (Can be null.)
*/
public void show
(double x, double y, double w,
double h, double s, Color b) {
/* Create display area. */
panel = new DisplayPanel();
// (DisplayPanel is defined below.)
int width = (int) (w*s);
int height = (int) (h*s);
panel.setPreferredSize(new Dimension(width, height));
if (b != null) panel.setBackground(b);
JFrame frame = new JFrame();
frame.setLocation(100,100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
/* Create transformation 'convert' that converts
from picture coordinates to user coordinates. */
convert = new AffineTransform();
convert.scale(s,s);
convert.translate(0,h);
convert.scale(1,-1);
convert.translate(-x,-y);
/* Create Timer. */
int delay = (int) Math.round(1000.0/frameRate);
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);
/* Start the movie. */
movieTime = 0;
timer.start();
}
/** Set movie speed
(= number of movie time units per second).
*/
public void setMovieRate(double r) {
movieRate = r;
}
/** Set number of frames per second.
*/
public void setFrameRate(double r) {
frameRate = r;
}
/** If b = true, turn antialiasing on.
If b = false, turn it off.
*/
public void setAntialiasingOn(boolean b) {
antialiasOn = b;
}
public void actionPerformed(ActionEvent e) {
picture = scene(movieTime);
movieTime = movieTime + movieRate/frameRate;
panel.repaint();
}
/** Return the picture for time t.
*/
abstract Picture scene(double t);
/****************************************************/
/* JPanel which displays the Picture
stored in 'picture'.
*/
private class DisplayPanel extends JPanel {
public void paintComponent(Graphics g) {
if (picture == null) return;
final AffineTransform ident = new AffineTransform();
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.transform(convert);
if (antialiasOn)
g2.setRenderingHint
(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
picture.transDisplay(ident,g2);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -