📄 whirlinglogo.java
字号:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.net.*;
import java.awt.geom.*; // For AffineTransform
public class WhirlingLogo extends JApplet
implements Runnable
{
// This method is called when the applet is loaded
public void init()
{
tracker = new MediaTracker(this);
Image image = null;
try
{
// Image from a file specified by a URL
image = getImage(new URL(getCodeBase(),"Images/wrox_logo.gif"));
}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
}
tracker.addImage(image,0);
try
{
tracker.waitForAll(); // Wait for image to load
if(tracker.isErrorAny()) // If there is an error
return; // give up
Dimension size = getSize(); // Get applet size
imageWidth = image.getWidth(this); // Get image width
imageHeight = image.getHeight(this); // and its height
// Calculate scale factor so diagonal of image fits width and height
double diagonal = Math.sqrt(imageWidth*imageWidth + imageHeight*imageHeight);
double scaleFactor = Math.min(size.width/diagonal, size.height/diagonal);
// Create a transform to translate and scale the image
at.setToTranslation((size.width-imageWidth*scaleFactor)/2,
(size.height-imageHeight*scaleFactor)/2);
at.scale(scaleFactor,scaleFactor);
imagePanel = new ImagePanel(image); // Create panel showing the image
getContentPane().add(imagePanel); // Add the panel to the content pane
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
// This method is called when the browser starts the applet
public void start()
{
if(tracker.isErrorAny()) // If any image errors
return; // don't create the thread
whirler = new Thread(this); // Create the animation thread
whirler.start(); // and start it
}
// This method is called when the browser want to stops the applet
// when it's not visible for example
public void stop()
{
whirler = null; // Discard the thread
}
// This method is called when the animation thread is started
public void run()
{
long time = System.currentTimeMillis(); // Starting time
long interval = 50; // Time interval msec
angle = 0; // Initial rotation angle
double angleIncrement = interval*Math.PI/1000; // Complete rotation in 2 secs
// Move image while the whirler thread is running
while(Thread.currentThread() == whirler)
{
imagePanel.repaint(); // Repaint the image
// Wait until the end of the next interval
try
{
time += interval;
angle += angleIncrement%(2*Math.PI); // Increment the rotation angle
Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
}
}
class ImagePanel extends JPanel
{
public ImagePanel(Image image)
{
this.image = image;
}
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
g2D.transform(at); // Apply scale & translate
g2D.setPaint(Color.lightGray); // Set a background color
g2D.fillRect(0, 0, imageWidth, imageHeight); // paint background
g2D.rotate(angle, imageWidth/2.0, imageHeight/2.0); // Rotate about center
// draw scaled image with background
g2D.drawImage(image, 0, 0, this);
}
Image image; // The image
}
Thread whirler; // Animation thread
MediaTracker tracker; // Tracks image loading
ImagePanel imagePanel;
AffineTransform at = new AffineTransform();
int imageWidth, imageHeight; // Image dimensions
double angle; // Rotation angle
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -