简介
java 核心编程
code
/*
* @Author: your name
* @Date: 2020-10-28 22:38:26
* @LastEditTime: 2020-10-28 22:45:52
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /java/ImageTest.java
*/
import java.awt.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args){
EventQueue.invokeLater(()->{
JFrame frame = new ImageFrame();
frame.setTitle("ImageTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});;
}
}
class ImageFrame extends JFrame {
public ImageFrame() {
add(new ImageComponent());
pack();
}
}
class ImageComponent extends JComponent {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private Image image;
public ImageComponent(){
image = new ImageIcon("test.gif").getImage();
}
public void paintComponent(Graphics g) {
if (image == null)
return;
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
// draw the iamge in the upper-left cornet
g.drawImage(image, 0, 0, null);
// tile the iamge across the component
for (int i = 0; i * imageWidth <= getWidth(); i++) {
for (int j = 0; j * imageHeight <= getHeight(); j++) {
if (i + j > 0) {
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);
}
}
}
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}
image

---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》
浙公网安备 33010602011771号