📄 exercise14_15.java
字号:
// Exercise14_15.java: Display an image in different sizesimport javax.swing.*;import java.awt.*;import java.net.URL;public class Exercise14_15 extends JApplet { private Image image; public void init() { // Get the URL for the file name URL url = this.getClass().getResource("blackTicker.gif"); // Obtain a Toolkit instance Toolkit toolkit = Toolkit.getDefaultToolkit(); // Get the image image = toolkit.getImage(url); getContentPane().add(new DisplayImage()); } // Main method public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("Exercise14_15"); // Create an instance of the applet Exercise14_15 applet = new Exercise14_15(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Invoke init() and start() applet.init(); applet.start(); // Display the frame frame.setSize(500, 400); frame.setVisible(true); }// Inner classclass DisplayImage extends JPanel implements Runnable { private Thread thread = null; int width = 300; int height = 300; int delta = -10; public DisplayImage() { thread = new Thread(this); thread.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, width, height, this); try { Thread.sleep(50); } catch (InterruptedException ex) { } if (width < 50) { delta = 1; } else if (width > 300) { delta = -1; } width = width + delta; height = height + delta; } public void run() { while (true) { repaint(); try { thread.sleep(50); } catch (InterruptedException ex) { } } }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -