如果我正确理解了这个问题,则此示例采用 aBufferedImage
并将该图像中的所有像素替换为红色像素。
这是通过使用SwingWorker
. 基本上,这会复制原始图像并遍历像素数据,更新每个像素。然后,它通过复制该图像与 UI 重新同步该图像。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PixelMe {
public static void main(String[] args) {
new PixelMe();
}
public PixelMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public BufferedImage createImage() {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 100, 100);
g.dispose();
return image;
}
public class TestPane extends JPanel {
private JLabel label;
private BufferedImage master;
public TestPane() {
setLayout(new BorderLayout());
label = new JLabel(new ImageIcon(createImage()));
add(label);
JButton update = new JButton("Update");
add(update, BorderLayout.SOUTH);
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage image = (BufferedImage) ((ImageIcon)label.getIcon()).getImage();
new UpdateWorker(image, label).execute();
}
});
}
}
public class UpdateWorker extends SwingWorker<BufferedImage, BufferedImage> {
private BufferedImage copy;
private JLabel target;
public UpdateWorker(BufferedImage master, JLabel target) {
this.target = target;
copy = makeCopy(master);
}
public BufferedImage makeCopy(BufferedImage master) {
BufferedImage image = new BufferedImage(master.getWidth(), master.getHeight(), master.getType());
Graphics2D g = image.createGraphics();
g.drawImage(master, 0, 0, null);
g.dispose();
return image;
}
@Override
protected void process(List<BufferedImage> chunks) {
target.setIcon(new ImageIcon(chunks.get(chunks.size() - 1)));
}
@Override
protected BufferedImage doInBackground() throws Exception {
int pixel = Color.RED.getRGB();
for (int row = 0; row < copy.getHeight(); row++) {
for (int col = 0; col < copy.getWidth(); col++) {
copy.setRGB(col, row, pixel);
publish(makeCopy(copy));
}
}
return null;
}
}
}
应该注意的是,这是一个非常昂贵的示例,因为BufferedImage
每次更改像素都会创建一个新示例。您可以建立一个图像池并使用它们,因为实际上只对最后一个图像感兴趣(在process
方法内)或减少更新的数量,但这只是一个概念证明。