📄 colorpan.java
字号:
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ColorPan extends JComponent {
BufferedImage image;
public void initialize() {
// Returns the size of this component in the form of
// a Dimension object. The height field of the Dimension object
// contains this component's height, and the width field of
// the Dimension object contains this component's width.
int width = getSize().width;
int height = getSize().height;
int[] data = new int[width * height];
int index = 0;
int blue = 128;
for (int i = 0; i < height; i++) {
int red = (i * 255) / (height - 1);
for (int j = 0; j < width; j++) {
int green = (j * 255) / (width - 1);
data[index++] = (red << 16) | (green << 8) | blue;
}
}
// Constructs a BufferedImage of one of the predefined image types.
// The ColorSpace for the image is the default sRGB space.
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Sets an array of integer pixels in the default RGB color model
// (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data.
image.setRGB(0, 0, width, height, data, 0, width);
}
public void paint(Graphics g) {
if (image == null)
initialize();
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
JFrame f = new JFrame("ColorPan");
f.getContentPane().add(new ColorPan());
f.setSize(300, 300);
f.setLocation(100, 100);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -