Tue Nov 11, 2008 6:07 pm
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class ShowRainbowColors extends JComponent {
BufferedImage bufferedImage;
public void initialize() {
int wd= getSize().width;
int ht = getSize().height;
int[] data = new int[wd * ht];
int index = 0;
for (int j = 0; j < ht; j++) {
int red = (j * 255) / (ht - 1);
for (int k = 0; k < wd; k++) {
int green = (k* 255) / (wd - 1);
int blue = 128;
data[index++] = (red << 16) | (green << 8) | blue;
}
}
bufferedImage = new BufferedImage(wd, ht, BufferedImage.
TYPE_INT_RGB);
bufferedImage.setRGB(0, 0, wd, ht, data, 0, wd);
}
public void paint(Graphics g) {
if (bufferedImage == null)
initialize();
g.drawImage(bufferedImage, 0, 0, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Show Rainbow Colors");
frame.getContentPane().add(new ShowRainbowColors());
frame.setSize(450, 300);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.