我正在写一个曼德尔布罗分形观众骑自行车的形象,我想实现一个聪明的办法颜色循环。给定一个图像,我想修改它的IndexColorModel。高效彩色在Java中
据我所知,有没有办法来修改一个IndexColorModel,而且也没有办法给出一个图像的新IndexColorModel的。事实上,我认为无法提取其颜色模型或图像数据。
看来,唯一的解决方案是坚持用于创建图像的原始图像数据和调色板,手动创建一个新的调色板与旋转的颜色,创建一个新的IndexColorModel,然后创建一个全新的来自数据和新颜色模型的图像。
这一切都似乎有太多的工作。有更简单快捷的方法吗?
这是我能拿出最好的解决方案。此代码创建一个1000x1000像素的图像,并显示以大约每秒30帧循环的颜色动画。
(旧)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ColorCycler {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame jFrame = new JFrame("Color Cycler");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(new MyPanel());
jFrame.pack();
jFrame.setVisible(true);
}
}
class MyPanel extends JPanel implements ActionListener {
private byte[] reds = new byte[216];
private byte[] greens = new byte[216];
private byte[] blues = new byte[216];
private final byte[] imageData = new byte[1000 * 1000];
private Image image;
public MyPanel() {
generateColors();
generateImageData();
(new Timer(35, this)).start();
}
// The window size is 1000x1000 pixels.
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
// Generate 216 unique colors for the color model.
private void generateColors() {
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++, index++) {
reds[index] = (byte) (i * 51);
greens[index] = (byte) (j * 51);
blues[index] = (byte) (k * 51);
}
}
}
}
// Create the image data for the MemoryImageSource.
// This data is created once and never changed.
private void generateImageData() {
for (int i = 0; i < 1000 * 1000; i++) {
imageData[i] = (byte) (i % 216);
}
}
// Draw the image.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 1000, 1000, null);
}
// This method is called by the timer every 35 ms.
// It creates the modified image to be drawn.
@Override
public void actionPerformed(ActionEvent e) { // Called by Timer.
reds = cycleColors(reds);
greens = cycleColors(greens);
blues = cycleColors(blues);
IndexColorModel colorModel = new IndexColorModel(8, 216, reds, greens, blues);
image = createImage(new MemoryImageSource(1000, 1000, colorModel, imageData, 0, 1000));
repaint();
}
// Cycle the colors to the right by 1.
private byte[] cycleColors(byte[] colors) {
byte[] newColors = new byte[216];
newColors[0] = colors[215];
System.arraycopy(colors, 0, newColors, 1, 215);
return newColors;
}
}
编辑2:
现在我预先计算IndexColorModels。这意味着在每一帧我只需要用一个新的IndexColorModel更新MemoryImageSource。这似乎是最好的解决方案。 (我也注意到在我的分形浏览器中,我可以在每个生成的图像上重新使用一组预先计算好的IndexColorModels,这意味着140K的一次性成本让我可以实时对所有的事物进行实时颜色循环。是伟大的)
下面的代码:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ColorCycler {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame jFrame = new JFrame("Color Cycler");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(new MyPanel());
jFrame.pack();
jFrame.setVisible(true);
}
}
class MyPanel extends JPanel implements ActionListener {
private final IndexColorModel[] colorModels = new IndexColorModel[216];
private final byte[] imageData = new byte[1000 * 1000];
private final MemoryImageSource imageSource;
private final Image image;
private int currentFrame = 0;
public MyPanel() {
generateColorModels();
generateImageData();
imageSource = new MemoryImageSource(1000, 1000, colorModels[0], imageData, 0, 1000);
imageSource.setAnimated(true);
image = createImage(imageSource);
(new Timer(35, this)).start();
}
// The window size is 1000x1000 pixels.
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
// Generate 216 unique colors models, one for each frame.
private void generateColorModels() {
byte[] reds = new byte[216];
byte[] greens = new byte[216];
byte[] blues = new byte[216];
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++, index++) {
reds[index] = (byte) (i * 51);
greens[index] = (byte) (j * 51);
blues[index] = (byte) (k * 51);
}
}
}
for (int i = 0; i < 216; i++) {
colorModels[i] = new IndexColorModel(8, 216, reds, greens, blues);
reds = cycleColors(reds);
greens = cycleColors(greens);
blues = cycleColors(blues);
}
}
// Create the image data for the MemoryImageSource.
// This data is created once and never changed.
private void generateImageData() {
for (int i = 0; i < 1000 * 1000; i++) {
imageData[i] = (byte) (i % 216);
}
}
// Draw the image.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 1000, 1000, null);
}
// This method is called by the timer every 35 ms.
// It updates the ImageSource of the image to be drawn.
@Override
public void actionPerformed(ActionEvent e) { // Called by Timer.
currentFrame++;
if (currentFrame == 216) {
currentFrame = 0;
}
imageSource.newPixels(imageData, colorModels[currentFrame], 0, 1000);
repaint();
}
// Cycle the colors to the right by 1.
private byte[] cycleColors(byte[] colors) {
byte[] newColors = new byte[216];
newColors[0] = colors[215];
System.arraycopy(colors, 0, newColors, 1, 215);
return newColors;
}
}
编辑:(旧)
Heisenbug建议我使用newPixels MemoryImageSource的()方法。答案已经被删除,但结果是一个好主意。现在我只创建一个MemoryImageSource和一个Image。在每一帧我创建一个新的IndexColorModel并更新MemoryImageSource。
下面是更新的代码:(旧)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ColorCycler {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame jFrame = new JFrame("Color Cycler");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(new MyPanel());
jFrame.pack();
jFrame.setVisible(true);
}
}
class MyPanel extends JPanel implements ActionListener {
private byte[] reds = new byte[216];
private byte[] greens = new byte[216];
private byte[] blues = new byte[216];
private final byte[] imageData = new byte[1000 * 1000];
private final MemoryImageSource imageSource;
private final Image image;
public MyPanel() {
generateColors();
generateImageData();
IndexColorModel colorModel = new IndexColorModel(8, 216, reds, greens, blues);
imageSource = new MemoryImageSource(1000, 1000, colorModel, imageData, 0, 1000);
imageSource.setAnimated(true);
image = createImage(imageSource);
(new Timer(35, this)).start();
}
// The window size is 1000x1000 pixels.
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
// Generate 216 unique colors for the color model.
private void generateColors() {
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++, index++) {
reds[index] = (byte) (i * 51);
greens[index] = (byte) (j * 51);
blues[index] = (byte) (k * 51);
}
}
}
}
// Create the image data for the MemoryImageSource.
// This data is created once and never changed.
private void generateImageData() {
for (int i = 0; i < 1000 * 1000; i++) {
imageData[i] = (byte) (i % 216);
}
}
// Draw the image.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 1000, 1000, null);
}
// This method is called by the timer every 35 ms.
// It updates the ImageSource of the image to be drawn.
@Override
public void actionPerformed(ActionEvent e) { // Called by Timer.
reds = cycleColors(reds);
greens = cycleColors(greens);
blues = cycleColors(blues);
IndexColorModel colorModel = new IndexColorModel(8, 216, reds, greens, blues);
imageSource.newPixels(imageData, colorModel, 0, 1000);
repaint();
}
// Cycle the colors to the right by 1.
private byte[] cycleColors(byte[] colors) {
byte[] newColors = new byte[216];
newColors[0] = colors[215];
System.arraycopy(colors, 0, newColors, 1, 215);
return newColors;
}
}
关于预计算一个周期,然后动画图片会出现什么? –
@thomas上面的代码示例显示216个1000x1000像素的帧。计算帧使用每个像素4个字节。这是864 MB。我已经尝试过了,现在我特意避开它。 – dln385
不要预先计算所有的帧,只需做三个支架:3 * 216 * 216 =〜140K – trashgod